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
use core::ops::Mul;

use super::TimeSpan;

pub const FEMTOS_PER_SEC: u64 = 1_000_000_000_000_000;
pub const FEMTOS_PER_NANO: u64 = 1_000_000;
pub const NANOS_PER_SEC: u64 = 1_000_000_000;

#[derive(Debug)]
pub enum TimeUnitError {
    ConversionOverflow,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct Seconds(pub u64);

impl Mul<Seconds> for u64 {
    type Output = TimeSpan;

    fn mul(self, rhs: Seconds) -> Self::Output {
        TimeSpan::from_secs(self * rhs.0)
    }
}

impl Mul<u64> for Seconds {
    type Output = TimeSpan;

    // apply reflexive property
    fn mul(self, rhs: u64) -> Self::Output {
        rhs * self
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct MilliSeconds(pub u64);

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct MicroSeconds(pub u64);

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct NanoSeconds(pub u64);

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct PicoSeconds(pub u64);

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(transparent)]
pub struct FemtoSeconds(pub u64);

impl Mul<FemtoSeconds> for u64 {
    type Output = TimeSpan;

    fn mul(self, rhs: FemtoSeconds) -> Self::Output {
        let t = self as u128 * rhs.0 as u128;
        TimeSpan::new(
            (t / FEMTOS_PER_SEC as u128) as u64,
            (t % FEMTOS_PER_SEC as u128) as u64,
        )
    }
}

impl Mul<u64> for FemtoSeconds {
    type Output = TimeSpan;

    // apply reflexive property
    fn mul(self, rhs: u64) -> Self::Output {
        rhs * self
    }
}

macro_rules! impl_scalar_mul {
    ($unit: ident, $conver: expr) => {
        impl Mul<$unit> for u64 {
            type Output = TimeSpan;

            fn mul(self, rhs: $unit) -> Self::Output {
                let t = self as u128 * rhs.0 as u128;
                let f: FemtoSeconds = $unit((t % $conver as u128) as u64).try_into().unwrap();
                TimeSpan::new((t / $conver as u128) as u64, f.0)
            }
        }

        impl Mul<u64> for $unit {
            type Output = TimeSpan;

            // apply reflexive property
            fn mul(self, rhs: u64) -> Self::Output {
                rhs * self
            }
        }
    };
}

impl_scalar_mul!(NanoSeconds, NANOS_PER_SEC);

macro_rules! impl_unit_conversion {
    ($big: ident, $small: ident, $conver: expr) => {
        impl From<$small> for $big {
            fn from(unit: $small) -> Self {
                $big(unit.0 / $conver)
            }
        }

        // conversion to a smaller unit might fail (overlfow)
        impl TryFrom<$big> for $small {
            type Error = TimeUnitError;
            fn try_from(unit: $big) -> Result<Self, Self::Error> {
                match unit.0.checked_mul($conver) {
                    Some(t) => Ok($small(t)),
                    None => Err(TimeUnitError::ConversionOverflow),
                }
            }
        }
    };
}

impl_unit_conversion!(Seconds, FemtoSeconds, FEMTOS_PER_SEC);
impl_unit_conversion!(NanoSeconds, FemtoSeconds, FEMTOS_PER_NANO);

#[cfg(test)]
mod tests {

    use crate::syscall::{FemtoSeconds, Seconds, TimeSpan, FEMTOS_PER_SEC};

    #[test]
    fn secs_mult() {
        let scalar: u64 = 100;
        let secs: u64 = 5;

        // lhs is Seconds(), rhs is a scalar
        assert_eq!(Seconds(secs) * scalar, TimeSpan::new(secs * scalar, 0));

        // lhs is a scalar, rhs is Seconds()
        assert_eq!(scalar * Seconds(secs), TimeSpan::new(secs * scalar, 0));
    }

    #[test]
    fn femtos_mult() {
        let scalar: u64 = 1234;
        let femtos: u64 = 500;

        // lhs is FemtoSeconds(), rhs is a scalar
        assert_eq!(
            FemtoSeconds(femtos) * scalar,
            TimeSpan::new(0, femtos * scalar)
        );

        // lhs is a scalar, rhs is FemtoSeconds()
        assert_eq!(
            scalar * FemtoSeconds(femtos),
            TimeSpan::new(0, femtos * scalar)
        );
    }

    #[test]
    fn conversion() {
        let femtos = FemtoSeconds(FEMTOS_PER_SEC * 3);
        let mut secs: Seconds = femtos.into();

        assert_eq!(secs, Seconds(3));

        secs = Seconds(3);
        let f: FemtoSeconds = secs
            .try_into()
            .expect("could not convert Seconds to FemtoSeconds");

        assert_eq!(femtos, f);
    }
}