Skip to main content

gstreamer/format/
clock_time.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{
4    fmt,
5    io::{self, prelude::*},
6    time::Duration,
7};
8
9use crate::{ffi, prelude::*};
10use glib::translate::*;
11
12use super::{
13    Format, FormattedValue, FormattedValueError, FormattedValueFullRange, FormattedValueIntrinsic,
14    FormattedValueNoneBuilder, GenericFormattedValue, Signed, SpecificFormattedValue,
15    SpecificFormattedValueFullRange, SpecificFormattedValueIntrinsic,
16};
17
18const TRY_FROM_FLOAT_SECS_ERROR_MSG: &str =
19    "can not convert float seconds to ClockTime: value is either negative, too big or NaN";
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct TryFromFloatSecsError;
23
24impl fmt::Display for TryFromFloatSecsError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.write_str(TRY_FROM_FLOAT_SECS_ERROR_MSG)
27    }
28}
29
30impl std::error::Error for TryFromFloatSecsError {}
31
32// rustdoc-stripper-ignore-next
33/// A Time quantity
34///
35/// Some functions enforce format specific quantities. This type can be used when
36/// a Duration or an Instant is expected. It comes with functions to perform computations without the
37/// need to retrieve the inner integer.
38///
39/// # Examples
40///
41/// ```rust
42/// # use gstreamer::{prelude::*, ClockTime};
43/// // Regular constructors (can be used in `const` contexts)
44/// const FORTY_TWO_NS: ClockTime = ClockTime::from_nseconds(42);
45/// const TWO_US: ClockTime = ClockTime::from_useconds(2);
46/// let three_ms: ClockTime = ClockTime::from_mseconds(3);
47/// let four_s: ClockTime = ClockTime::from_seconds(4);
48///
49/// // Convenience constructors (not `const`)
50/// let forty_two_ns = 42.nseconds();
51/// let two_us = 2.useconds();
52/// let three_ms = 3.mseconds();
53/// let four_s = 4.seconds();
54///
55/// // All four arithmetic operations
56/// let deadline = (2.mseconds() + 512.useconds()) * 2 / 3;
57///
58/// // Comparisons
59/// if deadline > ClockTime::MSECOND {
60///     println!("Greater");
61/// }
62/// ```
63///
64/// See [the documentation of the `format` module] for more examples.
65///
66/// [the documentation of the `format` module]: ./index.html
67#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Default)]
68pub struct ClockTime(u64);
69
70impl ClockTime {
71    #[doc(alias = "GST_SECOND")]
72    pub const SECOND: ClockTime = ClockTime(1_000_000_000);
73    #[doc(alias = "GST_MSECOND")]
74    pub const MSECOND: ClockTime = ClockTime(1_000_000);
75    #[doc(alias = "GST_USECOND")]
76    pub const USECOND: ClockTime = ClockTime(1_000);
77    #[doc(alias = "GST_NSECOND")]
78    pub const NSECOND: ClockTime = ClockTime(1);
79    // checker-ignore-item
80    pub const MAX: ClockTime = ClockTime(ffi::GST_CLOCK_TIME_NONE - 1);
81
82    #[inline]
83    pub const fn hours(self) -> u64 {
84        self.0 / Self::SECOND.0 / 60 / 60
85    }
86
87    #[inline]
88    pub const fn minutes(self) -> u64 {
89        self.0 / Self::SECOND.0 / 60
90    }
91
92    #[inline]
93    pub const fn seconds(self) -> u64 {
94        self.0 / Self::SECOND.0
95    }
96
97    #[inline]
98    pub fn seconds_f32(self) -> f32 {
99        self.0 as f32 / Self::SECOND.0 as f32
100    }
101
102    #[inline]
103    pub fn seconds_f64(self) -> f64 {
104        self.0 as f64 / Self::SECOND.0 as f64
105    }
106
107    #[inline]
108    pub const fn mseconds(self) -> u64 {
109        self.0 / Self::MSECOND.0
110    }
111
112    #[inline]
113    pub const fn useconds(self) -> u64 {
114        self.0 / Self::USECOND.0
115    }
116
117    #[inline]
118    pub const fn nseconds(self) -> u64 {
119        self.0
120    }
121
122    // rustdoc-stripper-ignore-next
123    /// Builds a new `ClockTime` which value is the given number of seconds.
124    ///
125    /// # Panics
126    ///
127    /// Panics if the resulting duration in nanoseconds exceeds the `u64` range.
128    #[track_caller]
129    #[inline]
130    pub const fn from_seconds(seconds: u64) -> Self {
131        skip_assert_initialized!();
132        ClockTime(
133            seconds
134                .checked_mul(Self::SECOND.0)
135                .expect("Out of `ClockTime` range"),
136        )
137    }
138
139    // rustdoc-stripper-ignore-next
140    /// Builds a new `ClockTime` which value is the given number of seconds.
141    ///
142    /// Returns an error if seconds is negative, infinite or NaN, or
143    /// the resulting duration in nanoseconds exceeds the `u64` range.
144    #[inline]
145    pub fn try_from_seconds_f32(seconds: f32) -> Result<Self, TryFromFloatSecsError> {
146        skip_assert_initialized!();
147
148        let dur = Duration::try_from_secs_f32(seconds).map_err(|_| TryFromFloatSecsError)?;
149        ClockTime::try_from(dur).map_err(|_| TryFromFloatSecsError)
150    }
151
152    // rustdoc-stripper-ignore-next
153    /// Builds a new `ClockTime` which value is the given number of seconds.
154    ///
155    /// # Panics
156    ///
157    /// Panics if seconds is negative, infinite or NaN, or the resulting duration
158    /// in nanoseconds exceeds the `u64` range.
159    #[track_caller]
160    #[inline]
161    pub fn from_seconds_f32(seconds: f32) -> Self {
162        skip_assert_initialized!();
163
164        Self::try_from_seconds_f32(seconds).expect(TRY_FROM_FLOAT_SECS_ERROR_MSG)
165    }
166
167    // rustdoc-stripper-ignore-next
168    /// Builds a new `ClockTime` which value is the given number of seconds.
169    ///
170    /// Returns an error if seconds is negative, infinite or NaN, or
171    /// the resulting duration in nanoseconds exceeds the `u64` range.
172    #[inline]
173    pub fn try_from_seconds_f64(seconds: f64) -> Result<Self, TryFromFloatSecsError> {
174        skip_assert_initialized!();
175
176        let dur = Duration::try_from_secs_f64(seconds).map_err(|_| TryFromFloatSecsError)?;
177        ClockTime::try_from(dur).map_err(|_| TryFromFloatSecsError)
178    }
179
180    // rustdoc-stripper-ignore-next
181    /// Builds a new `ClockTime` which value is the given number of seconds.
182    ///
183    /// # Panics
184    ///
185    /// Panics if seconds is negative, infinite or NaN, or the resulting duration
186    /// in nanoseconds exceeds the `u64` range.
187    #[track_caller]
188    #[inline]
189    pub fn from_seconds_f64(seconds: f64) -> Self {
190        skip_assert_initialized!();
191
192        Self::try_from_seconds_f64(seconds).expect(TRY_FROM_FLOAT_SECS_ERROR_MSG)
193    }
194
195    // rustdoc-stripper-ignore-next
196    /// Builds a new `ClockTime` which value is the given number of milliseconds.
197    ///
198    /// # Panics
199    ///
200    /// Panics if the resulting duration in nanoseconds exceeds the `u64` range.
201    #[track_caller]
202    #[inline]
203    pub const fn from_mseconds(mseconds: u64) -> Self {
204        skip_assert_initialized!();
205        ClockTime(
206            mseconds
207                .checked_mul(Self::MSECOND.0)
208                .expect("Out of `ClockTime` range"),
209        )
210    }
211
212    // rustdoc-stripper-ignore-next
213    /// Builds a new `ClockTime` which value is the given number of microseconds.
214    ///
215    /// # Panics
216    ///
217    /// Panics if the resulting duration in nanoseconds exceeds the `u64` range.
218    #[track_caller]
219    #[inline]
220    pub const fn from_useconds(useconds: u64) -> Self {
221        skip_assert_initialized!();
222        ClockTime(
223            useconds
224                .checked_mul(Self::USECOND.0)
225                .expect("Out of `ClockTime` range"),
226        )
227    }
228
229    // rustdoc-stripper-ignore-next
230    /// Builds a new `ClockTime` which value is the given number of nanoseconds.
231    ///
232    /// # Panics
233    ///
234    /// Panics if the requested duration equals `GST_CLOCK_TIME_NONE`
235    /// (`u64::MAX`).
236    #[track_caller]
237    #[inline]
238    pub const fn from_nseconds(nseconds: u64) -> Self {
239        skip_assert_initialized!();
240        assert!(
241            nseconds != ffi::GST_CLOCK_TIME_NONE,
242            "Attempt to build a `ClockTime` with value `GST_CLOCK_TIME_NONE`",
243        );
244        ClockTime(nseconds * Self::NSECOND.0)
245    }
246}
247
248impl Signed<ClockTime> {
249    // rustdoc-stripper-ignore-next
250    /// Returns the `self` in nanoseconds.
251    #[inline]
252    pub fn nseconds(self) -> Signed<u64> {
253        match self {
254            Signed::Positive(val) => Signed::Positive(val.nseconds()),
255            Signed::Negative(val) => Signed::Negative(val.nseconds()),
256        }
257    }
258
259    // rustdoc-stripper-ignore-next
260    /// Creates new value from nanoseconds.
261    #[inline]
262    pub fn from_nseconds(val: Signed<u64>) -> Self {
263        skip_assert_initialized!();
264        match val {
265            Signed::Positive(val) => Signed::Positive(ClockTime::from_nseconds(val)),
266            Signed::Negative(val) => Signed::Negative(ClockTime::from_nseconds(val)),
267        }
268    }
269
270    // rustdoc-stripper-ignore-next
271    /// Returns the `self` in microseconds.
272    #[inline]
273    pub fn useconds(self) -> Signed<u64> {
274        match self {
275            Signed::Positive(val) => Signed::Positive(val.useconds()),
276            Signed::Negative(val) => Signed::Negative(val.useconds()),
277        }
278    }
279
280    // rustdoc-stripper-ignore-next
281    /// Creates new value from microseconds.
282    #[inline]
283    pub fn from_useconds(val: Signed<u64>) -> Self {
284        skip_assert_initialized!();
285        match val {
286            Signed::Positive(val) => Signed::Positive(ClockTime::from_useconds(val)),
287            Signed::Negative(val) => Signed::Negative(ClockTime::from_useconds(val)),
288        }
289    }
290
291    // rustdoc-stripper-ignore-next
292    /// Returns the `self` in milliseconds.
293    #[inline]
294    pub fn mseconds(self) -> Signed<u64> {
295        match self {
296            Signed::Positive(val) => Signed::Positive(val.mseconds()),
297            Signed::Negative(val) => Signed::Negative(val.mseconds()),
298        }
299    }
300
301    // rustdoc-stripper-ignore-next
302    /// Creates new value from milliseconds.
303    #[inline]
304    pub fn from_mseconds(val: Signed<u64>) -> Self {
305        skip_assert_initialized!();
306        match val {
307            Signed::Positive(val) => Signed::Positive(ClockTime::from_mseconds(val)),
308            Signed::Negative(val) => Signed::Negative(ClockTime::from_mseconds(val)),
309        }
310    }
311
312    // rustdoc-stripper-ignore-next
313    /// Returns the `self` in seconds.
314    #[inline]
315    pub fn seconds(self) -> Signed<u64> {
316        match self {
317            Signed::Positive(val) => Signed::Positive(val.seconds()),
318            Signed::Negative(val) => Signed::Negative(val.seconds()),
319        }
320    }
321
322    // rustdoc-stripper-ignore-next
323    /// Returns the `self` in f32 seconds.
324    #[inline]
325    pub fn seconds_f32(self) -> f32 {
326        match self {
327            Signed::Positive(val) => val.seconds_f32(),
328            Signed::Negative(val) => -val.seconds_f32(),
329        }
330    }
331
332    // rustdoc-stripper-ignore-next
333    /// Returns the `self` in f64 seconds.
334    #[inline]
335    pub fn seconds_f64(self) -> f64 {
336        match self {
337            Signed::Positive(val) => val.seconds_f64(),
338            Signed::Negative(val) => -val.seconds_f64(),
339        }
340    }
341
342    // rustdoc-stripper-ignore-next
343    /// Creates new value from seconds.
344    #[inline]
345    pub fn from_seconds(val: Signed<u64>) -> Self {
346        skip_assert_initialized!();
347        match val {
348            Signed::Positive(val) => Signed::Positive(ClockTime::from_seconds(val)),
349            Signed::Negative(val) => Signed::Negative(ClockTime::from_seconds(val)),
350        }
351    }
352
353    // rustdoc-stripper-ignore-next
354    /// Builds a new `Signed<ClockTime>` which value is the given number of seconds.
355    ///
356    /// Returns an error if seconds is infinite or NaN, or
357    /// the resulting duration in nanoseconds exceeds the `u64` range.
358    #[inline]
359    pub fn try_from_seconds_f32(seconds: f32) -> Result<Self, TryFromFloatSecsError> {
360        skip_assert_initialized!();
361
362        ClockTime::try_from_seconds_f32(seconds.abs()).map(|ct| {
363            if seconds.is_sign_positive() {
364                Signed::Positive(ct)
365            } else {
366                Signed::Negative(ct)
367            }
368        })
369    }
370
371    // rustdoc-stripper-ignore-next
372    /// Builds a new `Signed<ClockTime>` which value is the given number of seconds.
373    ///
374    /// # Panics
375    ///
376    /// Panics if seconds is infinite or NaN, or the resulting duration
377    /// in nanoseconds exceeds the `u64` range.
378    #[track_caller]
379    #[inline]
380    pub fn from_seconds_f32(seconds: f32) -> Self {
381        skip_assert_initialized!();
382
383        Self::try_from_seconds_f32(seconds).expect(TRY_FROM_FLOAT_SECS_ERROR_MSG)
384    }
385
386    // rustdoc-stripper-ignore-next
387    /// Builds a new `Signed<ClockTime>` which value is the given number of seconds.
388    ///
389    /// Returns an error if seconds is infinite or NaN, or
390    /// the resulting duration in nanoseconds exceeds the `u64` range.
391    #[inline]
392    pub fn try_from_seconds_f64(seconds: f64) -> Result<Self, TryFromFloatSecsError> {
393        skip_assert_initialized!();
394
395        ClockTime::try_from_seconds_f64(seconds.abs()).map(|ct| {
396            if seconds.is_sign_positive() {
397                Signed::Positive(ct)
398            } else {
399                Signed::Negative(ct)
400            }
401        })
402    }
403
404    // rustdoc-stripper-ignore-next
405    /// Builds a new `Signed<ClockTime>` which value is the given number of seconds.
406    ///
407    /// # Panics
408    ///
409    /// Panics if seconds is infinite or NaN, or the resulting duration
410    /// in nanoseconds exceeds the `u64` range.
411    #[track_caller]
412    #[inline]
413    pub fn from_seconds_f64(seconds: f64) -> Self {
414        skip_assert_initialized!();
415
416        Self::try_from_seconds_f64(seconds).expect(TRY_FROM_FLOAT_SECS_ERROR_MSG)
417    }
418}
419
420impl_format_value_traits!(ClockTime, Time, Time, u64);
421option_glib_newtype_from_to!(ClockTime, ffi::GST_CLOCK_TIME_NONE);
422
423// FIXME `functions in traits cannot be const` (rustc 1.64.0)
424// rustdoc-stripper-ignore-next
425/// `ClockTime` formatted value constructor trait.
426pub trait TimeFormatConstructor {
427    // rustdoc-stripper-ignore-next
428    /// Builds a `ClockTime` formatted value from `self` interpreted as nano seconds.
429    fn nseconds(self) -> ClockTime;
430
431    // rustdoc-stripper-ignore-next
432    /// Builds a `ClockTime` formatted value from `self` interpreted as micro seconds.
433    fn useconds(self) -> ClockTime;
434
435    // rustdoc-stripper-ignore-next
436    /// Builds a `ClockTime` formatted value from `self` interpreted as milli seconds.
437    fn mseconds(self) -> ClockTime;
438
439    // rustdoc-stripper-ignore-next
440    /// Builds a `ClockTime` formatted value from `self` interpreted as seconds.
441    fn seconds(self) -> ClockTime;
442
443    // rustdoc-stripper-ignore-next
444    /// Builds a `ClockTime` formatted value from `self` interpreted as minutes.
445    fn minutes(self) -> ClockTime;
446
447    // rustdoc-stripper-ignore-next
448    /// Builds a `ClockTime` formatted value from `self` interpreted as hours.
449    fn hours(self) -> ClockTime;
450}
451
452impl TimeFormatConstructor for u64 {
453    #[track_caller]
454    #[inline]
455    fn nseconds(self) -> ClockTime {
456        ClockTime::from_nseconds(self)
457    }
458
459    #[track_caller]
460    #[inline]
461    fn useconds(self) -> ClockTime {
462        ClockTime::from_useconds(self)
463    }
464
465    #[track_caller]
466    #[inline]
467    fn mseconds(self) -> ClockTime {
468        ClockTime::from_mseconds(self)
469    }
470
471    #[track_caller]
472    #[inline]
473    fn seconds(self) -> ClockTime {
474        ClockTime::from_seconds(self)
475    }
476
477    #[track_caller]
478    #[inline]
479    fn minutes(self) -> ClockTime {
480        ClockTime::from_seconds(self * 60)
481    }
482
483    #[track_caller]
484    #[inline]
485    fn hours(self) -> ClockTime {
486        ClockTime::from_seconds(self * 60 * 60)
487    }
488}
489
490impl glib::value::ValueType for ClockTime {
491    type Type = Self;
492}
493
494pub enum ClockTimeValueTypeOrNoneChecker {}
495
496unsafe impl glib::value::ValueTypeChecker for ClockTimeValueTypeOrNoneChecker {
497    type Error = glib::value::ValueTypeMismatchOrNoneError<glib::value::ValueTypeMismatchError>;
498
499    #[inline]
500    fn check(value: &glib::Value) -> Result<(), Self::Error> {
501        skip_assert_initialized!();
502        glib::value::GenericValueTypeChecker::<ClockTime>::check(value)?;
503
504        let gct = unsafe { glib::gobject_ffi::g_value_get_uint64(value.to_glib_none().0) };
505        if gct == ffi::GST_CLOCK_TIME_NONE {
506            return Err(glib::value::ValueTypeMismatchOrNoneError::UnexpectedNone);
507        }
508
509        Ok(())
510    }
511}
512
513unsafe impl glib::value::FromValue<'_> for ClockTime {
514    type Checker = ClockTimeValueTypeOrNoneChecker;
515
516    #[inline]
517    unsafe fn from_value(value: &glib::Value) -> ClockTime {
518        unsafe {
519            skip_assert_initialized!();
520            ClockTime(glib::gobject_ffi::g_value_get_uint64(
521                value.to_glib_none().0,
522            ))
523        }
524    }
525}
526
527impl glib::value::ToValue for ClockTime {
528    #[inline]
529    fn to_value(&self) -> glib::Value {
530        let mut value = glib::Value::for_value_type::<ClockTime>();
531        let gct = self.into_glib();
532        if gct == ffi::GST_CLOCK_TIME_NONE {
533            crate::warning!(
534                crate::CAT_RUST,
535                "converting a defined `ClockTime` with value `GST_CLOCK_TIME_NONE` to `Value`, this is probably not what you wanted.",
536            );
537        }
538        unsafe { glib::gobject_ffi::g_value_set_uint64(value.to_glib_none_mut().0, gct) }
539        value
540    }
541
542    #[inline]
543    fn value_type(&self) -> glib::Type {
544        Self::static_type()
545    }
546}
547
548impl glib::value::ToValueOptional for ClockTime {
549    #[inline]
550    fn to_value_optional(opt: Option<&Self>) -> glib::Value {
551        skip_assert_initialized!();
552        let mut value = glib::Value::for_value_type::<ClockTime>();
553        let inner = opt.map(|inner| inner.0).unwrap_or(ffi::GST_CLOCK_TIME_NONE);
554        unsafe { glib::gobject_ffi::g_value_set_uint64(value.to_glib_none_mut().0, inner) };
555
556        value
557    }
558}
559
560impl From<ClockTime> for glib::Value {
561    #[inline]
562    fn from(v: ClockTime) -> glib::Value {
563        glib::value::ToValue::to_value(&v)
564    }
565}
566
567#[doc(hidden)]
568impl StaticType for ClockTime {
569    #[inline]
570    fn static_type() -> glib::Type {
571        <u64 as StaticType>::static_type()
572    }
573}
574
575impl HasParamSpec for ClockTime {
576    type ParamSpec = glib::ParamSpecUInt64;
577    type SetValue = Self;
578    type BuilderFn = fn(&str) -> glib::ParamSpecUInt64Builder;
579
580    fn param_spec_builder() -> Self::BuilderFn {
581        Self::ParamSpec::builder
582    }
583}
584
585#[derive(Debug)]
586pub struct DurationError;
587
588impl fmt::Display for DurationError {
589    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
590        write!(fmt, "out of range conversion from Duration attempted")
591    }
592}
593
594impl std::error::Error for DurationError {}
595
596impl TryFrom<Duration> for ClockTime {
597    type Error = DurationError;
598
599    #[inline]
600    fn try_from(d: Duration) -> Result<Self, Self::Error> {
601        skip_assert_initialized!();
602
603        let nanos = d.as_nanos();
604
605        // Note: `u64::MAX` is `ClockTime::NONE`.
606        if nanos >= u64::MAX as u128 {
607            return Err(DurationError);
608        }
609
610        Ok(ClockTime::from_nseconds(nanos as u64))
611    }
612}
613
614impl From<ClockTime> for Duration {
615    #[inline]
616    fn from(t: ClockTime) -> Self {
617        skip_assert_initialized!();
618
619        Duration::from_nanos(t.nseconds())
620    }
621}
622
623impl_common_ops_for_newtype_uint!(ClockTime, u64);
624impl_signed_div_mul!(ClockTime, u64);
625impl_signed_int_into_signed!(ClockTime, u64);
626
627// rustdoc-stripper-ignore-next
628/// Tell [`pad_clocktime`] what kind of time we're formatting
629enum Sign {
630    // rustdoc-stripper-ignore-next
631    /// An undefined time (`None`)
632    Undefined,
633
634    // rustdoc-stripper-ignore-next
635    /// A non-negative time (zero or greater)
636    NonNegative,
637
638    // For a future ClockTimeDiff formatting
639    #[allow(dead_code)]
640    // rustdoc-stripper-ignore-next
641    /// A negative time (below zero)
642    Negative,
643}
644
645// Derived from libcore `Formatter::pad_integral` (same APACHE v2 + MIT licenses)
646//
647// TODO: Would be useful for formatting ClockTimeDiff
648// if it was a new type instead of an alias for i64
649//
650// rustdoc-stripper-ignore-next
651/// Performs the correct padding for a clock time which has already been
652/// emitted into a str, as by [`write_clocktime`]. The str should *not*
653/// contain the sign; that will be added by this method.
654fn pad_clocktime(f: &mut fmt::Formatter<'_>, sign: Sign, buf: &str) -> fmt::Result {
655    skip_assert_initialized!();
656    use std::fmt::{Alignment, Write};
657
658    use self::Sign::*;
659
660    // Start by determining how we're padding, gathering
661    // settings from the Formatter and the Sign
662
663    // Choose the fill character
664    let sign_aware_zero_pad = f.sign_aware_zero_pad();
665    let fill_char = match sign {
666        Undefined if sign_aware_zero_pad => '-', // Zero-padding an undefined time
667        _ if sign_aware_zero_pad => '0',         // Zero-padding a valid time
668        _ => f.fill(),                           // Otherwise, pad with the user-chosen character
669    };
670
671    // Choose the sign character
672    let sign_plus = f.sign_plus();
673    let sign_char = match sign {
674        Undefined if sign_plus => Some(fill_char), // User requested sign, time is undefined
675        NonNegative if sign_plus => Some('+'),     // User requested sign, time is zero or above
676        Negative => Some('-'),                     // Time is below zero
677        _ => None,                                 // Otherwise, add no sign
678    };
679
680    // Our minimum width is the value's width, plus 1 for the sign if present
681    let width = buf.len() + sign_char.map_or(0, |_| 1);
682
683    // Subtract the minimum width from the requested width to get the padding,
684    // taking care not to allow wrapping due to underflow
685    let padding = f.width().unwrap_or(0).saturating_sub(width);
686
687    // Split the required padding into the three possible parts
688    let align = f.align().unwrap_or(Alignment::Right);
689    let (pre_padding, zero_padding, post_padding) = match align {
690        _ if sign_aware_zero_pad => (0, padding, 0), // Zero-padding: Pad between sign and value
691        Alignment::Left => (0, 0, padding),          // Align left: Pad on the right side
692        Alignment::Right => (padding, 0, 0),         // Align right: Pad on the left side
693
694        // Align center: Split equally between left and right side
695        // If the required padding is odd, the right side gets one more char
696        Alignment::Center => (padding / 2, 0, padding.div_ceil(2)),
697    };
698
699    // And now for the actual writing
700
701    for _ in 0..pre_padding {
702        f.write_char(fill_char)?; // Left padding
703    }
704    if let Some(c) = sign_char {
705        f.write_char(c)?; // ------- Sign character
706    }
707    for _ in 0..zero_padding {
708        f.write_char(fill_char)?; // Padding between sign and value
709    }
710    f.write_str(buf)?; // ---------- Value
711    for _ in 0..post_padding {
712        f.write_char(fill_char)?; // Right padding
713    }
714
715    Ok(())
716}
717
718// rustdoc-stripper-ignore-next
719/// Writes an unpadded, signless clocktime string with the given precision
720fn write_clocktime<W: io::Write>(
721    mut writer: W,
722    clocktime: Option<ClockTime>,
723    precision: usize,
724) -> io::Result<()> {
725    skip_assert_initialized!();
726    let precision = std::cmp::min(9, precision);
727
728    if let Some(ns) = clocktime.map(ClockTime::nseconds) {
729        // Split the time into parts
730        let (s, ns) = num_integer::div_rem(ns, 1_000_000_000);
731        let (m, s) = num_integer::div_rem(s, 60);
732        let (h, m) = num_integer::div_rem(m, 60);
733
734        // Write HH:MM:SS
735        write!(writer, "{h}:{m:02}:{s:02}")?;
736
737        if precision > 0 {
738            // Format the nanoseconds into a stack-allocated string
739            // The value is zero-padded so always 9 digits long
740            let mut buf = [0u8; 9];
741            write!(&mut buf[..], "{ns:09}").unwrap();
742            let buf_str = std::str::from_utf8(&buf[..]).unwrap();
743
744            // Write decimal point and a prefix of the nanoseconds for more precision
745            write!(writer, ".{buf_str:.precision$}")?;
746        }
747    } else {
748        // Undefined time
749
750        // Write HH:MM:SS, but invalid
751        write!(writer, "--:--:--")?;
752
753        if precision > 0 {
754            // Write decimal point and dashes for more precision
755            write!(writer, ".{:->p$}", "", p = precision)?;
756        }
757    }
758
759    Ok(())
760}
761
762fn fmt_opt_clock_time(ct: Option<ClockTime>, f: &mut fmt::Formatter) -> fmt::Result {
763    skip_assert_initialized!();
764    let precision = f.precision().unwrap_or(9);
765
766    // What the maximum time (u64::MAX - 1) would format to
767    const MAX_SIZE: usize = "5124095:34:33.709551614".len();
768
769    // Write the unpadded clocktime value into a stack-allocated string
770    let mut buf = [0u8; MAX_SIZE];
771    let mut cursor = io::Cursor::new(&mut buf[..]);
772    write_clocktime(&mut cursor, ct, precision).unwrap();
773    let pos = cursor.position() as usize;
774    let buf_str = std::str::from_utf8(&buf[..pos]).unwrap();
775
776    let sign = if ct.is_some() {
777        Sign::NonNegative
778    } else {
779        Sign::Undefined
780    };
781
782    pad_clocktime(f, sign, buf_str)
783}
784
785impl fmt::Display for ClockTime {
786    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
787        fmt_opt_clock_time(Some(*self), f)
788    }
789}
790
791impl fmt::Debug for ClockTime {
792    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
793        fmt::Display::fmt(self, f)
794    }
795}
796
797pub struct DisplayableOptClockTime(Option<ClockTime>);
798
799impl fmt::Display for DisplayableOptClockTime {
800    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
801        fmt_opt_clock_time(self.0, f)
802    }
803}
804
805impl fmt::Debug for DisplayableOptClockTime {
806    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
807        fmt::Display::fmt(self, f)
808    }
809}
810
811impl crate::utils::Displayable for Option<ClockTime> {
812    type DisplayImpl = DisplayableOptClockTime;
813
814    fn display(self) -> DisplayableOptClockTime {
815        DisplayableOptClockTime(self)
816    }
817}
818
819impl crate::utils::Displayable for ClockTime {
820    type DisplayImpl = ClockTime;
821
822    fn display(self) -> ClockTime {
823        self
824    }
825}
826
827impl std::iter::Sum for ClockTime {
828    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
829        skip_assert_initialized!();
830        iter.fold(ClockTime::ZERO, |a, b| a + b)
831    }
832}
833
834#[cfg(test)]
835mod tests {
836    use opt_ops::prelude::*;
837
838    use super::*;
839    use crate::format::{Signed, UnsignedIntoSigned};
840
841    const CT_1: ClockTime = ClockTime::from_nseconds(1);
842    const CT_2: ClockTime = ClockTime::from_nseconds(2);
843    const CT_3: ClockTime = ClockTime::from_nseconds(3);
844    const CT_10: ClockTime = ClockTime::from_nseconds(10);
845    const CT_20: ClockTime = ClockTime::from_nseconds(20);
846    const CT_30: ClockTime = ClockTime::from_nseconds(30);
847
848    const P_CT_0: Signed<ClockTime> = Signed::Positive(ClockTime::ZERO);
849    const P_CT_NONE: Option<Signed<ClockTime>> = None;
850    const P_CT_1: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(1));
851    const P_CT_2: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(2));
852    const P_CT_3: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(3));
853    const N_CT_1: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(1));
854    const N_CT_2: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(2));
855    const N_CT_3: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(3));
856
857    #[test]
858    fn opt_time_clock() {
859        assert_eq!(CT_1.into_glib(), 1);
860        assert_eq!(Some(CT_1).into_glib(), 1);
861        assert_eq!(ClockTime::NONE.into_glib(), ffi::GST_CLOCK_TIME_NONE);
862
863        let ct_1_from: ClockTime = unsafe { try_from_glib(1u64) }.unwrap();
864        assert_eq!(ct_1_from, CT_1);
865
866        let opt_ct_some: Option<ClockTime> = unsafe { from_glib(1u64) };
867        assert_eq!(opt_ct_some, Some(CT_1));
868
869        let ct_none: Option<ClockTime> = unsafe { from_glib(ffi::GST_CLOCK_TIME_NONE) };
870        assert_eq!(ct_none, None);
871    }
872
873    #[test]
874    #[allow(clippy::eq_op, clippy::op_ref)]
875    fn ops() {
876        assert_eq!(CT_10 + CT_20, CT_30);
877        assert_eq!(CT_30 - CT_20, CT_10);
878        assert_eq!(CT_30 - CT_30, ClockTime::ZERO);
879        assert_eq!(CT_10 * 3, CT_30);
880        assert_eq!(3 * CT_10, CT_30);
881        assert_eq!(CT_20 / 2, CT_10);
882        assert_eq!(CT_20 / CT_2, 10);
883        assert_eq!(CT_30.nseconds(), 30);
884
885        assert_eq!(P_CT_1 + P_CT_2, P_CT_3);
886        assert_eq!(P_CT_3 + N_CT_2, P_CT_1);
887        assert_eq!(P_CT_2 + N_CT_3, N_CT_1);
888        assert_eq!(N_CT_3 + P_CT_1, N_CT_2);
889        assert_eq!(N_CT_2 + P_CT_3, P_CT_1);
890        assert_eq!(N_CT_2 + N_CT_1, N_CT_3);
891
892        assert_eq!(CT_1 + P_CT_2, P_CT_3);
893        assert_eq!(P_CT_1 + CT_2, P_CT_3);
894        assert_eq!(CT_3 + N_CT_1, P_CT_2);
895        assert_eq!(N_CT_1 + CT_2, P_CT_1);
896
897        assert_eq!(P_CT_3 - P_CT_2, P_CT_1);
898        assert_eq!(P_CT_2 - P_CT_3, N_CT_1);
899        assert_eq!(P_CT_2 - N_CT_1, P_CT_3);
900        assert_eq!(N_CT_2 - P_CT_1, N_CT_3);
901        assert_eq!(N_CT_3 - N_CT_1, N_CT_2);
902
903        assert_eq!(CT_3 - P_CT_2, P_CT_1);
904        assert_eq!(P_CT_3 - CT_2, P_CT_1);
905        assert_eq!(N_CT_2 - CT_1, N_CT_3);
906        assert_eq!(CT_2 - N_CT_1, P_CT_3);
907
908        assert_eq!(P_CT_1 * 2i64, P_CT_2);
909        assert_eq!(P_CT_1 * -2i64, N_CT_2);
910        assert_eq!(N_CT_1 * 2i64, N_CT_2);
911        assert_eq!(N_CT_1 * -2i64, P_CT_2);
912
913        assert_eq!(2i64 * P_CT_1, P_CT_2);
914        assert_eq!(-2i64 * P_CT_1, N_CT_2);
915
916        assert_eq!(P_CT_1 * 2u64, P_CT_2);
917        assert_eq!(N_CT_1 * 2u64, N_CT_2);
918
919        assert_eq!(P_CT_2 / 2i64, P_CT_1);
920        assert_eq!(P_CT_2 / -2i64, N_CT_1);
921        assert_eq!(N_CT_2 / 2i64, N_CT_1);
922        assert_eq!(N_CT_2 / -2i64, P_CT_1);
923
924        assert_eq!(P_CT_2 / N_CT_2, Signed::Negative(1));
925
926        assert_eq!(P_CT_2 / 2u64, P_CT_1);
927        assert_eq!(N_CT_2 / 2u64, N_CT_1);
928
929        assert_eq!(P_CT_3 % 2i64, P_CT_1);
930        assert_eq!(P_CT_3 % -2i64, P_CT_1);
931        assert_eq!(N_CT_3 % 2i64, N_CT_1);
932        assert_eq!(N_CT_3 % -2i64, N_CT_1);
933
934        assert_eq!(N_CT_3 % N_CT_2, N_CT_1);
935
936        assert_eq!(P_CT_3 % 2u64, P_CT_1);
937        assert_eq!(N_CT_3 % 2u64, N_CT_1);
938    }
939
940    #[test]
941    fn checked_ops() {
942        assert_eq!(CT_1.checked_add(CT_1), Some(CT_2));
943        assert_eq!(P_CT_1.checked_add(P_CT_2), Some(P_CT_3));
944        assert_eq!(P_CT_3.checked_add(N_CT_2), Some(P_CT_1));
945        assert_eq!(P_CT_2.checked_add(N_CT_3), Some(N_CT_1));
946        assert_eq!(N_CT_3.checked_add(P_CT_1), Some(N_CT_2));
947        assert_eq!(N_CT_2.checked_add(P_CT_3), Some(P_CT_1));
948        assert_eq!(N_CT_2.checked_add(N_CT_1), Some(N_CT_3));
949
950        assert_eq!(CT_1.opt_checked_add(CT_1), Ok(Some(CT_2)));
951        assert_eq!(CT_1.opt_checked_add(Some(CT_1)), Ok(Some(CT_2)));
952        assert_eq!(Some(CT_1).opt_checked_add(Some(CT_1)), Ok(Some(CT_2)));
953        assert_eq!(CT_1.opt_checked_add(ClockTime::NONE), Ok(None));
954        assert_eq!(Some(CT_1).opt_checked_add(ClockTime::NONE), Ok(None));
955
956        assert_eq!(CT_1.opt_checked_add(P_CT_1), Ok(Some(P_CT_2)));
957        assert_eq!(N_CT_3.opt_checked_add(CT_1), Ok(Some(N_CT_2)));
958
959        assert!(ClockTime::MAX.checked_add(CT_1).is_none());
960        assert_eq!(
961            ClockTime::MAX.opt_checked_add(Some(CT_1)),
962            Err(opt_ops::Error::Overflow)
963        );
964
965        assert_eq!(P_CT_1.opt_checked_add(P_CT_1), Ok(Some(P_CT_2)));
966        assert_eq!(P_CT_1.opt_checked_add(Some(N_CT_2)), Ok(Some(N_CT_1)));
967        assert_eq!(Some(P_CT_1).opt_checked_add(Some(P_CT_1)), Ok(Some(P_CT_2)));
968        assert_eq!(P_CT_1.opt_checked_add(ClockTime::NONE), Ok(None));
969        assert_eq!(Some(N_CT_1).opt_checked_add(ClockTime::NONE), Ok(None));
970
971        assert_eq!(
972            ClockTime::MAX.into_positive().opt_checked_add(Some(P_CT_1)),
973            Err(opt_ops::Error::Overflow)
974        );
975
976        assert_eq!(CT_2.checked_sub(CT_1), Some(CT_1));
977        assert_eq!(P_CT_3.checked_sub(P_CT_2), Some(P_CT_1));
978        assert_eq!(P_CT_2.checked_sub(P_CT_3), Some(N_CT_1));
979        assert_eq!(P_CT_2.checked_sub(N_CT_1), Some(P_CT_3));
980        assert_eq!(N_CT_2.checked_sub(P_CT_1), Some(N_CT_3));
981        assert_eq!(N_CT_3.checked_sub(N_CT_1), Some(N_CT_2));
982        assert_eq!(N_CT_2.checked_sub(N_CT_3), Some(P_CT_1));
983
984        assert_eq!(CT_2.opt_checked_sub(CT_1), Ok(Some(CT_1)));
985        assert_eq!(CT_2.opt_checked_sub(Some(CT_1)), Ok(Some(CT_1)));
986        assert_eq!(Some(CT_2).opt_checked_sub(CT_1), Ok(Some(CT_1)));
987        assert_eq!(Some(CT_2).opt_checked_sub(Some(CT_1)), Ok(Some(CT_1)));
988        assert_eq!(CT_2.opt_checked_sub(ClockTime::NONE), Ok(None));
989        assert_eq!(Some(CT_2).opt_checked_sub(ClockTime::NONE), Ok(None));
990
991        assert_eq!(P_CT_2.opt_checked_sub(CT_1), Ok(Some(P_CT_1)));
992        assert_eq!(N_CT_2.opt_checked_sub(CT_1), Ok(Some(N_CT_3)));
993
994        assert!(CT_1.checked_sub(CT_2).is_none());
995        assert_eq!(
996            Some(CT_1).opt_checked_sub(CT_2),
997            Err(opt_ops::Error::Overflow)
998        );
999
1000        assert_eq!(P_CT_2.opt_checked_sub(Some(N_CT_1)), Ok(Some(P_CT_3)));
1001        assert_eq!(Some(N_CT_2).opt_checked_sub(P_CT_1), Ok(Some(N_CT_3)));
1002
1003        assert_eq!(CT_1.checked_mul(2), Some(CT_2));
1004        assert_eq!(Some(CT_1).opt_checked_mul(2), Ok(Some(CT_2)));
1005        assert_eq!(1u64.opt_checked_mul(Some(CT_2)), Ok(Some(CT_2)));
1006        assert_eq!(P_CT_1.checked_mul(2), Some(P_CT_2));
1007        assert_eq!(P_CT_1.checked_mul(-2), Some(N_CT_2));
1008        assert_eq!(N_CT_1.checked_mul(2), Some(N_CT_2));
1009        assert_eq!(N_CT_1.checked_mul(-2), Some(P_CT_2));
1010
1011        assert_eq!(Some(P_CT_1).opt_checked_mul(-2i64), Ok(Some(N_CT_2)));
1012        assert_eq!(N_CT_1.opt_checked_mul(2u64), Ok(Some(N_CT_2)));
1013
1014        assert_eq!((-2i64).opt_checked_mul(Some(P_CT_1)), Ok(Some(N_CT_2)));
1015
1016        assert_eq!(P_CT_1.checked_mul_unsigned(2u64), Some(P_CT_2));
1017        assert_eq!(N_CT_1.checked_mul_unsigned(2u64), Some(N_CT_2));
1018
1019        assert_eq!(CT_3.checked_div(3), Some(CT_1));
1020        assert_eq!(P_CT_3.checked_div(3), Some(P_CT_1));
1021        assert_eq!(P_CT_3.checked_div(-3), Some(N_CT_1));
1022        assert_eq!(N_CT_3.checked_div(3), Some(N_CT_1));
1023        assert_eq!(N_CT_3.checked_div(-3), Some(P_CT_1));
1024
1025        assert_eq!(Some(CT_3).opt_checked_div(CT_3), Ok(Some(1)));
1026
1027        assert_eq!(Some(P_CT_3).opt_checked_div(-3i64), Ok(Some(N_CT_1)));
1028        assert_eq!(N_CT_3.opt_checked_div(3u64), Ok(Some(N_CT_1)));
1029
1030        assert_eq!(P_CT_3.checked_div_unsigned(3u64), Some(P_CT_1));
1031        assert_eq!(N_CT_3.checked_div_unsigned(3u64), Some(N_CT_1));
1032    }
1033
1034    #[test]
1035    fn overflowing_ops() {
1036        assert_eq!(CT_1.overflowing_add(CT_2), (CT_3, false));
1037        assert_eq!(CT_1.opt_overflowing_add(Some(CT_2)), Some((CT_3, false)));
1038        assert_eq!(Some(CT_1).opt_overflowing_add(CT_2), Some((CT_3, false)));
1039        assert_eq!(
1040            Some(CT_1).opt_overflowing_add(Some(CT_2)),
1041            Some((CT_3, false))
1042        );
1043
1044        assert_eq!(ClockTime::NONE.opt_overflowing_add(CT_2), None);
1045        assert_eq!(CT_1.opt_overflowing_add(ClockTime::NONE), None);
1046
1047        assert_eq!(
1048            ClockTime::MAX.overflowing_add(CT_1),
1049            (ClockTime::ZERO, true)
1050        );
1051        assert_eq!(
1052            Some(ClockTime::MAX).opt_overflowing_add(Some(CT_1)),
1053            Some((ClockTime::ZERO, true)),
1054        );
1055
1056        assert_eq!(CT_3.overflowing_sub(CT_2), (CT_1, false));
1057        assert_eq!(CT_3.opt_overflowing_sub(Some(CT_2)), Some((CT_1, false)));
1058        assert_eq!(Some(CT_3).opt_overflowing_sub(CT_2), Some((CT_1, false)));
1059        assert_eq!(
1060            Some(CT_3).opt_overflowing_sub(Some(CT_2)),
1061            Some((CT_1, false))
1062        );
1063        assert_eq!(
1064            Some(CT_3).opt_overflowing_sub(&Some(CT_2)),
1065            Some((CT_1, false))
1066        );
1067        assert_eq!(ClockTime::NONE.opt_overflowing_sub(CT_2), None);
1068        assert_eq!(CT_2.opt_overflowing_sub(ClockTime::NONE), None);
1069
1070        assert_eq!(CT_1.overflowing_sub(CT_2), (ClockTime::MAX, true));
1071        assert_eq!(
1072            Some(CT_1).opt_overflowing_sub(CT_2),
1073            Some((ClockTime::MAX, true))
1074        );
1075    }
1076
1077    #[test]
1078    fn saturating_ops() {
1079        let p_ct_max: Signed<ClockTime> = ClockTime::MAX.into_positive();
1080        let n_ct_max: Signed<ClockTime> = ClockTime::MAX.into_negative();
1081
1082        assert_eq!(CT_1.saturating_add(CT_2), CT_3);
1083        assert_eq!(P_CT_1.saturating_add(P_CT_2), P_CT_3);
1084        assert_eq!(P_CT_2.saturating_add(N_CT_3), N_CT_1);
1085        assert_eq!(P_CT_3.saturating_add(N_CT_2), P_CT_1);
1086        assert_eq!(N_CT_3.saturating_add(P_CT_1), N_CT_2);
1087        assert_eq!(N_CT_2.saturating_add(P_CT_3), P_CT_1);
1088        assert_eq!(N_CT_2.saturating_add(N_CT_1), N_CT_3);
1089
1090        assert_eq!(CT_1.opt_saturating_add(Some(CT_2)), Some(CT_3));
1091        assert_eq!(Some(CT_1).opt_saturating_add(Some(CT_2)), Some(CT_3));
1092        assert_eq!(Some(CT_1).opt_saturating_add(ClockTime::NONE), None);
1093
1094        assert_eq!(P_CT_1.opt_saturating_add(Some(CT_2)), Some(P_CT_3));
1095        assert_eq!(Some(CT_1).opt_saturating_add(P_CT_2), Some(P_CT_3));
1096
1097        assert_eq!(ClockTime::MAX.saturating_add(CT_1), ClockTime::MAX);
1098        assert_eq!(
1099            Some(ClockTime::MAX).opt_saturating_add(Some(CT_1)),
1100            Some(ClockTime::MAX)
1101        );
1102        assert_eq!(p_ct_max.saturating_add(P_CT_1), p_ct_max);
1103
1104        assert_eq!(CT_3.saturating_sub(CT_2), CT_1);
1105        assert_eq!(P_CT_3.saturating_sub(P_CT_2), P_CT_1);
1106        assert_eq!(P_CT_2.saturating_sub(P_CT_3), N_CT_1);
1107        assert_eq!(P_CT_2.saturating_sub(N_CT_1), P_CT_3);
1108        assert_eq!(N_CT_2.saturating_sub(P_CT_1), N_CT_3);
1109        assert_eq!(N_CT_3.saturating_sub(N_CT_1), N_CT_2);
1110        assert_eq!(N_CT_2.saturating_sub(N_CT_3), P_CT_1);
1111
1112        assert_eq!(CT_3.opt_saturating_sub(Some(CT_2)), Some(CT_1));
1113        assert_eq!(Some(CT_3).opt_saturating_sub(Some(CT_2)), Some(CT_1));
1114        assert_eq!(Some(CT_3).opt_saturating_sub(ClockTime::NONE), None);
1115
1116        assert_eq!(P_CT_2.opt_saturating_sub(Some(CT_3)), Some(N_CT_1));
1117        assert_eq!(Some(CT_3).opt_saturating_sub(P_CT_2), Some(P_CT_1));
1118
1119        assert!(CT_1.saturating_sub(CT_2).is_zero());
1120        assert_eq!(P_CT_1.saturating_sub(P_CT_2), N_CT_1);
1121        assert_eq!(
1122            Some(CT_1).opt_saturating_sub(Some(CT_2)),
1123            Some(ClockTime::ZERO)
1124        );
1125
1126        assert_eq!(CT_1.saturating_mul(2), CT_2);
1127        assert_eq!(ClockTime::MAX.saturating_mul(2), ClockTime::MAX);
1128
1129        assert_eq!(P_CT_1.saturating_mul(2), P_CT_2);
1130        assert_eq!(P_CT_1.saturating_mul(-2), N_CT_2);
1131        assert_eq!(N_CT_1.saturating_mul(2), N_CT_2);
1132        assert_eq!(N_CT_1.saturating_mul(-2), P_CT_2);
1133
1134        assert_eq!(Some(N_CT_1).opt_saturating_mul(-2i64), Some(P_CT_2));
1135        assert_eq!((-2i64).opt_saturating_mul(Some(N_CT_1)), Some(P_CT_2));
1136
1137        assert_eq!(P_CT_1.saturating_mul_unsigned(2u64), P_CT_2);
1138        assert_eq!(N_CT_1.saturating_mul_unsigned(2u64), N_CT_2);
1139
1140        assert_eq!(p_ct_max.saturating_mul(2), p_ct_max);
1141        assert_eq!(n_ct_max.saturating_mul(2), n_ct_max);
1142
1143        assert_eq!(Some(2i64).opt_saturating_mul(p_ct_max), Some(p_ct_max));
1144        assert_eq!(2u64.opt_saturating_mul(Some(n_ct_max)), Some(n_ct_max));
1145
1146        assert_eq!(p_ct_max.saturating_mul_unsigned(2u64), p_ct_max);
1147        assert_eq!(n_ct_max.saturating_mul_unsigned(2u64), n_ct_max);
1148    }
1149
1150    #[test]
1151    fn wrapping_ops() {
1152        assert_eq!(CT_1.wrapping_add(CT_2), CT_3);
1153        assert_eq!(CT_1.opt_wrapping_add(CT_2), Some(CT_3));
1154        assert_eq!(Some(CT_1).opt_wrapping_add(CT_2), Some(CT_3));
1155        assert_eq!(Some(CT_1).opt_wrapping_add(Some(CT_2)), Some(CT_3));
1156        assert_eq!(Some(CT_1).opt_wrapping_add(None), None);
1157
1158        assert_eq!(ClockTime::MAX.wrapping_add(CT_1), ClockTime::ZERO);
1159        assert_eq!(
1160            Some(ClockTime::MAX).opt_wrapping_add(Some(CT_1)),
1161            Some(ClockTime::ZERO)
1162        );
1163
1164        assert_eq!(CT_3.wrapping_sub(CT_2), CT_1);
1165        assert_eq!(CT_3.opt_wrapping_sub(CT_2), Some(CT_1));
1166        assert_eq!(Some(CT_3).opt_wrapping_sub(CT_2), Some(CT_1));
1167        assert_eq!(Some(CT_3).opt_wrapping_sub(Some(CT_2)), Some(CT_1));
1168        assert_eq!(Some(CT_3).opt_wrapping_sub(ClockTime::NONE), None);
1169
1170        assert_eq!(CT_1.wrapping_sub(CT_2), ClockTime::MAX);
1171        assert_eq!(
1172            Some(CT_1).opt_wrapping_sub(Some(CT_2)),
1173            Some(ClockTime::MAX)
1174        );
1175    }
1176
1177    #[test]
1178    fn mul_div_ops() {
1179        use muldiv::MulDiv;
1180
1181        assert_eq!(CT_1.mul_div_floor(7, 3), Some(CT_2));
1182
1183        assert_eq!(P_CT_1.mul_div_floor(7u64, 3), Some(P_CT_2));
1184        assert_eq!(P_CT_1.mul_div_floor(-7i64, 3), Some(N_CT_2));
1185        assert_eq!(P_CT_1.mul_div_floor(7i64, -3), Some(N_CT_2));
1186        assert_eq!(P_CT_1.mul_div_floor(-7i64, -3), Some(P_CT_2));
1187
1188        assert_eq!(N_CT_1.mul_div_floor(7u64, 3), Some(N_CT_2));
1189        assert_eq!(N_CT_1.mul_div_floor(-7i64, 3), Some(P_CT_2));
1190        assert_eq!(N_CT_1.mul_div_floor(7i64, -3), Some(P_CT_2));
1191        assert_eq!(N_CT_1.mul_div_floor(-7i64, -3), Some(N_CT_2));
1192
1193        assert_eq!(CT_1.mul_div_round(10, 3), Some(CT_3));
1194        assert_eq!(CT_1.mul_div_round(8, 3), Some(CT_3));
1195
1196        assert_eq!(P_CT_1.mul_div_round(10u64, 3), Some(P_CT_3));
1197        assert_eq!(P_CT_1.mul_div_round(8u64, 3), Some(P_CT_3));
1198        assert_eq!(P_CT_1.mul_div_round(-10i64, 3), Some(N_CT_3));
1199        assert_eq!(P_CT_1.mul_div_round(-8i64, 3), Some(N_CT_3));
1200        assert_eq!(P_CT_1.mul_div_round(10i64, -3), Some(N_CT_3));
1201        assert_eq!(P_CT_1.mul_div_round(-10i64, -3), Some(P_CT_3));
1202
1203        assert_eq!(N_CT_1.mul_div_round(10u64, 3), Some(N_CT_3));
1204        assert_eq!(N_CT_1.mul_div_round(-10i64, 3), Some(P_CT_3));
1205        assert_eq!(N_CT_1.mul_div_round(10i64, -3), Some(P_CT_3));
1206        assert_eq!(N_CT_1.mul_div_round(-10i64, -3), Some(N_CT_3));
1207
1208        assert_eq!(CT_1.mul_div_ceil(7, 3), Some(CT_3));
1209
1210        assert_eq!(P_CT_1.mul_div_ceil(7u64, 3), Some(P_CT_3));
1211        assert_eq!(P_CT_1.mul_div_ceil(-7i64, 3), Some(N_CT_3));
1212        assert_eq!(P_CT_1.mul_div_ceil(7i64, -3), Some(N_CT_3));
1213        assert_eq!(P_CT_1.mul_div_ceil(-7i64, -3), Some(P_CT_3));
1214
1215        assert_eq!(N_CT_1.mul_div_ceil(7u64, 3), Some(N_CT_3));
1216        assert_eq!(N_CT_1.mul_div_ceil(-7i64, 3), Some(P_CT_3));
1217        assert_eq!(N_CT_1.mul_div_ceil(7i64, -3), Some(P_CT_3));
1218        assert_eq!(N_CT_1.mul_div_ceil(-7i64, -3), Some(N_CT_3));
1219    }
1220
1221    #[test]
1222    #[allow(clippy::nonminimal_bool)]
1223    fn comp() {
1224        assert!(ClockTime::ZERO < CT_2);
1225        assert!(Some(ClockTime::ZERO) < Some(CT_2));
1226        assert!(CT_2 < CT_3);
1227        assert!(Some(CT_2) < Some(CT_3));
1228        assert!(ClockTime::ZERO < CT_3);
1229        assert!(Some(ClockTime::ZERO) < Some(CT_3));
1230
1231        assert_eq!(CT_2, CT_2);
1232        assert_ne!(CT_3, CT_2);
1233
1234        assert!(ClockTime::ZERO.into_positive() < P_CT_1);
1235        assert!(ClockTime::ZERO.into_positive() > N_CT_1);
1236        assert!(P_CT_1 < P_CT_2);
1237        assert!(P_CT_1 > N_CT_2);
1238        assert!(N_CT_1 < P_CT_2);
1239        assert!(N_CT_3 < N_CT_2);
1240
1241        assert!(P_CT_1 < CT_2);
1242        assert!(CT_1 < P_CT_2);
1243        assert!(N_CT_2 < CT_1);
1244        assert!(CT_1 > N_CT_2);
1245
1246        assert_eq!(CT_2, P_CT_2);
1247        assert_ne!(N_CT_3, CT_3);
1248
1249        assert_eq!(Some(CT_2).opt_lt(Some(CT_3)), Some(true));
1250        assert_eq!(Some(CT_3).opt_lt(CT_2), Some(false));
1251        assert_eq!(Some(CT_2).opt_le(Some(CT_3)), Some(true));
1252        assert_eq!(Some(CT_3).opt_le(CT_3), Some(true));
1253
1254        assert_eq!(Some(P_CT_2).opt_lt(Some(P_CT_3)), Some(true));
1255        assert_eq!(Some(P_CT_3).opt_lt(P_CT_2), Some(false));
1256        assert_eq!(Some(P_CT_2).opt_le(Some(P_CT_3)), Some(true));
1257        assert_eq!(Some(P_CT_3).opt_le(P_CT_3), Some(true));
1258
1259        assert_eq!(Some(P_CT_0).opt_lt(P_CT_NONE), None);
1260        assert_eq!(P_CT_NONE.opt_lt(P_CT_0), None);
1261
1262        assert_eq!(Some(N_CT_3).opt_lt(Some(N_CT_2)), Some(true));
1263        assert_eq!(Some(N_CT_2).opt_lt(N_CT_3), Some(false));
1264        assert_eq!(Some(N_CT_3).opt_le(Some(N_CT_2)), Some(true));
1265        assert_eq!(Some(N_CT_3).opt_le(N_CT_3), Some(true));
1266
1267        assert_eq!(Some(P_CT_2).opt_lt(N_CT_3), Some(false));
1268        assert_eq!(Some(N_CT_3).opt_lt(Some(P_CT_2)), Some(true));
1269
1270        assert!(CT_3 > CT_2);
1271        assert!(Some(CT_3) > Some(CT_2));
1272        assert!(CT_2 > ClockTime::ZERO);
1273        assert!(Some(CT_2) > Some(ClockTime::ZERO));
1274        assert!(CT_3 > ClockTime::ZERO);
1275        assert!(Some(CT_3) > Some(ClockTime::ZERO));
1276
1277        assert!(!(ClockTime::NONE > None));
1278        // This doesn't work due to the `PartialOrd` impl on `Option<T>`
1279        //assert_eq!(Some(ClockTime::ZERO) > ClockTime::ZERO, false);
1280        assert!(!(Some(ClockTime::ZERO) < ClockTime::NONE));
1281        assert_eq!(Some(CT_3).opt_gt(Some(CT_2)), Some(true));
1282        assert_eq!(Some(CT_3).opt_ge(Some(CT_2)), Some(true));
1283        assert_eq!(Some(CT_3).opt_ge(CT_3), Some(true));
1284
1285        assert_eq!(Some(P_CT_3).opt_gt(Some(P_CT_2)), Some(true));
1286        assert_eq!(Some(P_CT_3).opt_ge(Some(P_CT_2)), Some(true));
1287        assert_eq!(Some(P_CT_3).opt_ge(P_CT_3), Some(true));
1288
1289        assert_eq!(Some(P_CT_0).opt_gt(P_CT_NONE), None);
1290        assert_eq!(P_CT_NONE.opt_gt(P_CT_0), None);
1291
1292        assert_eq!(Some(N_CT_3).opt_gt(Some(N_CT_2)), Some(false));
1293        assert_eq!(Some(N_CT_3).opt_ge(Some(N_CT_2)), Some(false));
1294        assert_eq!(Some(N_CT_3).opt_ge(N_CT_3), Some(true));
1295
1296        assert_eq!(Some(P_CT_2).opt_gt(N_CT_3), Some(true));
1297        assert_eq!(Some(N_CT_3).opt_gt(Some(P_CT_2)), Some(false));
1298
1299        assert!(!(ClockTime::NONE < None));
1300        assert!(!(ClockTime::NONE > None));
1301
1302        // This doesn't work due to the `PartialOrd` impl on `Option<T>`
1303        //assert!(Some(ClockTime::ZERO) > ClockTime::NONE, false);
1304        // Use opt_gt instead.
1305        assert_eq!(Some(ClockTime::ZERO).opt_gt(ClockTime::NONE), None);
1306        assert_eq!(ClockTime::ZERO.opt_gt(ClockTime::NONE), None);
1307        assert_eq!(ClockTime::ZERO.opt_ge(ClockTime::NONE), None);
1308        assert_eq!(ClockTime::NONE.opt_gt(Some(ClockTime::ZERO)), None);
1309        assert_eq!(ClockTime::NONE.opt_gt(ClockTime::ZERO), None);
1310        assert_eq!(ClockTime::NONE.opt_ge(ClockTime::ZERO), None);
1311
1312        assert!(!(Some(ClockTime::ZERO) < ClockTime::NONE));
1313        assert_eq!(Some(ClockTime::ZERO).opt_lt(ClockTime::NONE), None);
1314        assert_eq!(Some(ClockTime::ZERO).opt_le(ClockTime::NONE), None);
1315
1316        assert_eq!(CT_3.opt_min(CT_2), Some(CT_2));
1317        assert_eq!(CT_3.opt_min(Some(CT_2)), Some(CT_2));
1318        assert_eq!(Some(CT_3).opt_min(Some(CT_2)), Some(CT_2));
1319        assert_eq!(ClockTime::NONE.opt_min(Some(CT_2)), None);
1320        assert_eq!(Some(CT_3).opt_min(ClockTime::NONE), None);
1321
1322        assert_eq!(P_CT_3.opt_min(P_CT_2), Some(P_CT_2));
1323        assert_eq!(P_CT_2.opt_min(P_CT_3), Some(P_CT_2));
1324        assert_eq!(N_CT_3.opt_min(N_CT_2), Some(N_CT_3));
1325        assert_eq!(N_CT_2.opt_min(N_CT_3), Some(N_CT_3));
1326        assert_eq!(P_CT_2.opt_min(N_CT_3), Some(N_CT_3));
1327
1328        assert_eq!(CT_3.opt_max(CT_2), Some(CT_3));
1329        assert_eq!(CT_3.opt_max(Some(CT_2)), Some(CT_3));
1330        assert_eq!(Some(CT_3).opt_max(Some(CT_2)), Some(CT_3));
1331        assert_eq!(ClockTime::NONE.opt_max(Some(CT_2)), None);
1332        assert_eq!(Some(CT_3).opt_max(ClockTime::NONE), None);
1333
1334        assert_eq!(P_CT_3.opt_max(P_CT_2), Some(P_CT_3));
1335        assert_eq!(P_CT_2.opt_max(P_CT_3), Some(P_CT_3));
1336        assert_eq!(N_CT_3.opt_max(N_CT_2), Some(N_CT_2));
1337        assert_eq!(N_CT_2.opt_max(N_CT_3), Some(N_CT_2));
1338        assert_eq!(P_CT_2.opt_max(N_CT_3), Some(P_CT_2));
1339    }
1340
1341    #[test]
1342    fn display() {
1343        let none = Option::<ClockTime>::None;
1344        let some = Some(45_834_908_569_837 * ClockTime::NSECOND);
1345        let lots = ClockTime::from_nseconds(u64::MAX - 1);
1346
1347        // Simple
1348
1349        assert_eq!(format!("{:.0}", DisplayableOptClockTime(none)), "--:--:--");
1350        assert_eq!(
1351            format!("{:.3}", DisplayableOptClockTime(none)),
1352            "--:--:--.---"
1353        );
1354        assert_eq!(
1355            format!("{}", DisplayableOptClockTime(none)),
1356            "--:--:--.---------"
1357        );
1358
1359        assert_eq!(format!("{:.0}", DisplayableOptClockTime(some)), "12:43:54");
1360        assert_eq!(
1361            format!("{:.3}", DisplayableOptClockTime(some)),
1362            "12:43:54.908"
1363        );
1364        assert_eq!(
1365            format!("{}", DisplayableOptClockTime(some)),
1366            "12:43:54.908569837"
1367        );
1368
1369        assert_eq!(format!("{lots:.0}"), "5124095:34:33");
1370        assert_eq!(format!("{lots:.3}"), "5124095:34:33.709");
1371        assert_eq!(format!("{lots}"), "5124095:34:33.709551614");
1372
1373        // Precision caps at 9
1374        assert_eq!(
1375            format!("{:.10}", DisplayableOptClockTime(none)),
1376            "--:--:--.---------"
1377        );
1378        assert_eq!(
1379            format!("{:.10}", DisplayableOptClockTime(some)),
1380            "12:43:54.908569837"
1381        );
1382        assert_eq!(format!("{lots:.10}"), "5124095:34:33.709551614");
1383
1384        // Short width
1385
1386        assert_eq!(format!("{:4.0}", DisplayableOptClockTime(none)), "--:--:--");
1387        assert_eq!(
1388            format!("{:4.3}", DisplayableOptClockTime(none)),
1389            "--:--:--.---"
1390        );
1391        assert_eq!(
1392            format!("{:4}", DisplayableOptClockTime(none)),
1393            "--:--:--.---------"
1394        );
1395
1396        assert_eq!(format!("{:4.0}", DisplayableOptClockTime(some)), "12:43:54");
1397        assert_eq!(
1398            format!("{:4.3}", DisplayableOptClockTime(some)),
1399            "12:43:54.908"
1400        );
1401        assert_eq!(
1402            format!("{:4}", DisplayableOptClockTime(some)),
1403            "12:43:54.908569837"
1404        );
1405
1406        assert_eq!(format!("{lots:4.0}"), "5124095:34:33");
1407        assert_eq!(format!("{lots:4.3}"), "5124095:34:33.709");
1408        assert_eq!(format!("{lots:4}"), "5124095:34:33.709551614");
1409
1410        // Simple padding
1411
1412        assert_eq!(
1413            format!("{:>9.0}", DisplayableOptClockTime(none)),
1414            " --:--:--"
1415        );
1416        assert_eq!(
1417            format!("{:<9.0}", DisplayableOptClockTime(none)),
1418            "--:--:-- "
1419        );
1420        assert_eq!(
1421            format!("{:^10.0}", DisplayableOptClockTime(none)),
1422            " --:--:-- "
1423        );
1424        assert_eq!(
1425            format!("{:>13.3}", DisplayableOptClockTime(none)),
1426            " --:--:--.---"
1427        );
1428        assert_eq!(
1429            format!("{:<13.3}", DisplayableOptClockTime(none)),
1430            "--:--:--.--- "
1431        );
1432        assert_eq!(
1433            format!("{:^14.3}", DisplayableOptClockTime(none)),
1434            " --:--:--.--- "
1435        );
1436        assert_eq!(
1437            format!("{:>19}", DisplayableOptClockTime(none)),
1438            " --:--:--.---------"
1439        );
1440        assert_eq!(
1441            format!("{:<19}", DisplayableOptClockTime(none)),
1442            "--:--:--.--------- "
1443        );
1444        assert_eq!(
1445            format!("{:^20}", DisplayableOptClockTime(none)),
1446            " --:--:--.--------- "
1447        );
1448
1449        assert_eq!(
1450            format!("{:>9.0}", DisplayableOptClockTime(some)),
1451            " 12:43:54"
1452        );
1453        assert_eq!(
1454            format!("{:<9.0}", DisplayableOptClockTime(some)),
1455            "12:43:54 "
1456        );
1457        assert_eq!(
1458            format!("{:^10.0}", DisplayableOptClockTime(some)),
1459            " 12:43:54 "
1460        );
1461        assert_eq!(
1462            format!("{:>13.3}", DisplayableOptClockTime(some)),
1463            " 12:43:54.908"
1464        );
1465        assert_eq!(
1466            format!("{:<13.3}", DisplayableOptClockTime(some)),
1467            "12:43:54.908 "
1468        );
1469        assert_eq!(
1470            format!("{:^14.3}", DisplayableOptClockTime(some)),
1471            " 12:43:54.908 "
1472        );
1473        assert_eq!(
1474            format!("{:>19}", DisplayableOptClockTime(some)),
1475            " 12:43:54.908569837"
1476        );
1477        assert_eq!(
1478            format!("{:<19}", DisplayableOptClockTime(some)),
1479            "12:43:54.908569837 "
1480        );
1481        assert_eq!(
1482            format!("{:^20}", DisplayableOptClockTime(some)),
1483            " 12:43:54.908569837 "
1484        );
1485
1486        assert_eq!(format!("{lots:>14.0}"), " 5124095:34:33");
1487        assert_eq!(format!("{lots:<14.0}"), "5124095:34:33 ");
1488        assert_eq!(format!("{lots:^15.0}"), " 5124095:34:33 ");
1489        assert_eq!(format!("{lots:>18.3}"), " 5124095:34:33.709");
1490        assert_eq!(format!("{lots:<18.3}"), "5124095:34:33.709 ");
1491        assert_eq!(format!("{lots:^19.3}"), " 5124095:34:33.709 ");
1492        assert_eq!(format!("{lots:>24}"), " 5124095:34:33.709551614");
1493        assert_eq!(format!("{lots:<24}"), "5124095:34:33.709551614 ");
1494        assert_eq!(format!("{lots:^25}"), " 5124095:34:33.709551614 ");
1495
1496        // Padding with sign or zero-extension
1497
1498        assert_eq!(
1499            format!("{:+11.0}", DisplayableOptClockTime(none)),
1500            "   --:--:--"
1501        );
1502        assert_eq!(
1503            format!("{:011.0}", DisplayableOptClockTime(none)),
1504            "-----:--:--"
1505        );
1506        assert_eq!(
1507            format!("{:+011.0}", DisplayableOptClockTime(none)),
1508            "-----:--:--"
1509        );
1510        assert_eq!(
1511            format!("{:+15.3}", DisplayableOptClockTime(none)),
1512            "   --:--:--.---"
1513        );
1514        assert_eq!(
1515            format!("{:015.3}", DisplayableOptClockTime(none)),
1516            "-----:--:--.---"
1517        );
1518        assert_eq!(
1519            format!("{:+015.3}", DisplayableOptClockTime(none)),
1520            "-----:--:--.---"
1521        );
1522        assert_eq!(
1523            format!("{:+21}", DisplayableOptClockTime(none)),
1524            "   --:--:--.---------"
1525        );
1526        assert_eq!(
1527            format!("{:021}", DisplayableOptClockTime(none)),
1528            "-----:--:--.---------"
1529        );
1530        assert_eq!(
1531            format!("{:+021}", DisplayableOptClockTime(none)),
1532            "-----:--:--.---------"
1533        );
1534
1535        assert_eq!(
1536            format!("{:+11.0}", DisplayableOptClockTime(some)),
1537            "  +12:43:54"
1538        );
1539        assert_eq!(
1540            format!("{:011.0}", DisplayableOptClockTime(some)),
1541            "00012:43:54"
1542        );
1543        assert_eq!(
1544            format!("{:+011.0}", DisplayableOptClockTime(some)),
1545            "+0012:43:54"
1546        );
1547        assert_eq!(
1548            format!("{:+15.3}", DisplayableOptClockTime(some)),
1549            "  +12:43:54.908"
1550        );
1551        assert_eq!(
1552            format!("{:015.3}", DisplayableOptClockTime(some)),
1553            "00012:43:54.908"
1554        );
1555        assert_eq!(
1556            format!("{:+015.3}", DisplayableOptClockTime(some)),
1557            "+0012:43:54.908"
1558        );
1559        assert_eq!(
1560            format!("{:+21}", DisplayableOptClockTime(some)),
1561            "  +12:43:54.908569837"
1562        );
1563        assert_eq!(
1564            format!("{:021}", DisplayableOptClockTime(some)),
1565            "00012:43:54.908569837"
1566        );
1567        assert_eq!(
1568            format!("{:+021}", DisplayableOptClockTime(some)),
1569            "+0012:43:54.908569837"
1570        );
1571
1572        assert_eq!(format!("{lots:+16.0}"), "  +5124095:34:33");
1573        assert_eq!(format!("{lots:016.0}"), "0005124095:34:33");
1574        assert_eq!(format!("{lots:+016.0}"), "+005124095:34:33");
1575        assert_eq!(format!("{lots:+20.3}"), "  +5124095:34:33.709");
1576        assert_eq!(format!("{lots:020.3}"), "0005124095:34:33.709");
1577        assert_eq!(format!("{lots:+020.3}"), "+005124095:34:33.709");
1578        assert_eq!(format!("{lots:+26}"), "  +5124095:34:33.709551614");
1579        assert_eq!(format!("{lots:026}"), "0005124095:34:33.709551614");
1580        assert_eq!(format!("{lots:+026}"), "+005124095:34:33.709551614");
1581    }
1582
1583    #[test]
1584    fn iter_sum() {
1585        let s: ClockTime = vec![ClockTime::from_seconds(1), ClockTime::from_seconds(2)]
1586            .into_iter()
1587            .sum();
1588        assert_eq!(s, ClockTime::from_seconds(3));
1589    }
1590
1591    #[test]
1592    #[should_panic]
1593    fn attempt_to_build_from_clock_time_none() {
1594        let _ = ClockTime::from_nseconds(ffi::GST_CLOCK_TIME_NONE);
1595    }
1596
1597    #[test]
1598    #[should_panic]
1599    fn attempt_to_build_from_u64max() {
1600        let _ = ClockTime::from_nseconds(u64::MAX);
1601    }
1602
1603    #[test]
1604    fn try_into_signed() {
1605        let time = crate::Signed::Positive(ClockTime::from_nseconds(0));
1606        assert_eq!(i64::try_from(time), Ok(0));
1607
1608        let time = crate::Signed::Positive(ClockTime::from_nseconds(123));
1609        assert_eq!(i64::try_from(time), Ok(123));
1610
1611        let time = crate::Signed::Positive(ClockTime::from_nseconds(u64::MAX - 1));
1612        assert!(i64::try_from(time).is_err());
1613
1614        let time = crate::Signed::Positive(ClockTime::from_nseconds(u64::MAX >> 1));
1615        assert_eq!(i64::MAX as i128, (u64::MAX >> 1) as i128);
1616        assert_eq!(i64::try_from(time), Ok(i64::MAX));
1617
1618        let time = crate::Signed::Negative(ClockTime::from_nseconds(0));
1619        assert_eq!(i64::try_from(time), Ok(0));
1620
1621        let time = crate::Signed::Negative(ClockTime::from_nseconds(123));
1622        assert_eq!(i64::try_from(time), Ok(-123));
1623
1624        let time = crate::Signed::Negative(ClockTime::from_nseconds(u64::MAX - 1));
1625        assert!(i64::try_from(time).is_err());
1626
1627        let time = crate::Signed::Negative(ClockTime::from_nseconds(u64::MAX >> 1));
1628        assert_eq!(i64::MIN as i128 + 1, -((u64::MAX >> 1) as i128));
1629        assert_eq!(i64::try_from(time), Ok(i64::MIN + 1));
1630
1631        let time = crate::Signed::Negative(ClockTime::from_nseconds((u64::MAX >> 1) + 1));
1632        assert_eq!(i64::MIN as i128, -(((u64::MAX >> 1) + 1) as i128));
1633        assert_eq!(i64::try_from(time), Ok(i64::MIN));
1634    }
1635
1636    #[test]
1637    fn properties_macro_usage() {
1638        use super::ClockTime;
1639        use glib::{prelude::*, subclass::prelude::*};
1640        use std::cell::Cell;
1641
1642        #[derive(Default, glib::Properties)]
1643        #[properties(wrapper_type = TestObject)]
1644        pub struct TestObjectImp {
1645            #[property(get, set)]
1646            clock_time: Cell<ClockTime>,
1647            #[property(get, set)]
1648            optional_clock_time: Cell<Option<ClockTime>>,
1649        }
1650
1651        #[glib::object_subclass]
1652        impl ObjectSubclass for TestObjectImp {
1653            const NAME: &'static str = "GstTestObject";
1654            type Type = TestObject;
1655        }
1656
1657        impl ObjectImpl for TestObjectImp {
1658            fn properties() -> &'static [glib::ParamSpec] {
1659                Self::derived_properties()
1660            }
1661
1662            fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
1663                self.derived_set_property(id, value, pspec);
1664            }
1665
1666            fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
1667                self.derived_property(id, pspec)
1668            }
1669        }
1670
1671        glib::wrapper! {
1672            pub struct TestObject(ObjectSubclass<TestObjectImp>);
1673        }
1674
1675        let obj: TestObject = glib::Object::new();
1676
1677        assert_eq!(obj.clock_time(), ClockTime::default());
1678        obj.set_clock_time(ClockTime::MAX);
1679        assert_eq!(obj.clock_time(), ClockTime::MAX);
1680
1681        assert_eq!(obj.optional_clock_time(), None);
1682        obj.set_optional_clock_time(ClockTime::MAX);
1683        assert_eq!(obj.optional_clock_time(), Some(ClockTime::MAX));
1684    }
1685
1686    #[test]
1687    fn seconds_float() {
1688        let res = ClockTime::ZERO;
1689        assert_eq!(res.seconds_f32(), 0.0);
1690        assert_eq!(res.seconds_f64(), 0.0);
1691
1692        let res = ClockTime::from_nseconds(2_700_000_000);
1693        assert_eq!(res.seconds_f32(), 2.7);
1694        assert_eq!(res.seconds_f64(), 2.7);
1695
1696        let res = ClockTime::MAX;
1697        assert_eq!(res.seconds_f32(), 18_446_744_073.709_553);
1698        assert_eq!(res.seconds_f64(), 18_446_744_073.709_553);
1699    }
1700
1701    #[test]
1702    fn seconds_float_signed() {
1703        let pos = Signed::Positive(ClockTime::ZERO);
1704        assert_eq!(pos.seconds_f32(), 0.0);
1705        assert_eq!(pos.seconds_f64(), 0.0);
1706        let neg = Signed::Negative(ClockTime::ZERO);
1707        assert_eq!(neg.seconds_f32(), 0.0);
1708        assert_eq!(neg.seconds_f64(), 0.0);
1709
1710        let pos = Signed::Positive(ClockTime::from_nseconds(2_700_000_000));
1711        assert_eq!(pos.seconds_f32(), 2.7);
1712        assert_eq!(pos.seconds_f64(), 2.7);
1713        let neg = Signed::Negative(ClockTime::from_nseconds(2_700_000_000));
1714        assert_eq!(neg.seconds_f32(), -2.7);
1715        assert_eq!(neg.seconds_f64(), -2.7);
1716
1717        let pos = Signed::Positive(ClockTime::MAX);
1718        assert_eq!(pos.seconds_f32(), 18_446_744_073.709_553);
1719        assert_eq!(pos.seconds_f64(), 18_446_744_073.709_553);
1720        let neg = Signed::Negative(ClockTime::MAX);
1721        assert_eq!(neg.seconds_f32(), -18_446_744_073.709_553);
1722        assert_eq!(neg.seconds_f64(), -18_446_744_073.709_553);
1723    }
1724
1725    #[test]
1726    fn try_from_seconds_f32() {
1727        let res = ClockTime::try_from_seconds_f32(0.0);
1728        assert_eq!(res, Ok(ClockTime::ZERO));
1729        let res = ClockTime::try_from_seconds_f32(1e-20);
1730        assert_eq!(res, Ok(ClockTime::ZERO));
1731        let res = ClockTime::try_from_seconds_f32(4.2e-7);
1732        assert_eq!(res, Ok(ClockTime::from_nseconds(420)));
1733        let res = ClockTime::try_from_seconds_f32(2.7);
1734        assert_eq!(res, Ok(ClockTime::from_nseconds(2_700_000_048)));
1735        // subnormal float:
1736        let res = ClockTime::try_from_seconds_f32(f32::from_bits(1));
1737        assert_eq!(res, Ok(ClockTime::ZERO));
1738
1739        // the conversion uses rounding with tie resolution to even
1740        let res = ClockTime::try_from_seconds_f32(0.999e-9);
1741        assert_eq!(res, Ok(ClockTime::from_nseconds(1)));
1742
1743        let res = ClockTime::try_from_seconds_f32(-5.0);
1744        assert!(res.is_err());
1745        let res = ClockTime::try_from_seconds_f32(f32::NAN);
1746        assert!(res.is_err());
1747        let res = ClockTime::try_from_seconds_f32(2e19);
1748        assert!(res.is_err());
1749
1750        // this float represents exactly 976562.5e-9
1751        let val = f32::from_bits(0x3A80_0000);
1752        let res = ClockTime::try_from_seconds_f32(val);
1753        assert_eq!(res, Ok(ClockTime::from_nseconds(976_562)));
1754
1755        // this float represents exactly 2929687.5e-9
1756        let val = f32::from_bits(0x3B40_0000);
1757        let res = ClockTime::try_from_seconds_f32(val);
1758        assert_eq!(res, Ok(ClockTime::from_nseconds(2_929_688)));
1759
1760        // this float represents exactly 1.000_976_562_5
1761        let val = f32::from_bits(0x3F802000);
1762        let res = ClockTime::try_from_seconds_f32(val);
1763        assert_eq!(res, Ok(ClockTime::from_nseconds(1_000_976_562)));
1764
1765        // this float represents exactly 1.002_929_687_5
1766        let val = f32::from_bits(0x3F806000);
1767        let res = ClockTime::try_from_seconds_f32(val);
1768        assert_eq!(res, Ok(ClockTime::from_nseconds(1_002_929_688)));
1769    }
1770
1771    #[test]
1772    fn try_from_seconds_f64() {
1773        let res = ClockTime::try_from_seconds_f64(0.0);
1774        assert_eq!(res, Ok(ClockTime::ZERO));
1775        let res = ClockTime::try_from_seconds_f64(1e-20);
1776        assert_eq!(res, Ok(ClockTime::ZERO));
1777        let res = ClockTime::try_from_seconds_f64(4.2e-7);
1778        assert_eq!(res, Ok(ClockTime::from_nseconds(420)));
1779        let res = ClockTime::try_from_seconds_f64(2.7);
1780        assert_eq!(res, Ok(ClockTime::from_nseconds(2_700_000_000)));
1781        // subnormal float:
1782        let res = ClockTime::try_from_seconds_f64(f64::from_bits(1));
1783        assert_eq!(res, Ok(ClockTime::ZERO));
1784
1785        // the conversion uses rounding with tie resolution to even
1786        let res = ClockTime::try_from_seconds_f64(0.999e-9);
1787        assert_eq!(res, Ok(ClockTime::from_nseconds(1)));
1788        let res = ClockTime::try_from_seconds_f64(0.999_999_999_499);
1789        assert_eq!(res, Ok(ClockTime::from_nseconds(999_999_999)));
1790        let res = ClockTime::try_from_seconds_f64(0.999_999_999_501);
1791        assert_eq!(res, Ok(ClockTime::from_seconds(1)));
1792        let res = ClockTime::try_from_seconds_f64(42.999_999_999_499);
1793        assert_eq!(res, Ok(ClockTime::from_nseconds(42_999_999_999)));
1794        let res = ClockTime::try_from_seconds_f64(42.999_999_999_501);
1795        assert_eq!(res, Ok(ClockTime::from_seconds(43)));
1796
1797        let res = ClockTime::try_from_seconds_f64(-5.0);
1798        assert!(res.is_err());
1799        let res = ClockTime::try_from_seconds_f64(f64::NAN);
1800        assert!(res.is_err());
1801        let res = ClockTime::try_from_seconds_f64(2e19);
1802        assert!(res.is_err());
1803
1804        // this float represents exactly 976562.5e-9
1805        let val = f64::from_bits(0x3F50_0000_0000_0000);
1806        let res = ClockTime::try_from_seconds_f64(val);
1807        assert_eq!(res, Ok(ClockTime::from_nseconds(976_562)));
1808
1809        // this float represents exactly 2929687.5e-9
1810        let val = f64::from_bits(0x3F68_0000_0000_0000);
1811        let res = ClockTime::try_from_seconds_f64(val);
1812        assert_eq!(res, Ok(ClockTime::from_nseconds(2_929_688)));
1813
1814        // this float represents exactly 1.000_976_562_5
1815        let val = f64::from_bits(0x3FF0_0400_0000_0000);
1816        let res = ClockTime::try_from_seconds_f64(val);
1817        assert_eq!(res, Ok(ClockTime::from_nseconds(1_000_976_562)));
1818
1819        // this float represents exactly 1.002_929_687_5
1820        let val = f64::from_bits(0x3FF0_0C00_0000_0000);
1821        let res = ClockTime::try_from_seconds_f64(val);
1822        assert_eq!(res, Ok(ClockTime::from_nseconds(1_002_929_688)));
1823    }
1824
1825    #[test]
1826    fn try_from_seconds_f32_signed() {
1827        let pos = Signed::<ClockTime>::from_seconds_f32(5.0);
1828        assert!(pos.is_positive());
1829
1830        let neg = Signed::<ClockTime>::from_seconds_f32(-5.0);
1831        assert!(neg.is_negative());
1832    }
1833
1834    #[test]
1835    fn try_from_seconds_f64_signed() {
1836        let pos = Signed::<ClockTime>::from_seconds_f64(5.0);
1837        assert!(pos.is_positive());
1838
1839        let neg = Signed::<ClockTime>::from_seconds_f64(-5.0);
1840        assert!(neg.is_negative());
1841    }
1842
1843    #[test]
1844    fn absdiff() {
1845        let t1 = ClockTime::from_seconds(10);
1846        let t2 = ClockTime::from_seconds(4);
1847
1848        let d = ClockTime::from_seconds(6);
1849
1850        assert_eq!(t1.absdiff(t2), d);
1851        assert_eq!(t2.absdiff(t1), d);
1852    }
1853}