paiagram/units/
speed.rs

1use super::distance::Distance;
2use super::time::Duration;
3use derive_more::{Add, AddAssign, Sub, SubAssign};
4use std::ops;
5
6#[derive(Debug, Clone, Copy, Add, AddAssign, Sub, SubAssign)]
7pub struct Velocity(pub f32);
8
9impl ops::Mul<f32> for Velocity {
10    type Output = Velocity;
11    fn mul(self, rhs: f32) -> Self::Output {
12        Self(self.0 * rhs)
13    }
14}
15
16impl ops::MulAssign<f32> for Velocity {
17    fn mul_assign(&mut self, rhs: f32) {
18        self.0 *= rhs
19    }
20}
21
22impl ops::Div<f32> for Velocity {
23    type Output = Self;
24    fn div(self, rhs: f32) -> Self::Output {
25        Self(self.0 / rhs)
26    }
27}
28
29impl ops::DivAssign<f32> for Velocity {
30    fn div_assign(&mut self, rhs: f32) {
31        self.0 /= rhs
32    }
33}
34
35impl ops::Mul<Duration> for Velocity {
36    type Output = Distance;
37    fn mul(self, rhs: Duration) -> Self::Output {
38        Distance((self.0 * rhs.0 as f32).round() as i32)
39    }
40}
41
42impl ops::Div<Velocity> for Distance {
43    type Output = Duration;
44    fn div(self, rhs: Velocity) -> Self::Output {
45        if rhs.0 == 0.0 {
46            return Duration(0);
47        }
48        Duration((self.0 as f32 / rhs.0).round() as i32)
49    }
50}
51
52impl ops::Div<Duration> for Distance {
53    type Output = Velocity;
54    fn div(self, rhs: Duration) -> Self::Output {
55        if rhs.0 == 0 {
56            return Velocity(0.0);
57        }
58        Velocity(self.0 as f32 / rhs.0 as f32)
59    }
60}