Skip to main content

autd3_rs_core/common/
velocity.rs

1use super::length::Length;
2
3#[allow(non_camel_case_types)]
4pub struct s;
5
6#[derive(Clone, Copy, PartialEq, PartialOrd)]
7pub struct Velocity {
8    mm_per_s: f32,
9}
10
11impl core::fmt::Debug for Velocity {
12    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
13        write!(f, "{} mm/s", self.mm_per_s)
14    }
15}
16
17impl Velocity {
18    #[must_use]
19    pub const fn from_mm_s(mm_per_s: f32) -> Self {
20        Self { mm_per_s }
21    }
22
23    #[must_use]
24    pub const fn from_m_s(m_per_s: f32) -> Self {
25        Self {
26            mm_per_s: m_per_s * 1000.0,
27        }
28    }
29
30    #[must_use]
31    pub const fn mm_per_s(self) -> f32 {
32        self.mm_per_s
33    }
34
35    #[must_use]
36    pub const fn m_s(self) -> f32 {
37        self.mm_per_s / 1000.0
38    }
39}
40
41impl core::ops::Div<s> for Length {
42    type Output = Velocity;
43    fn div(self, _rhs: s) -> Self::Output {
44        Velocity {
45            mm_per_s: self.mm(),
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::super::length::{m, mm};
53    use super::*;
54
55    #[test]
56    fn from_length_per_s() {
57        approx::assert_abs_diff_eq!((340.0 * m / s).mm_per_s(), 340_000.0);
58        approx::assert_abs_diff_eq!((340 * m / s).mm_per_s(), 340_000.0);
59        approx::assert_abs_diff_eq!((340_000.0 * mm / s).mm_per_s(), 340_000.0);
60    }
61
62    #[test]
63    fn constructors() {
64        approx::assert_abs_diff_eq!(Velocity::from_m_s(340.0).mm_per_s(), 340_000.0);
65        approx::assert_abs_diff_eq!(Velocity::from_mm_s(340_000.0).mm_per_s(), 340_000.0);
66        approx::assert_abs_diff_eq!(Velocity::from_mm_s(340_000.0).m_s(), 340.0);
67    }
68
69    #[test]
70    fn dbg() {
71        assert_eq!(format!("{:?}", 340.0 * m / s), "340000 mm/s");
72    }
73}