Coverage Report

Created: 2026-05-16 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/time-0.3.14/src/ext.rs
Line
Count
Source
1
//! Extension traits.
2
3
use core::time::Duration as StdDuration;
4
5
use crate::Duration;
6
7
/// Sealed trait to prevent downstream implementations.
8
mod sealed {
9
    /// A trait that cannot be implemented by downstream users.
10
    pub trait Sealed {}
11
    impl Sealed for i64 {}
12
    impl Sealed for u64 {}
13
    impl Sealed for f64 {}
14
}
15
16
// region: NumericalDuration
17
/// Create [`Duration`]s from numeric literals.
18
///
19
/// # Examples
20
///
21
/// Basic construction of [`Duration`]s.
22
///
23
/// ```rust
24
/// # use time::{Duration, ext::NumericalDuration};
25
/// assert_eq!(5.nanoseconds(), Duration::nanoseconds(5));
26
/// assert_eq!(5.microseconds(), Duration::microseconds(5));
27
/// assert_eq!(5.milliseconds(), Duration::milliseconds(5));
28
/// assert_eq!(5.seconds(), Duration::seconds(5));
29
/// assert_eq!(5.minutes(), Duration::minutes(5));
30
/// assert_eq!(5.hours(), Duration::hours(5));
31
/// assert_eq!(5.days(), Duration::days(5));
32
/// assert_eq!(5.weeks(), Duration::weeks(5));
33
/// ```
34
///
35
/// Signed integers work as well!
36
///
37
/// ```rust
38
/// # use time::{Duration, ext::NumericalDuration};
39
/// assert_eq!((-5).nanoseconds(), Duration::nanoseconds(-5));
40
/// assert_eq!((-5).microseconds(), Duration::microseconds(-5));
41
/// assert_eq!((-5).milliseconds(), Duration::milliseconds(-5));
42
/// assert_eq!((-5).seconds(), Duration::seconds(-5));
43
/// assert_eq!((-5).minutes(), Duration::minutes(-5));
44
/// assert_eq!((-5).hours(), Duration::hours(-5));
45
/// assert_eq!((-5).days(), Duration::days(-5));
46
/// assert_eq!((-5).weeks(), Duration::weeks(-5));
47
/// ```
48
///
49
/// Just like any other [`Duration`], they can be added, subtracted, etc.
50
///
51
/// ```rust
52
/// # use time::ext::NumericalDuration;
53
/// assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
54
/// assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());
55
/// ```
56
///
57
/// When called on floating point values, any remainder of the floating point value will be
58
/// truncated. Keep in mind that floating point numbers are inherently imprecise and have limited
59
/// capacity.
60
pub trait NumericalDuration: sealed::Sealed {
61
    /// Create a [`Duration`] from the number of nanoseconds.
62
    fn nanoseconds(self) -> Duration;
63
    /// Create a [`Duration`] from the number of microseconds.
64
    fn microseconds(self) -> Duration;
65
    /// Create a [`Duration`] from the number of milliseconds.
66
    fn milliseconds(self) -> Duration;
67
    /// Create a [`Duration`] from the number of seconds.
68
    fn seconds(self) -> Duration;
69
    /// Create a [`Duration`] from the number of minutes.
70
    fn minutes(self) -> Duration;
71
    /// Create a [`Duration`] from the number of hours.
72
    fn hours(self) -> Duration;
73
    /// Create a [`Duration`] from the number of days.
74
    fn days(self) -> Duration;
75
    /// Create a [`Duration`] from the number of weeks.
76
    fn weeks(self) -> Duration;
77
}
78
79
impl NumericalDuration for i64 {
80
0
    fn nanoseconds(self) -> Duration {
81
0
        Duration::nanoseconds(self)
82
0
    }
83
84
0
    fn microseconds(self) -> Duration {
85
0
        Duration::microseconds(self)
86
0
    }
87
88
0
    fn milliseconds(self) -> Duration {
89
0
        Duration::milliseconds(self)
90
0
    }
91
92
0
    fn seconds(self) -> Duration {
93
0
        Duration::seconds(self)
94
0
    }
95
96
0
    fn minutes(self) -> Duration {
97
0
        Duration::minutes(self)
98
0
    }
99
100
0
    fn hours(self) -> Duration {
101
0
        Duration::hours(self)
102
0
    }
103
104
0
    fn days(self) -> Duration {
105
0
        Duration::days(self)
106
0
    }
107
108
0
    fn weeks(self) -> Duration {
109
0
        Duration::weeks(self)
110
0
    }
111
}
112
113
impl NumericalDuration for f64 {
114
0
    fn nanoseconds(self) -> Duration {
115
0
        Duration::nanoseconds(self as _)
116
0
    }
117
118
0
    fn microseconds(self) -> Duration {
119
0
        Duration::nanoseconds((self * 1_000.) as _)
120
0
    }
121
122
0
    fn milliseconds(self) -> Duration {
123
0
        Duration::nanoseconds((self * 1_000_000.) as _)
124
0
    }
125
126
0
    fn seconds(self) -> Duration {
127
0
        Duration::nanoseconds((self * 1_000_000_000.) as _)
128
0
    }
129
130
0
    fn minutes(self) -> Duration {
131
0
        Duration::nanoseconds((self * 60_000_000_000.) as _)
132
0
    }
133
134
0
    fn hours(self) -> Duration {
135
0
        Duration::nanoseconds((self * 3_600_000_000_000.) as _)
136
0
    }
137
138
0
    fn days(self) -> Duration {
139
0
        Duration::nanoseconds((self * 86_400_000_000_000.) as _)
140
0
    }
141
142
0
    fn weeks(self) -> Duration {
143
0
        Duration::nanoseconds((self * 604_800_000_000_000.) as _)
144
0
    }
145
}
146
// endregion NumericalDuration
147
148
// region: NumericalStdDuration
149
/// Create [`std::time::Duration`]s from numeric literals.
150
///
151
/// # Examples
152
///
153
/// Basic construction of [`std::time::Duration`]s.
154
///
155
/// ```rust
156
/// # use time::ext::NumericalStdDuration;
157
/// # use core::time::Duration;
158
/// assert_eq!(5.std_nanoseconds(), Duration::from_nanos(5));
159
/// assert_eq!(5.std_microseconds(), Duration::from_micros(5));
160
/// assert_eq!(5.std_milliseconds(), Duration::from_millis(5));
161
/// assert_eq!(5.std_seconds(), Duration::from_secs(5));
162
/// assert_eq!(5.std_minutes(), Duration::from_secs(5 * 60));
163
/// assert_eq!(5.std_hours(), Duration::from_secs(5 * 3_600));
164
/// assert_eq!(5.std_days(), Duration::from_secs(5 * 86_400));
165
/// assert_eq!(5.std_weeks(), Duration::from_secs(5 * 604_800));
166
/// ```
167
///
168
/// Just like any other [`std::time::Duration`], they can be added, subtracted, etc.
169
///
170
/// ```rust
171
/// # use time::ext::NumericalStdDuration;
172
/// assert_eq!(
173
///     2.std_seconds() + 500.std_milliseconds(),
174
///     2_500.std_milliseconds()
175
/// );
176
/// assert_eq!(
177
///     2.std_seconds() - 500.std_milliseconds(),
178
///     1_500.std_milliseconds()
179
/// );
180
/// ```
181
///
182
/// When called on floating point values, any remainder of the floating point value will be
183
/// truncated. Keep in mind that floating point numbers are inherently imprecise and have limited
184
/// capacity.
185
pub trait NumericalStdDuration: sealed::Sealed {
186
    /// Create a [`std::time::Duration`] from the number of nanoseconds.
187
    fn std_nanoseconds(self) -> StdDuration;
188
    /// Create a [`std::time::Duration`] from the number of microseconds.
189
    fn std_microseconds(self) -> StdDuration;
190
    /// Create a [`std::time::Duration`] from the number of milliseconds.
191
    fn std_milliseconds(self) -> StdDuration;
192
    /// Create a [`std::time::Duration`] from the number of seconds.
193
    fn std_seconds(self) -> StdDuration;
194
    /// Create a [`std::time::Duration`] from the number of minutes.
195
    fn std_minutes(self) -> StdDuration;
196
    /// Create a [`std::time::Duration`] from the number of hours.
197
    fn std_hours(self) -> StdDuration;
198
    /// Create a [`std::time::Duration`] from the number of days.
199
    fn std_days(self) -> StdDuration;
200
    /// Create a [`std::time::Duration`] from the number of weeks.
201
    fn std_weeks(self) -> StdDuration;
202
}
203
204
impl NumericalStdDuration for u64 {
205
0
    fn std_nanoseconds(self) -> StdDuration {
206
0
        StdDuration::from_nanos(self)
207
0
    }
208
209
0
    fn std_microseconds(self) -> StdDuration {
210
0
        StdDuration::from_micros(self)
211
0
    }
212
213
0
    fn std_milliseconds(self) -> StdDuration {
214
0
        StdDuration::from_millis(self)
215
0
    }
216
217
0
    fn std_seconds(self) -> StdDuration {
218
0
        StdDuration::from_secs(self)
219
0
    }
220
221
0
    fn std_minutes(self) -> StdDuration {
222
0
        StdDuration::from_secs(self * 60)
223
0
    }
224
225
0
    fn std_hours(self) -> StdDuration {
226
0
        StdDuration::from_secs(self * 3_600)
227
0
    }
228
229
0
    fn std_days(self) -> StdDuration {
230
0
        StdDuration::from_secs(self * 86_400)
231
0
    }
232
233
0
    fn std_weeks(self) -> StdDuration {
234
0
        StdDuration::from_secs(self * 604_800)
235
0
    }
236
}
237
238
impl NumericalStdDuration for f64 {
239
0
    fn std_nanoseconds(self) -> StdDuration {
240
0
        assert!(self >= 0.);
241
0
        StdDuration::from_nanos(self as _)
242
0
    }
243
244
0
    fn std_microseconds(self) -> StdDuration {
245
0
        assert!(self >= 0.);
246
0
        StdDuration::from_nanos((self * 1_000.) as _)
247
0
    }
248
249
0
    fn std_milliseconds(self) -> StdDuration {
250
0
        assert!(self >= 0.);
251
0
        StdDuration::from_nanos((self * 1_000_000.) as _)
252
0
    }
253
254
0
    fn std_seconds(self) -> StdDuration {
255
0
        assert!(self >= 0.);
256
0
        StdDuration::from_nanos((self * 1_000_000_000.) as _)
257
0
    }
258
259
0
    fn std_minutes(self) -> StdDuration {
260
0
        assert!(self >= 0.);
261
0
        StdDuration::from_nanos((self * 60_000_000_000.) as _)
262
0
    }
263
264
0
    fn std_hours(self) -> StdDuration {
265
0
        assert!(self >= 0.);
266
0
        StdDuration::from_nanos((self * 3_600_000_000_000.) as _)
267
0
    }
268
269
0
    fn std_days(self) -> StdDuration {
270
0
        assert!(self >= 0.);
271
0
        StdDuration::from_nanos((self * 86_400_000_000_000.) as _)
272
0
    }
273
274
0
    fn std_weeks(self) -> StdDuration {
275
0
        assert!(self >= 0.);
276
0
        StdDuration::from_nanos((self * 604_800_000_000_000.) as _)
277
0
    }
278
}
279
// endregion NumericalStdDuration