abi_stable/std_types/
time.rs

1//! Contains ffi-safe equivalent of `std::time::Duration`.
2
3use std::time::Duration;
4
5/// Ffi-safe equivalent of `std::time::Duration` .
6///
7/// # Example
8///
9/// ```
10/// use abi_stable::std_types::RDuration;
11///
12/// let dur = RDuration::from_millis(31416);
13/// assert_eq!(dur.as_secs(), 31);
14/// assert_eq!(dur.as_nanos(), 31_416_000_000);
15///
16/// ```
17#[derive(
18    Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize, StableAbi,
19)]
20#[repr(C)]
21pub struct RDuration {
22    seconds: u64,
23    subsec_nanos: u32,
24}
25
26impl RDuration {
27    /// Constructs this RDuration from seconds and the nanoseconds inside a second .
28    ///
29    /// # Example
30    ///
31    /// ```
32    /// use abi_stable::std_types::RDuration;
33    ///
34    /// let dur = RDuration::new(1, 456_000_000);
35    /// assert_eq!(dur.as_millis(), 1_456);
36    /// assert_eq!(dur.as_micros(), 1_456_000);
37    ///
38    /// ```
39    pub const fn new(seconds: u64, subsec_nanos: u32) -> Self {
40        Self {
41            subsec_nanos,
42            seconds,
43        }
44    }
45
46    /// Creates an RDuration of `secs` seconds.
47    ///
48    /// # Example
49    ///
50    /// ```
51    /// use abi_stable::std_types::RDuration;
52    ///
53    /// let dur = RDuration::from_secs(14);
54    /// assert_eq!(dur.as_millis(), 14_000);
55    /// assert_eq!(dur.as_micros(), 14_000_000);
56    ///
57    /// ```
58    pub const fn from_secs(secs: u64) -> RDuration {
59        RDuration {
60            seconds: secs,
61            subsec_nanos: 0,
62        }
63    }
64
65    /// Creates an RDuration of `milli` milliseconds.
66    ///
67    /// # Example
68    ///
69    /// ```
70    /// use abi_stable::std_types::RDuration;
71    ///
72    /// let dur = RDuration::from_millis(628);
73    /// assert_eq!(dur.as_micros(), 628_000);
74    /// assert_eq!(dur.as_nanos(), 628_000_000);
75    ///
76    /// ```
77    pub const fn from_millis(milli: u64) -> RDuration {
78        RDuration {
79            seconds: milli / 1000,
80            subsec_nanos: (milli % 1000) as u32 * 1_000_000,
81        }
82    }
83
84    /// Creates an RDuration of `micro` microseconds.
85    ///
86    /// # Example
87    ///
88    /// ```
89    /// use abi_stable::std_types::RDuration;
90    ///
91    /// let dur = RDuration::from_micros(1024);
92    /// assert_eq!(dur.as_millis(), 1);
93    /// assert_eq!(dur.as_nanos(), 1024_000);
94    ///
95    /// ```
96    pub const fn from_micros(micro: u64) -> RDuration {
97        let million = 1_000_000;
98        RDuration {
99            seconds: micro / million,
100            subsec_nanos: (micro % million) as u32 * 1000,
101        }
102    }
103
104    /// Creates an RDuration of `nano` nanoseconds.
105    ///
106    /// # Example
107    ///
108    /// ```
109    /// use abi_stable::std_types::RDuration;
110    ///
111    /// let dur = RDuration::from_nanos(128_256_512);
112    /// assert_eq!(dur.as_millis(), 128);
113    /// assert_eq!(dur.as_micros(), 128_256);
114    ///
115    /// ```
116    pub const fn from_nanos(nano: u64) -> RDuration {
117        let billion = 1_000_000_000;
118        RDuration {
119            seconds: nano / billion,
120            subsec_nanos: (nano % billion) as u32,
121        }
122    }
123
124    /// The amount of fractional nanoseconds (total_nanoseconds % 1_000_000_000)
125    /// of this RDuration.
126    ///
127    /// # Example
128    ///
129    /// ```
130    /// use abi_stable::std_types::RDuration;
131    ///
132    /// let dur = RDuration::from_nanos(64_128_256_512);
133    /// assert_eq!(dur.subsec_nanos(), 128_256_512);
134    ///
135    /// ```
136    pub const fn subsec_nanos(&self) -> u32 {
137        self.subsec_nanos
138    }
139
140    /// The amount of seconds of this RDuration.
141    ///
142    /// # Example
143    ///
144    /// ```
145    /// use abi_stable::std_types::RDuration;
146    ///
147    /// let dur = RDuration::from_nanos(64_128_256_512);
148    /// assert_eq!(dur.seconds(), 64);
149    ///
150    /// ```
151    pub const fn seconds(&self) -> u64 {
152        self.seconds
153    }
154
155    /// The amount of seconds of this RDuration.
156    ///
157    /// # Example
158    ///
159    /// ```
160    /// use abi_stable::std_types::RDuration;
161    ///
162    /// let dur = RDuration::from_nanos(64_128_256_512);
163    /// assert_eq!(dur.as_secs(), 64);
164    ///
165    /// ```
166    pub const fn as_secs(&self) -> u64 {
167        self.seconds
168    }
169
170    /// The amount of milliseconds of this RDuration.
171    ///
172    /// # Example
173    ///
174    /// ```
175    /// use abi_stable::std_types::RDuration;
176    ///
177    /// let dur = RDuration::from_nanos(64_128_256_512);
178    /// assert_eq!(dur.as_millis(), 64_128);
179    ///
180    /// ```
181    pub const fn as_millis(&self) -> u128 {
182        self.seconds as u128 * 1_000_u128 + (self.subsec_nanos / 1_000_000) as u128
183    }
184
185    /// The amount of microseconds of this RDuration.
186    ///
187    /// # Example
188    ///
189    /// ```
190    /// use abi_stable::std_types::RDuration;
191    ///
192    /// let dur = RDuration::from_nanos(64_128_256_512);
193    /// assert_eq!(dur.as_micros(), 64_128_256);
194    ///
195    /// ```
196    pub const fn as_micros(&self) -> u128 {
197        self.seconds as u128 * 1_000_000_u128 + (self.subsec_nanos / 1000) as u128
198    }
199
200    /// The amount of nanoseconds of this RDuration.
201    ///
202    /// # Example
203    ///
204    /// ```
205    /// use abi_stable::std_types::RDuration;
206    ///
207    /// let dur = RDuration::from_micros(256);
208    /// assert_eq!(dur.as_nanos(), 256_000);
209    ///
210    /// ```
211    pub const fn as_nanos(&self) -> u128 {
212        self.seconds as u128 * 1_000_000_000_u128 + self.subsec_nanos as u128
213    }
214}
215
216impl_from_rust_repr! {
217    impl From<Duration> for RDuration {
218        fn(v){
219            RDuration {
220                subsec_nanos: v.subsec_nanos(),
221                seconds: v.as_secs(),
222            }
223        }
224    }
225}
226
227impl_into_rust_repr! {
228    impl Into<Duration> for RDuration {
229        fn(this){
230            Duration::new(this.seconds, this.subsec_nanos)
231        }
232    }
233}