1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! Rate-based types/units

use crate::{
    duration,
    fixed_point::{self, FixedPoint},
    time_int::TimeInt,
    ConversionError, Fraction,
};
use core::{convert::TryFrom, mem::size_of, prelude::v1::*};
use num::{CheckedDiv, CheckedMul};

/// An unsigned, fixed-point rate type
///
/// Each implementation defines an _integer_ type and a [`Fraction`] _scaling factor_.
///
/// # Constructing a rate
///
/// ```rust
/// # use embedded_time::{traits::*, rate::units::*};
/// #
/// assert_eq!(45_u32.Hz(), Hertz(45_u32));
/// ```
pub trait Rate: Copy {
    /// Construct a `Generic` `Rate` from an _named_ `Rate`
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use embedded_time::{Fraction, rate::units::*, rate::{Generic, Rate}};
    /// # use core::convert::{TryFrom, TryInto};
    /// #
    /// assert_eq!(Hertz(2_u64).try_into_generic(Fraction::new(1,2_000)),
    ///     Ok(Generic::new(4_000_u32, Fraction::new(1,2_000))));
    /// ```
    ///
    /// # Errors
    ///
    /// Failure will only occur if the provided value does not fit in the selected destination type.
    ///
    /// ---
    ///
    /// [`ConversionError::Overflow`] : The conversion of the _scaling factor_ causes an overflow.
    ///
    /// ```rust
    /// # use embedded_time::{Fraction, rate::units::*, rate::{Rate, Generic}, ConversionError};
    /// # use core::convert::TryFrom;
    /// #
    /// assert_eq!(Hertz(u32::MAX).try_into_generic::<u32>(Fraction::new(1, 2)),
    ///     Err(ConversionError::Overflow));
    /// ```
    ///
    /// ---
    ///
    /// [`ConversionError::ConversionFailure`] : The integer conversion to that of the destination
    /// type fails.
    ///
    /// ```rust
    /// # use embedded_time::{Fraction, rate::units::*, rate::{Rate, Generic}, ConversionError};
    /// # use core::convert::TryFrom;
    /// #
    /// assert_eq!(Hertz(u32::MAX as u64 + 1).try_into_generic::<u32>(Fraction::new(1, 1)),
    ///     Err(ConversionError::ConversionFailure));
    /// ```
    fn try_into_generic<DestInt: TimeInt>(
        self,
        scaling_factor: Fraction,
    ) -> Result<Generic<DestInt>, ConversionError>
    where
        Self: FixedPoint,
        DestInt: TryFrom<Self::T>,
    {
        Ok(Generic::<DestInt>::new(
            self.into_ticks(scaling_factor)?,
            scaling_factor,
        ))
    }

    /// Attempt to construct the given _rate_ type from the given _duration_ type
    ///
    /// (the rate is equal to the reciprocal of the duration)
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use embedded_time::{duration::units::*, rate::{Rate, units::*}};
    /// #
    /// assert_eq!(
    ///     Kilohertz::<u32>::try_from_duration(Microseconds(2_u32)),
    ///     Ok(Kilohertz(500_u32))
    /// );
    /// ```
    ///
    /// # Errors
    ///
    /// Failure will only occur if the provided value does not fit in the selected destination type.
    ///
    /// ---
    ///
    /// [`ConversionError::Overflow`] : The conversion of the _scaling factor_ causes an overflow.
    ///
    /// ```rust
    /// # use embedded_time::{duration::units::*, rate::units::*, ConversionError, traits::*};
    /// #
    /// assert_eq!(
    ///     Megahertz::<u32>::try_from_duration(u32::MAX.hours()),
    ///     Err(ConversionError::Overflow)
    /// );
    /// ```
    ///
    /// ---
    ///
    /// [`ConversionError::DivByZero`] : The rate is `0`, therefore the reciprocal is undefined.
    ///
    /// ```rust
    /// # use embedded_time::{duration::units::*, rate::units::*, ConversionError, traits::*};
    /// #
    /// assert_eq!(
    ///     Hertz::<u32>::try_from_duration(0_u32.seconds()),
    ///     Err(ConversionError::DivByZero)
    /// );
    /// ```
    fn try_from_duration<Duration: duration::Duration>(
        duration: Duration,
    ) -> Result<Self, ConversionError>
    where
        Duration: FixedPoint,
        Self: FixedPoint,
        Self::T: TryFrom<Duration::T>,
    {
        let conversion_factor = Duration::SCALING_FACTOR
            .checked_mul(&Self::SCALING_FACTOR)?
            .recip();

        if size_of::<Duration::T>() >= size_of::<Self::T>() {
            fixed_point::from_ticks(
                Duration::T::from(*conversion_factor.numerator())
                    .checked_div(
                        &duration
                            .integer()
                            .checked_mul(&Duration::T::from(*conversion_factor.denominator()))
                            .ok_or(ConversionError::Overflow)?,
                    )
                    .ok_or(ConversionError::DivByZero)?,
                Self::SCALING_FACTOR,
            )
        } else {
            fixed_point::from_ticks(
                Self::T::from(*conversion_factor.numerator())
                    .checked_div(
                        &Self::T::try_from(*duration.integer())
                            .ok()
                            .unwrap()
                            .checked_mul(&Self::T::from(*conversion_factor.denominator()))
                            .ok_or(ConversionError::Overflow)?,
                    )
                    .ok_or(ConversionError::DivByZero)?,
                Self::SCALING_FACTOR,
            )
        }
    }

    // TODO: add try_into_duration
}

/// The `Generic` `Rate` type allows arbitrary _scaling factor_s to be used without having to impl
/// FixedPoint.
///
/// The purpose of this type is to allow a simple `Rate` that can be defined at run-time. It does
/// this by replacing the `const` _scaling factor_ with a field.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Generic<T> {
    integer: T,
    scaling_factor: Fraction,
}

impl<T> Generic<T> {
    /// Constructs a new (ram) fixed-point `Generic` `Rate` value
    pub const fn new(integer: T, scaling_factor: Fraction) -> Self {
        Self {
            integer,
            scaling_factor,
        }
    }

    /// Returns the _integer_ value
    pub const fn integer(&self) -> &T {
        &self.integer
    }

    /// Returns the _scaling factor_ [`Fraction`] value
    pub const fn scaling_factor(&self) -> &Fraction {
        &self.scaling_factor
    }
}

impl<T: TimeInt> Rate for Generic<T> {}

/// Rate-type units
pub mod units {
    use super::*;
    use crate::{
        fixed_point::{self, FixedPoint},
        fraction::Fraction,
        time_int::TimeInt,
        ConversionError,
    };
    use core::{
        convert::TryFrom,
        fmt::{self, Formatter},
        ops,
    };

    macro_rules! impl_rate {
        ( $name:ident, ($numer:expr, $denom:expr), $desc:literal ) => {
            #[doc = $desc]
            #[derive(Copy, Clone, Debug, Eq, Ord)]
            pub struct $name<T: TimeInt = u32>(pub T);

            impl<T: TimeInt> $name<T> {
                #[doc(hidden)]
                pub fn new(value: T) -> Self {
                    Self(value)
                }
            }

            impl<T: TimeInt> Rate for $name<T> {}

            impl<T: TimeInt> FixedPoint for $name<T> {
                type T = T;
                const SCALING_FACTOR: Fraction = Fraction::new($numer, $denom);

                fn new(value: Self::T) -> Self {
                    Self(value)
                }

                fn integer(&self) -> &Self::T {
                    &self.0
                }
            }

            impl<T: TimeInt> fmt::Display for $name<T> {
                /// Just forwards the underlying integer to [`core::fmt::Display::fmt()`]
                ///
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert_eq!(format!("{}", Hertz(123_u32)), "123");
                /// ```
                fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                    fmt::Display::fmt(&self.0, f)
                }
            }

            impl<T: TimeInt, Rhs: Rate> ops::Add<Rhs> for $name<T>
            where
                T: TryFrom<Rhs::T>,
                Rhs: FixedPoint,
            {
                type Output = Self;

                /// Returns the sum as the LHS type
                ///
                /// # Examples
                ///
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert_eq!((Hertz(1_u32) + Hertz(1_u32)), Hertz(2_u32));
                /// ```
                ///
                /// # Panics
                ///
                /// The same reason the integer operation would panic. Namely, if the
                /// result overflows the type.
                ///
                /// ```rust,should_panic
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// let _ = Hertz(u32::MAX) + Hertz(1_u32);
                /// ```
                fn add(self, rhs: Rhs) -> Self::Output {
                    <Self as FixedPoint>::add(self, rhs)
                }
            }

            impl<T: TimeInt, Rhs: Rate> ops::Sub<Rhs> for $name<T>
            where
                T: TryFrom<Rhs::T>,
                Rhs: FixedPoint,
            {
                type Output = Self;

                /// Returns the difference as the LHS type
                ///
                /// # Examples
                ///
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert_eq!((Hertz(2u32) - Hertz(1_u32)),
                ///     Hertz(1_u32));
                /// ```
                ///
                /// # Panics
                ///
                /// The same reason the integer operation would panic. Namely, if the
                /// result overflows the type.
                ///
                /// ```rust,should_panic
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// let _ = Hertz(0_u32) - Hertz(1_u32);
                /// ```
                fn sub(self, rhs: Rhs) -> Self::Output {
                    <Self as FixedPoint>::sub(self, rhs)
                }
            }

            impl<T: TimeInt, Rhs: Rate> ops::Rem<Rhs> for $name<T>
            where
                T: TryFrom<Rhs::T>,
                Rhs: FixedPoint,
            {
                type Output = Self;

                /// Returns the remainder as the LHS type
                ///
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert_eq!(Hertz(2_037_u32) % Kilohertz(1_u32), Hertz(37_u32));
                /// ```
                fn rem(self, rhs: Rhs) -> Self::Output {
                    <Self as FixedPoint>::rem(self, rhs)
                }
            }

            impl<T: TimeInt, Rhs: Rate> PartialEq<Rhs> for $name<T>
            where
                T: TryFrom<Rhs::T>,
                Rhs: FixedPoint,
                Rhs::T: TryFrom<T>,
            {
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert_eq!(Hertz(123_000_u32), Kilohertz(123_u32));
                /// assert_ne!(Hertz(123_001_u32), Kilohertz(123_u32));
                /// ```
                fn eq(&self, rhs: &Rhs) -> bool {
                    <Self as FixedPoint>::eq(self, rhs)
                }
            }

            impl<T: TimeInt, Rhs: Rate> PartialOrd<Rhs> for $name<T>
            where
                T: TryFrom<Rhs::T>,
                Rhs: FixedPoint,
                Rhs::T: TryFrom<T>,
            {
                /// ```rust
                /// # use embedded_time::{traits::*, rate::units::*};
                /// #
                /// assert!(Hertz(2_001_u32) > Kilohertz(2_u32));
                /// assert!(Hertz(1_999_u32) < Kilohertz(2_u32));
                /// ```
                fn partial_cmp(&self, rhs: &Rhs) -> Option<core::cmp::Ordering> {
                    <Self as FixedPoint>::partial_cmp(self, rhs)
                }
            }

            impl<SourceInt: TimeInt, DestInt: TimeInt> TryFrom<Generic<SourceInt>>
                for $name<DestInt>
            where
                DestInt: TryFrom<SourceInt>,
            {
                type Error = ConversionError;

                /// Construct a _named_ `Rate` from a `Generic` `Rate`
                ///
                /// # Examples
                ///
                /// ```rust
                /// # use embedded_time::{Fraction, rate::units::*, rate::Generic};
                /// # use core::convert::{TryFrom, TryInto};
                /// #
                /// assert_eq!(
                ///     Hertz::<u64>::try_from(Generic::new(2_000_u32, Fraction::new(1,1_000))),
                ///     Ok(Hertz(2_u64))
                /// );
                ///
                /// assert_eq!(
                ///     Generic::new(2_000_u64, Fraction::new(1,1_000)).try_into(),
                ///     Ok(Hertz(2_u64))
                /// );
                /// ```
                ///
                /// # Errors
                ///
                /// Failure will only occur if the provided value does not fit in the selected
                /// destination type.
                ///
                /// ---
                ///
                /// [`ConversionError::Overflow`] : The conversion of the _scaling factor_ causes an
                /// overflow.
                ///
                /// ```rust
                /// # use embedded_time::{Fraction, rate::units::*, rate::Generic, ConversionError};
                /// # use core::convert::TryFrom;
                /// #
                /// assert_eq!(Hertz::<u32>::try_from(Generic::new(u32::MAX, Fraction::new(10,1))),
                ///     Err(ConversionError::Overflow));
                /// ```
                ///
                /// ---
                ///
                /// [`ConversionError::ConversionFailure`] : The _integer_ conversion to that of the
                /// destination type fails.
                ///
                /// ```rust
                /// # use embedded_time::{Fraction, rate::units::*, rate::Generic, ConversionError};
                /// # use core::convert::TryFrom;
                /// #
                /// assert_eq!(Hertz::<u32>::try_from(Generic::new(u32::MAX as u64 + 1, Fraction::new(1,1))),
                ///     Err(ConversionError::ConversionFailure));
                /// ```
                fn try_from(generic_rate: Generic<SourceInt>) -> Result<Self, Self::Error> {
                    fixed_point::from_ticks(generic_rate.integer, generic_rate.scaling_factor)
                }
            }

            impl<T: TimeInt> From<$name<T>> for Generic<T> {
                // TODO: documentation
                fn from(rate: $name<T>) -> Self {
                    Self::new(*rate.integer(), $name::<T>::SCALING_FACTOR)
                }
            }
        };
    }
    impl_rate![Megahertz, (1_000_000, 1), "Hertz × 1,000,000"];
    impl_rate![Kilohertz, (1_000, 1), "Hertz × 1,000"];
    impl_rate![Hertz, (1, 1), "Hertz"];

    impl_rate![MebibytesPerSecond, (1_048_576, 1), "Bytes/s × 1,048,576"];
    impl_rate![MegabytesPerSecond, (1_000_000, 1), "Bytes/s × 1,000,000"];
    impl_rate![KibibytesPerSecond, (1_024, 1), "Bytes/s × 1,024"];
    impl_rate![KiloBytesPerSecond, (1_000, 1), "Bytes/s × 1,000"];
    impl_rate![BytesPerSecond, (1, 1), "Bytes/s"];

    impl_rate![MebibitsPerSecond, (1_048_576, 1), "Bits/s × 1,048,576"];
    impl_rate![MegabitsPerSecond, (1_000_000, 1), "Bits/s × 1,000,000"];
    impl_rate![KibibitsPerSecond, (1_024, 1), "Bits/s × 1,024"];
    impl_rate![KilobitsPerSecond, (1_000, 1), "Bits/s × 1,000"];
    impl_rate![BitsPerSecond, (1, 1), "Bits/s"];

    impl_rate![Mebibaud, (1_048_576, 1), "Baud × 1,048,576"];
    impl_rate![Megabaud, (1_000_000, 1), "Baud × 1,000,000"];
    impl_rate![Kibibaud, (1_024, 1), "Baud × 1,024"];
    impl_rate![Kilobaud, (1_000, 1), "Baud × 1,000"];
    impl_rate![Baud, (1, 1), "Baud"];

    /// Create time-based values from primitive and core numeric types.
    ///
    /// This trait is anonomously re-exported in [`traits`](crate::traits)
    ///
    /// # Examples
    /// Basic construction of time-based values.
    /// ```rust
    /// # use embedded_time::{traits::*, rate::units::*};
    /// assert_eq!(5_u32.MHz(), Megahertz(5_u32));
    /// assert_eq!(5_u32.kHz(), Kilohertz(5_u32));
    /// assert_eq!(5_u32.Hz(), Hertz(5_u32));
    /// assert_eq!(5_u32.MiBps(), MebibytesPerSecond(5_u32));
    /// assert_eq!(5_u32.MBps(), MegabytesPerSecond(5_u32));
    /// assert_eq!(5_u32.KiBps(), KibibytesPerSecond(5_u32));
    /// assert_eq!(5_u32.kBps(), KiloBytesPerSecond(5_u32));
    /// assert_eq!(5_u32.Bps(), BytesPerSecond(5_u32));
    /// assert_eq!(5_u32.Mibps(), MebibitsPerSecond(5_u32));
    /// assert_eq!(5_u32.Mbps(), MegabitsPerSecond(5_u32));
    /// assert_eq!(5_u32.Kibps(), KibibitsPerSecond(5_u32));
    /// assert_eq!(5_u32.kbps(), KilobitsPerSecond(5_u32));
    /// assert_eq!(5_u32.bps(), BitsPerSecond(5_u32));
    /// assert_eq!(5_u32.MiBd(), Mebibaud(5_u32));
    /// assert_eq!(5_u32.MBd(), Megabaud(5_u32));
    /// assert_eq!(5_u32.KiBd(), Kibibaud(5_u32));
    /// assert_eq!(5_u32.kBd(), Kilobaud(5_u32));
    /// assert_eq!(5_u32.Bd(), Baud(5_u32));
    /// ```
    #[allow(non_snake_case)]
    pub trait Extensions: TimeInt {
        /// megahertz
        fn MHz(self) -> Megahertz<Self> {
            Megahertz::new(self)
        }

        /// kilohertz
        fn kHz(self) -> Kilohertz<Self> {
            Kilohertz::new(self)
        }

        /// hertz
        fn Hz(self) -> Hertz<Self> {
            Hertz::new(self)
        }

        /// mebibytes per second
        fn MiBps(self) -> MebibytesPerSecond<Self> {
            MebibytesPerSecond::new(self)
        }

        /// megabytes per second
        fn MBps(self) -> MegabytesPerSecond<Self> {
            MegabytesPerSecond::new(self)
        }

        /// kibibytes per second
        fn KiBps(self) -> KibibytesPerSecond<Self> {
            KibibytesPerSecond::new(self)
        }

        /// kiloBytes per second
        fn kBps(self) -> KiloBytesPerSecond<Self> {
            KiloBytesPerSecond::new(self)
        }

        /// bytes per second
        fn Bps(self) -> BytesPerSecond<Self> {
            BytesPerSecond::new(self)
        }

        /// mebibits per second
        fn Mibps(self) -> MebibitsPerSecond<Self> {
            MebibitsPerSecond::new(self)
        }

        /// megabits per second
        fn Mbps(self) -> MegabitsPerSecond<Self> {
            MegabitsPerSecond::new(self)
        }

        /// kibibits per second
        fn Kibps(self) -> KibibitsPerSecond<Self> {
            KibibitsPerSecond::new(self)
        }

        /// kilobits per second
        fn kbps(self) -> KilobitsPerSecond<Self> {
            KilobitsPerSecond::new(self)
        }

        /// bits per second
        fn bps(self) -> BitsPerSecond<Self> {
            BitsPerSecond::new(self)
        }

        /// mebibaud
        fn MiBd(self) -> Mebibaud<Self> {
            Mebibaud::new(self)
        }

        /// megabaud
        fn MBd(self) -> Megabaud<Self> {
            Megabaud::new(self)
        }

        /// kibibaud
        fn KiBd(self) -> Kibibaud<Self> {
            Kibibaud::new(self)
        }

        /// kilobaud
        fn kBd(self) -> Kilobaud<Self> {
            Kilobaud::new(self)
        }

        /// baud
        fn Bd(self) -> Baud<Self> {
            Baud::new(self)
        }
    }

    impl Extensions for u32 {}
    impl Extensions for u64 {}
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{duration::units::*, rate::units::*, traits::*};

    #[test]
    fn try_from_generic_ok() {
        assert_eq!(
            Hertz::try_from(Generic::new(246_u32, Fraction::new(1, 2))),
            Ok(Hertz(123_u32))
        );
    }

    #[test]
    fn try_into_generic_ok() {
        assert_eq!(
            Hertz(123_u32).try_into_generic(Fraction::new(1, 2)),
            Ok(Generic::new(246_u32, Fraction::new(1, 2)))
        );
    }

    #[test]
    fn try_into_generic_err() {
        assert_eq!(
            Hertz(u32::MAX).try_into_generic::<u32>(Fraction::new(1, 2)),
            Err(ConversionError::Overflow)
        );
    }

    #[test]
    fn get_generic_count() {
        let generic = Generic::new(246_u32, Fraction::new(1, 2));
        assert_eq!(generic.integer(), &246_u32);
    }

    #[test]
    fn check_for_overflows() {
        let mut time = 1_u32;
        time *= 60;
        assert_eq!(Hertz(time), Hertz(60_u32));
    }

    #[test]
    fn remainder() {
        assert_eq!(Hertz(62_u32) % Hertz(60_u32), Hertz(2_u32));
        assert_eq!(Hertz(2_003_u32) % Kilohertz(1_u32), Hertz(3_u32));
        assert_eq!(Kilohertz(40_u32) % Hertz(100_u32), Kilohertz(0_u32));
    }

    #[test]
    fn convert_from_duration() {
        assert_eq!(
            Hertz::<u32>::try_from_duration(Milliseconds(2_u32)),
            Ok(Hertz(500_u32))
        );

        assert_eq!(
            Kilohertz::<u32>::try_from_duration(Microseconds(2_u32)),
            Ok(Kilohertz(500_u32))
        );
    }

    #[test]
    fn frequency_scaling() {
        assert_eq!(1_u32.Hz(), 1_u32.Hz());
        assert_eq!(1_u32.kHz(), 1_000_u32.Hz());
        assert_eq!(1_u32.MHz(), 1_000_000_u32.Hz());
    }

    #[test]
    fn bytes_per_second_scaling() {
        assert_eq!(1_u32.Bps(), 1_u32.Bps());
        assert_eq!(1_u32.kBps(), 1_000_u32.Bps());
        assert_eq!(1_u32.KiBps(), 1_024_u32.Bps());
        assert_eq!(1_u32.MBps(), 1_000_000_u32.Bps());
        assert_eq!(1_u32.MiBps(), 1_048_576_u32.Bps());
    }

    #[test]
    fn bits_per_second_scaling() {
        assert_eq!(1_u32.bps(), 1_u32.bps());
        assert_eq!(1_u32.kbps(), 1_000_u32.bps());
        assert_eq!(1_u32.Kibps(), 1_024_u32.bps());
        assert_eq!(1_u32.Mbps(), 1_000_000_u32.bps());
        assert_eq!(1_u32.Mibps(), 1_048_576_u32.bps());
    }

    #[test]
    fn baud_scaling() {
        assert_eq!(1_u32.Bd(), 1_u32.Bd());
        assert_eq!(1_u32.kBd(), 1_000_u32.Bd());
        assert_eq!(1_u32.KiBd(), 1_024_u32.Bd());
        assert_eq!(1_u32.MBd(), 1_000_000_u32.Bd());
        assert_eq!(1_u32.MiBd(), 1_048_576_u32.Bd());
    }
}