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
use std::time::Duration;

//TODO: Timestamp & Instant implementations can probably be merged

cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
        // Wasm //

        use js_sys::Date;

        /// Represents a specific moment in time
        #[derive(Debug, Clone)]
        pub struct Instant {
            inner: f64,
        }

        impl Instant {
            /// Creates an Instant from the moment the method is called
            pub fn now() -> Self {
                Instant {
                    inner: Date::now(),
                }
            }

            /// Returns time elapsed since the Instant
            pub fn elapsed(&self) -> Duration {
                let inner_duration = Date::now() - self.inner;
                let seconds: u64 = (inner_duration as u64) / 1000;
                let nanos: u32 = ((inner_duration as u32) % 1000) * 1000000;
                return Duration::new(seconds, nanos);
            }

            /// Returns the duration since a previous Instant
            pub fn duration_since(&self, earlier: &Instant) -> Duration {
                let inner_duration = self.inner - earlier.inner;
                let seconds: u64 = (inner_duration as u64) / 1000;
                let nanos: u32 = ((inner_duration as u32) % 1000) * 1000000;
                return Duration::new(seconds, nanos);
            }

            /// Sets the Instant to the value of another
            pub fn set_to(&mut self, other: &Instant) {
                self.inner = other.inner.clone();
            }
        }
    }
    else {
        // Linux //
        /// Represents a specific moment in time
        #[derive(Debug, Clone)]
        pub struct Instant {
            inner: std::time::Instant,
        }

        impl Instant {
            /// Creates an Instant from the moment the method is called
            pub fn now() -> Self {
                Instant {
                    inner: std::time::Instant::now(),
                }
            }

            /// Returns current time elapsed since the Instant
            pub fn elapsed(&self) -> Duration {
                let inner_duration = self.inner.elapsed();
                return Duration::new(inner_duration.as_secs(), inner_duration.subsec_nanos());
            }

            /// Returns the duration since a previous Instant
            pub fn duration_since(&self, earlier: &Instant) -> Duration {
                let inner_duration = self.inner.saturating_duration_since(earlier.inner);
                return Duration::new(inner_duration.as_secs(), inner_duration.subsec_nanos());
            }

            /// Sets the Instant to the value of another
            pub fn set_to(&mut self, other: &Instant) {
                self.inner = other.inner.clone();
            }

            /// Adds a Duration to the Instant, if allowed
            pub fn add(&mut self, duration: &Duration) {
                if let Some(result) = self.inner.checked_add(*duration) {
                    self.inner = result;
                }
            }

            /// Adds a Duration to the Instant, if allowed
            pub fn sub(&mut self, duration: &Duration) {
                if let Some(result) = self.inner.checked_sub(*duration) {
                    self.inner = result;
                }
            }
        }
    }
}