1#[cfg(target_os = "fuchsia")]
6mod fuchsia {
7 pub use zx::{
8 BootInstant, BootTicks, Instant, MonotonicInstant, MonotonicTicks, NsUnit, Ticks,
9 TicksUnit, Timeline,
10 };
11}
12#[cfg(target_os = "fuchsia")]
13pub use fuchsia::*;
14
15#[cfg(not(target_os = "fuchsia"))]
16mod host {
17 use std::marker::PhantomData;
20 use zx_types::{zx_ticks_t, zx_time_t};
21
22 pub trait Timeline {}
24
25 #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
27 pub struct NsUnit;
28
29 #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
31 pub struct TicksUnit;
32
33 #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
35 pub struct MonotonicTimeline;
36 impl Timeline for MonotonicTimeline {}
37
38 #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
40 pub struct BootTimeline;
41 impl Timeline for BootTimeline {}
42
43 #[repr(transparent)]
45 #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
46 pub struct Instant<T, U = NsUnit>(i64, PhantomData<(T, U)>);
47
48 pub type MonotonicInstant = Instant<MonotonicTimeline, NsUnit>;
50 pub type BootInstant = Instant<BootTimeline, NsUnit>;
52 pub type MonotonicTicks = Instant<MonotonicTimeline, TicksUnit>;
55 pub type BootTicks = Instant<BootTimeline, TicksUnit>;
57 pub type Ticks<T> = Instant<T, TicksUnit>;
59
60 impl<T, U> Instant<T, U> {
61 pub const ZERO: Instant<T, U> = Instant(0, PhantomData);
63 }
64
65 impl<T: Timeline> Instant<T> {
66 pub const fn from_nanos(nanos: zx_time_t) -> Self {
68 Self(nanos, PhantomData)
69 }
70
71 pub fn into_nanos(self) -> zx_time_t {
73 self.0
74 }
75 }
76
77 impl<T: Timeline> Instant<T, TicksUnit> {
78 pub const fn from_raw(nanos: zx_ticks_t) -> Self {
80 Self(nanos, PhantomData)
81 }
82
83 pub fn into_raw(self) -> zx_ticks_t {
85 self.0
86 }
87 }
88}
89
90#[cfg(not(target_os = "fuchsia"))]
91pub use host::*;