1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! Contains ffi-safe equivalent of `std::time::Duration`.

use std::time::Duration;

/// Ffi-safe equivalent of `std::time::Duration` .
///
/// # Example
///
/// ```
/// use abi_stable::std_types::RDuration;
///
/// let dur = RDuration::from_millis(31416);
/// assert_eq!(dur.as_secs(), 31);
/// assert_eq!(dur.as_nanos(), 31_416_000_000);
///
/// ```
#[derive(
    Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize, StableAbi,
)]
#[repr(C)]
pub struct RDuration {
    seconds: u64,
    subsec_nanos: u32,
}

impl RDuration {
    /// Constructs this RDuration from seconds and the nanoseconds inside a second .
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::new(1, 456_000_000);
    /// assert_eq!(dur.as_millis(), 1_456);
    /// assert_eq!(dur.as_micros(), 1_456_000);
    ///
    /// ```
    pub const fn new(seconds: u64, subsec_nanos: u32) -> Self {
        Self {
            subsec_nanos,
            seconds,
        }
    }

    /// Creates an RDuration of `secs` seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_secs(14);
    /// assert_eq!(dur.as_millis(), 14_000);
    /// assert_eq!(dur.as_micros(), 14_000_000);
    ///
    /// ```
    pub const fn from_secs(secs: u64) -> RDuration {
        RDuration {
            seconds: secs,
            subsec_nanos: 0,
        }
    }

    /// Creates an RDuration of `milli` milliseconds.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_millis(628);
    /// assert_eq!(dur.as_micros(), 628_000);
    /// assert_eq!(dur.as_nanos(), 628_000_000);
    ///
    /// ```
    pub const fn from_millis(milli: u64) -> RDuration {
        RDuration {
            seconds: milli / 1000,
            subsec_nanos: (milli % 1000) as u32 * 1_000_000,
        }
    }

    /// Creates an RDuration of `micro` microseconds.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_micros(1024);
    /// assert_eq!(dur.as_millis(), 1);
    /// assert_eq!(dur.as_nanos(), 1024_000);
    ///
    /// ```
    pub const fn from_micros(micro: u64) -> RDuration {
        let million = 1_000_000;
        RDuration {
            seconds: micro / million,
            subsec_nanos: (micro % million) as u32 * 1000,
        }
    }

    /// Creates an RDuration of `nano` nanoseconds.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(128_256_512);
    /// assert_eq!(dur.as_millis(), 128);
    /// assert_eq!(dur.as_micros(), 128_256);
    ///
    /// ```
    pub const fn from_nanos(nano: u64) -> RDuration {
        let billion = 1_000_000_000;
        RDuration {
            seconds: nano / billion,
            subsec_nanos: (nano % billion) as u32,
        }
    }

    /// The amount of fractional nanoseconds (total_nanoseconds % 1_000_000_000)
    /// of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(64_128_256_512);
    /// assert_eq!(dur.subsec_nanos(), 128_256_512);
    ///
    /// ```
    pub const fn subsec_nanos(&self) -> u32 {
        self.subsec_nanos
    }

    /// The amount of seconds of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(64_128_256_512);
    /// assert_eq!(dur.seconds(), 64);
    ///
    /// ```
    pub const fn seconds(&self) -> u64 {
        self.seconds
    }

    /// The amount of seconds of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(64_128_256_512);
    /// assert_eq!(dur.as_secs(), 64);
    ///
    /// ```
    pub const fn as_secs(&self) -> u64 {
        self.seconds
    }

    /// The amount of milliseconds of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(64_128_256_512);
    /// assert_eq!(dur.as_millis(), 64_128);
    ///
    /// ```
    pub const fn as_millis(&self) -> u128 {
        self.seconds as u128 * 1_000_u128 + (self.subsec_nanos / 1_000_000) as u128
    }

    /// The amount of microseconds of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_nanos(64_128_256_512);
    /// assert_eq!(dur.as_micros(), 64_128_256);
    ///
    /// ```
    pub const fn as_micros(&self) -> u128 {
        self.seconds as u128 * 1_000_000_u128 + (self.subsec_nanos / 1000) as u128
    }

    /// The amount of nanoseconds of this RDuration.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RDuration;
    ///
    /// let dur = RDuration::from_micros(256);
    /// assert_eq!(dur.as_nanos(), 256_000);
    ///
    /// ```
    pub const fn as_nanos(&self) -> u128 {
        self.seconds as u128 * 1_000_000_000_u128 + self.subsec_nanos as u128
    }
}

impl_from_rust_repr! {
    impl From<Duration> for RDuration {
        fn(v){
            RDuration {
                subsec_nanos: v.subsec_nanos(),
                seconds: v.as_secs(),
            }
        }
    }
}

impl_into_rust_repr! {
    impl Into<Duration> for RDuration {
        fn(this){
            Duration::new(this.seconds, this.subsec_nanos)
        }
    }
}