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