fidl/
time.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#[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    // This module provides a small subset of our Zircon Time bindings for the purposes of
18    // encoding/decoding times in the host without losing the type safety we get on the target.
19    use std::marker::PhantomData;
20    use zx_types::{zx_ticks_t, zx_time_t};
21
22    /// A marker trait for times to prevent accidental comparison between different timelines.
23    pub trait Timeline {}
24
25    /// A marker type representing nanoseconds.
26    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
27    pub struct NsUnit;
28
29    /// A marker type representing ticks.
30    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
31    pub struct TicksUnit;
32
33    /// A marker type for the system's monotonic timeline which pauses during suspend.
34    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
35    pub struct MonotonicTimeline;
36    impl Timeline for MonotonicTimeline {}
37
38    /// A marker type for the system's boot timeline which continues running during suspend.
39    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
40    pub struct BootTimeline;
41    impl Timeline for BootTimeline {}
42
43    /// Time generic over the Timeline.
44    #[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    /// Monotonic timestamp (ns).
49    pub type MonotonicInstant = Instant<MonotonicTimeline, NsUnit>;
50    /// Boot timestamp (ns).
51    pub type BootInstant = Instant<BootTimeline, NsUnit>;
52    /// A timestamp from system ticks on the monotonic timeline. Does not advance while the system is
53    /// suspended.
54    pub type MonotonicTicks = Instant<MonotonicTimeline, TicksUnit>;
55    /// A timestamp from system ticks on the boot timeline. Advances while the system is suspended.
56    pub type BootTicks = Instant<BootTimeline, TicksUnit>;
57    /// A timestamp from system ticks.
58    pub type Ticks<T> = Instant<T, TicksUnit>;
59
60    impl<T, U> Instant<T, U> {
61        /// Zero timestamp.
62        pub const ZERO: Instant<T, U> = Instant(0, PhantomData);
63    }
64
65    impl<T: Timeline> Instant<T> {
66        /// Create a timestamp from nanoseconds.
67        pub const fn from_nanos(nanos: zx_time_t) -> Self {
68            Self(nanos, PhantomData)
69        }
70
71        /// Return the number of nanoseconds associated with this timestamp.
72        pub fn into_nanos(self) -> zx_time_t {
73            self.0
74        }
75    }
76
77    impl<T: Timeline> Instant<T, TicksUnit> {
78        /// Create an instant from ticks.
79        pub const fn from_raw(nanos: zx_ticks_t) -> Self {
80            Self(nanos, PhantomData)
81        }
82
83        /// Return the number of ticks associated with this instant.
84        pub fn into_raw(self) -> zx_ticks_t {
85            self.0
86        }
87    }
88}
89
90#[cfg(not(target_os = "fuchsia"))]
91pub use host::*;