twizzler_rt_abi/
time.rs

1//! Functions for interacting with the runtime's support for time, monotonic and system.
2
3use core::time::Duration;
4
5/// Possible monotonicities supported by the system monotonic clock.
6#[repr(u32)]
7pub enum Monotonicity {
8    NonMonotonic = crate::bindings::monotonicity_NonMonotonic,
9    Weak = crate::bindings::monotonicity_WeakMonotonic,
10    Strict = crate::bindings::monotonicity_StrongMonotonic,
11}
12
13impl Into<crate::bindings::monotonicity> for Monotonicity {
14    fn into(self) -> crate::bindings::monotonicity {
15        self as crate::bindings::monotonicity
16    }
17}
18
19impl From<crate::bindings::monotonicity> for Monotonicity {
20    fn from(value: crate::bindings::monotonicity) -> Self {
21        match value {
22            crate::bindings::monotonicity_WeakMonotonic => Self::Weak,
23            crate::bindings::monotonicity_StrongMonotonic => Self::Strict,
24            _ => Self::NonMonotonic,
25        }
26    }
27}
28
29/// Read the system monotonic clock.
30pub fn twz_rt_get_monotonic_time() -> Duration {
31    unsafe { crate::bindings::twz_rt_get_monotonic_time().into() }
32}
33
34/// Read the system time.
35pub fn twz_rt_get_system_time() -> Duration {
36    unsafe { crate::bindings::twz_rt_get_system_time().into() }
37}
38
39impl From<Duration> for crate::bindings::duration {
40    fn from(value: Duration) -> Self {
41        Self {
42            seconds: value.as_secs(),
43            nanos: value.subsec_nanos(),
44        }
45    }
46}
47
48impl From<crate::bindings::duration> for Duration {
49    fn from(value: crate::bindings::duration) -> Self {
50        Self::new(value.seconds, value.nanos)
51    }
52}
53
54impl From<Option<Duration>> for crate::bindings::option_duration {
55    fn from(value: Option<Duration>) -> Self {
56        match value {
57            Some(dur) => Self {
58                dur: dur.into(),
59                is_some: 1,
60            },
61            None => Self {
62                dur: crate::bindings::duration {
63                    seconds: 0,
64                    nanos: 0,
65                },
66                is_some: 0,
67            },
68        }
69    }
70}