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
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
use serde::{Deserialize, Serialize};
units! {
_TimeTrait(Clone + Copy);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] u32:
Time("", " s");
}
impl Time {
pub fn as_secs(self) -> f64 { self.value() as f64 * 0.01 }
pub fn zero() -> Self { Self(0) }
pub fn int_div(self, other: Self) -> u32 { self.0 / other.0 }
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Instant(pub Time);
impl Instant {
pub fn since_epoch(self) -> Time { self.0 }
}
impl Add<Time> for Instant {
type Output = Self;
fn add(self, other: Time) -> Self { Self(self.0 + other) }
}
impl AddAssign<Time> for Instant {
fn add_assign(&mut self, other: Time) { self.0 += other; }
}
impl Sub<Time> for Instant {
type Output = Self;
fn sub(self, other: Time) -> Self { Self(self.0 - other) }
}
impl Sub<Instant> for Instant {
type Output = Time;
fn sub(self, other: Self) -> Time { self.0 - other.0 }
}
impl SubAssign<Time> for Instant {
fn sub_assign(&mut self, other: Time) { self.0 -= other; }
}
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Rate<T>(pub T);
impl<T: Mul<f64, Output = T>> std::ops::Mul<Time> for Rate<T> {
type Output = T;
fn mul(self, time: Time) -> T { self.0 * (time.value() as f64) }
}