naia_socket_shared/backends/native/
instant.rs

1use std::time::Duration;
2
3/// Represents a specific moment in time
4#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
5pub struct Instant {
6    inner: std::time::Instant,
7}
8
9impl Instant {
10    /// Creates an Instant from the moment the method is called
11    pub fn now() -> Self {
12        Self {
13            inner: std::time::Instant::now(),
14        }
15    }
16
17    /// Returns time elapsed since the Instant
18    pub fn elapsed(&self, now: &Self) -> Duration {
19        now.inner - self.inner
20    }
21
22    /// Returns time until the Instant occurs
23    pub fn until(&self, now: &Self) -> Duration {
24        self.inner.duration_since(now.inner())
25    }
26
27    pub fn is_after(&self, other: &Self) -> bool {
28        self.inner > other.inner
29    }
30
31    /// Adds a given number of milliseconds to the Instant
32    pub fn add_millis(&mut self, millis: u32) {
33        self.inner += Duration::from_millis(millis.into());
34    }
35
36    /// Subtracts a given number of milliseconds to the Instant
37    pub fn subtract_millis(&mut self, millis: u32) {
38        self.inner -= Duration::from_millis(millis.into());
39    }
40
41    /// Returns inner Instant implementation
42    pub fn inner(&self) -> std::time::Instant {
43        self.inner
44    }
45}