euphony_core/time/
duration.rs

1use crate::ratio::Ratio;
2use core::ops::{Div, Mul};
3pub use core::time::Duration;
4
5impl Mul<Ratio<u64>> for Duration {
6    type Output = Duration;
7
8    fn mul(self, ratio: Ratio<u64>) -> Self::Output {
9        let whole_duration = self * ratio.whole() as u32;
10        let (numer, denom) = ratio.fraction().into();
11        let fract_duration = self / (denom as u32) * (numer as u32);
12        whole_duration + fract_duration
13    }
14}
15
16impl Div<Ratio<u64>> for Duration {
17    type Output = Duration;
18
19    fn div(self, ratio: Ratio<u64>) -> Self::Output {
20        let (numer, denom) = ratio.into();
21        if numer == 0 {
22            return Duration::from_secs(0);
23        }
24        self / (numer as u32) * (denom as u32)
25    }
26}
27
28#[test]
29fn div_duration_test() {
30    assert_eq!(
31        Duration::from_secs(1) / Ratio(4, 3),
32        Duration::from_millis(750)
33    );
34}