1use glib::translate::{FromGlib, GlibNoneError, IntoGlib, OptionIntoGlib, TryFromGlib};
4
5use super::{
6 Format, FormattedValue, FormattedValueError, FormattedValueFullRange, FormattedValueIntrinsic,
7 FormattedValueNoneBuilder, GenericFormattedValue,
8};
9use crate::ffi;
10
11pub trait SpecificFormattedValue: FormattedValue {}
12
13pub trait SpecificFormattedValueFullRange: FormattedValueFullRange {}
14
15pub trait SpecificFormattedValueIntrinsic: TryFromGlib<i64> + FormattedValueIntrinsic {}
23
24#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
52pub struct Buffers(u64);
53impl Buffers {
54 #[doc(alias = "GST_BUFFER_OFFSET_NONE")]
55 pub const OFFSET_NONE: u64 = ffi::GST_BUFFER_OFFSET_NONE;
56 pub const MAX: Self = Self(Self::OFFSET_NONE - 1);
57}
58
59impl Buffers {
60 #[track_caller]
68 #[inline]
69 pub const fn from_u64(buffers: u64) -> Self {
70 if buffers == ffi::GST_BUFFER_OFFSET_NONE {
71 panic!("`Buffers` value out of range");
72 }
73
74 Buffers(buffers)
75 }
76
77 #[track_caller]
85 #[inline]
86 pub fn from_usize(buffers: usize) -> Self {
87 Buffers::from_u64(buffers.try_into().unwrap())
88 }
89}
90
91impl_common_ops_for_newtype_uint!(Buffers, u64);
92impl_signed_div_mul!(Buffers, u64);
93impl_signed_int_into_signed!(Buffers, u64);
94impl_format_value_traits!(Buffers, Buffers, Buffers, u64);
95option_glib_newtype_from_to!(Buffers, Buffers::OFFSET_NONE);
96glib_newtype_display!(Buffers, DisplayableOptionBuffers, Format::Buffers);
97
98impl TryFrom<Buffers> for usize {
99 type Error = std::num::TryFromIntError;
100
101 fn try_from(value: Buffers) -> Result<Self, Self::Error> {
102 value.0.try_into()
103 }
104}
105
106pub trait BuffersFormatConstructor {
110 fn buffers(self) -> Buffers;
113}
114
115impl BuffersFormatConstructor for u64 {
116 #[track_caller]
117 #[inline]
118 fn buffers(self) -> Buffers {
119 Buffers::from_u64(self)
120 }
121}
122
123#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
159pub struct Bytes(u64);
160impl Bytes {
161 #[allow(non_upper_case_globals)]
164 pub const KiB: Self = Self(1024);
165 #[allow(non_upper_case_globals)]
168 pub const MiB: Self = Self(1024 * 1024);
169 #[allow(non_upper_case_globals)]
172 pub const GiB: Self = Self(1024 * 1024 * 1024);
173 pub const MAX: Self = Self(u64::MAX - 1);
174}
175
176impl Bytes {
177 #[track_caller]
185 #[inline]
186 pub const fn from_bytes(bytes: u64) -> Self {
187 Bytes::from_u64(bytes)
188 }
189
190 #[track_caller]
198 #[inline]
199 pub const fn from_kibibytes(kibibytes: u64) -> Self {
200 Bytes::from_u64(kibibytes * 1024)
201 }
202
203 #[track_caller]
211 #[inline]
212 pub const fn from_mebibytes(mebibytes: u64) -> Self {
213 Bytes::from_u64(mebibytes * 1024 * 1024)
214 }
215
216 #[track_caller]
224 #[inline]
225 pub const fn from_gibibytes(gibibytes: u64) -> Self {
226 Bytes::from_u64(gibibytes * 1024 * 1024 * 1024)
227 }
228
229 #[track_caller]
237 #[inline]
238 pub const fn from_u64(bytes: u64) -> Self {
239 if bytes == u64::MAX {
240 panic!("`Bytes` value out of range");
241 }
242
243 Bytes(bytes)
244 }
245
246 #[track_caller]
254 #[inline]
255 pub fn from_usize(bytes: usize) -> Self {
256 Bytes::from_u64(bytes.try_into().unwrap())
258 }
259}
260
261impl_common_ops_for_newtype_uint!(Bytes, u64);
262impl_signed_div_mul!(Bytes, u64);
263impl_signed_int_into_signed!(Bytes, u64);
264impl_format_value_traits!(Bytes, Bytes, Bytes, u64);
265option_glib_newtype_from_to!(Bytes, u64::MAX);
266glib_newtype_display!(Bytes, DisplayableOptionBytes, Format::Bytes);
267
268impl TryFrom<Bytes> for usize {
269 type Error = std::num::TryFromIntError;
270
271 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
272 value.0.try_into()
273 }
274}
275
276pub trait BytesFormatConstructor {
284 fn bytes(self) -> Bytes;
287
288 fn kibibytes(self) -> Bytes;
291
292 fn mebibytes(self) -> Bytes;
295
296 fn gibibytes(self) -> Bytes;
299}
300
301impl BytesFormatConstructor for u64 {
302 #[track_caller]
303 #[inline]
304 fn bytes(self) -> Bytes {
305 Bytes::from_u64(self)
306 }
307
308 #[track_caller]
309 #[inline]
310 fn kibibytes(self) -> Bytes {
311 Bytes::from_u64(self * 1024)
312 }
313
314 #[track_caller]
315 #[inline]
316 fn mebibytes(self) -> Bytes {
317 Bytes::from_u64(self * 1024 * 1024)
318 }
319
320 #[track_caller]
321 #[inline]
322 fn gibibytes(self) -> Bytes {
323 Bytes::from_u64(self * 1024 * 1024 * 1024)
324 }
325}
326
327#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
355pub struct Default(u64);
356impl Default {
357 pub const MAX: Self = Self(u64::MAX - 1);
358}
359
360impl Default {
361 #[track_caller]
369 #[inline]
370 pub const fn from_u64(quantity: u64) -> Self {
371 if quantity == u64::MAX {
372 panic!("`Default` value out of range");
373 }
374
375 Default(quantity)
376 }
377
378 #[track_caller]
386 #[inline]
387 pub fn from_usize(quantity: usize) -> Self {
388 Default::from_u64(quantity.try_into().unwrap())
390 }
391}
392
393impl_common_ops_for_newtype_uint!(Default, u64);
394impl_signed_div_mul!(Default, u64);
395impl_signed_int_into_signed!(Default, u64);
396impl_format_value_traits!(Default, Default, Default, u64);
397option_glib_newtype_from_to!(Default, u64::MAX);
398glib_newtype_display!(Default, DisplayableOptionDefault, Format::Default);
399
400impl TryFrom<Default> for usize {
401 type Error = std::num::TryFromIntError;
402
403 fn try_from(value: Default) -> Result<Self, Self::Error> {
404 value.0.try_into()
405 }
406}
407
408pub trait DefaultFormatConstructor {
412 fn default_format(self) -> Default;
415}
416
417impl DefaultFormatConstructor for u64 {
418 #[track_caller]
419 #[inline]
420 fn default_format(self) -> Default {
421 Default::from_u64(self)
422 }
423}
424
425pub type Time = super::ClockTime;
426
427#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
456pub struct Percent(u32);
457impl Percent {
458 #[doc(alias = "GST_FORMAT_PERCENT_MAX")]
459 pub const MAX: Self = Self(ffi::GST_FORMAT_PERCENT_MAX as u32);
460 #[doc(alias = "GST_FORMAT_PERCENT_SCALE")]
461 pub const SCALE: Self = Self(ffi::GST_FORMAT_PERCENT_SCALE as u32);
462
463 #[track_caller]
470 #[inline]
471 pub const fn from_percent(percent: u32) -> Self {
472 if percent > 100 {
473 panic!("`Percent` value out of range");
474 }
475
476 Percent(ffi::GST_FORMAT_PERCENT_SCALE as u32 * percent)
477 }
478
479 #[track_caller]
486 #[inline]
487 pub const fn from_ppm(ppm: u32) -> Self {
488 if ppm > ffi::GST_FORMAT_PERCENT_MAX as u32 {
489 panic!("`Percent` ppm value out of range");
490 }
491
492 Percent(ppm)
493 }
494
495 #[track_caller]
502 #[inline]
503 pub fn from_ratio(ratio: f32) -> Self {
504 Percent::try_from(ratio).expect("`Percent` ratio out of range")
506 }
507
508 #[track_caller]
511 #[inline]
512 pub fn percent(&self) -> u32 {
513 self.0 / ffi::GST_FORMAT_PERCENT_SCALE as u32
514 }
515
516 #[track_caller]
519 #[inline]
520 pub fn ppm(&self) -> u32 {
521 self.0
522 }
523
524 #[track_caller]
527 #[inline]
528 pub fn ratio(&self) -> f32 {
529 self.0 as f32 / ffi::GST_FORMAT_PERCENT_MAX as f32
530 }
531}
532
533impl_common_ops_for_newtype_uint!(Percent, u32, one: ffi::GST_FORMAT_PERCENT_SCALE as u32);
534impl_signed_div_mul!(Percent, u32);
535impl_signed_int_into_signed!(Percent, u32);
536
537impl FormattedValue for Option<Percent> {
538 type FullRange = Option<Percent>;
539
540 #[inline]
541 fn default_format() -> Format {
542 Format::Percent
543 }
544
545 #[inline]
546 fn format(&self) -> Format {
547 Format::Percent
548 }
549
550 #[inline]
551 fn is_some(&self) -> bool {
552 Option::is_some(self)
553 }
554
555 #[inline]
556 unsafe fn into_raw_value(self) -> i64 {
557 self.map_or(-1, |v| v.0 as i64)
558 }
559}
560
561impl FormattedValueFullRange for Option<Percent> {
562 #[inline]
563 unsafe fn from_raw(format: Format, value: i64) -> Self {
564 unsafe {
565 debug_assert_eq!(format, Format::Percent);
566 Percent::try_from_glib(value).ok()
567 }
568 }
569}
570
571impl From<Option<Percent>> for GenericFormattedValue {
572 #[inline]
573 fn from(v: Option<Percent>) -> Self {
574 skip_assert_initialized!();
575 GenericFormattedValue::Percent(v)
576 }
577}
578
579impl From<Percent> for GenericFormattedValue {
580 #[inline]
581 fn from(v: Percent) -> Self {
582 skip_assert_initialized!();
583 GenericFormattedValue::Percent(Some(v))
584 }
585}
586
587impl FormattedValue for Percent {
588 type FullRange = Option<Percent>;
589
590 #[inline]
591 fn default_format() -> Format {
592 Format::Percent
593 }
594
595 #[inline]
596 fn format(&self) -> Format {
597 Format::Percent
598 }
599
600 #[inline]
601 fn is_some(&self) -> bool {
602 true
603 }
604
605 #[inline]
606 unsafe fn into_raw_value(self) -> i64 {
607 self.0 as i64
608 }
609}
610
611impl TryFrom<u64> for Percent {
612 type Error = GlibNoneError;
613
614 #[inline]
615 fn try_from(v: u64) -> Result<Percent, GlibNoneError> {
616 skip_assert_initialized!();
617 unsafe { Self::try_from_glib(v as i64) }
618 }
619}
620
621impl TryFromGlib<i64> for Percent {
622 type Error = GlibNoneError;
623 #[inline]
624 unsafe fn try_from_glib(value: i64) -> Result<Self, Self::Error> {
625 skip_assert_initialized!();
626 if value < 0 || value > ffi::GST_FORMAT_PERCENT_MAX {
627 Err(GlibNoneError)
628 } else {
629 Ok(Percent(value as u32))
630 }
631 }
632}
633
634impl TryFrom<u32> for Percent {
635 type Error = FormattedValueError;
636
637 #[inline]
638 fn try_from(value: u32) -> Result<Self, Self::Error> {
639 skip_assert_initialized!();
640 if value > ffi::GST_FORMAT_PERCENT_MAX as u32 {
641 Err(FormattedValueError(Format::Percent))
642 } else {
643 Ok(Percent(value))
644 }
645 }
646}
647
648impl TryFrom<GenericFormattedValue> for Option<Percent> {
649 type Error = FormattedValueError;
650
651 #[inline]
652 fn try_from(v: GenericFormattedValue) -> Result<Option<Percent>, Self::Error> {
653 skip_assert_initialized!();
654 if let GenericFormattedValue::Percent(v) = v {
655 Ok(v)
656 } else {
657 Err(FormattedValueError(v.format()))
658 }
659 }
660}
661
662impl FormattedValueIntrinsic for Percent {}
663impl SpecificFormattedValue for Option<Percent> {}
664impl SpecificFormattedValueFullRange for Option<Percent> {}
665impl SpecificFormattedValueIntrinsic for Percent {}
666impl FormattedValueNoneBuilder for Option<Percent> {
667 #[inline]
668 fn none() -> Option<Percent> {
669 None
670 }
671}
672
673#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
674#[error("value out of range")]
675pub struct TryPercentFromFloatError(());
676
677impl TryFrom<f64> for Percent {
678 type Error = TryPercentFromFloatError;
679
680 #[inline]
681 fn try_from(v: f64) -> Result<Self, Self::Error> {
682 skip_assert_initialized!();
683 if v < 0.0 || v > 1.0 {
684 Err(TryPercentFromFloatError(()))
685 } else {
686 Ok(Percent(
687 (v * ffi::GST_FORMAT_PERCENT_MAX as f64).round() as u32
688 ))
689 }
690 }
691}
692
693impl TryFrom<f32> for Percent {
694 type Error = TryPercentFromFloatError;
695
696 #[inline]
697 fn try_from(v: f32) -> Result<Self, Self::Error> {
698 skip_assert_initialized!();
699 if v < 0.0 || v > 1.0 {
700 Err(TryPercentFromFloatError(()))
701 } else {
702 Ok(Percent(
703 (v * ffi::GST_FORMAT_PERCENT_MAX as f32).round() as u32
704 ))
705 }
706 }
707}
708
709pub trait PercentFormatIntegerConstructor {
713 fn percent(self) -> Percent;
716
717 fn ppm(self) -> Percent;
720}
721
722impl PercentFormatIntegerConstructor for u32 {
723 #[track_caller]
724 #[inline]
725 fn percent(self) -> Percent {
726 Percent::from_percent(self)
727 }
728
729 #[track_caller]
730 #[inline]
731 fn ppm(self) -> Percent {
732 Percent::from_ppm(self)
733 }
734}
735
736pub trait PercentFormatFloatConstructor {
740 fn percent_ratio(self) -> Percent;
743}
744
745impl PercentFormatFloatConstructor for f32 {
746 #[track_caller]
747 #[inline]
748 fn percent_ratio(self) -> Percent {
749 Percent::try_from(self).unwrap()
750 }
751}
752
753impl std::fmt::Display for Percent {
754 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
755 std::fmt::Display::fmt(&(self.0 as f32 / (*Percent::SCALE) as f32), f)?;
756 f.write_str(" %")
757 }
758}
759
760impl crate::utils::Displayable for Percent {
761 type DisplayImpl = Self;
762 fn display(self) -> Self {
763 self
764 }
765}
766pub struct DisplayableOptionPercent(Option<Percent>);
767
768impl std::fmt::Display for DisplayableOptionPercent {
769 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
770 if let Some(val) = self.0.as_ref() {
771 std::fmt::Display::fmt(val, f)
772 } else {
773 f.write_str("undef. %")
774 }
775 }
776}
777
778impl crate::utils::Displayable for Option<Percent> {
779 type DisplayImpl = DisplayableOptionPercent;
780 fn display(self) -> Self::DisplayImpl {
781 DisplayableOptionPercent(self)
782 }
783}