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
use crate::abi;
use core::{convert::TryFrom, time::Duration as StdDuration};
use super::Timeout;
#[cfg_attr(
feature = "nightly",
doc = "[`duration!`] can be used to construct a `Duration` in a concise syntax."
)]
#[cfg_attr(
not(feature = "nightly"),
doc = "If `nightly` feature is enabled, \
`duration!` can be used to construct a `Duration` in a concise syntax."
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Duration {
value: abi::RELTIM,
}
impl Duration {
pub const ZERO: Self = unsafe { Self::from_raw(0) };
#[inline]
pub const unsafe fn from_raw(value: abi::RELTIM) -> Self {
Self { value }
}
#[inline]
pub const fn as_raw(self) -> abi::RELTIM {
self.value
}
#[inline]
pub const fn from_secs(secs: u64) -> Option<Self> {
if secs > u64::MAX / 1_000_000 {
None
} else {
Self::from_micros(secs * 1_000_000)
}
}
#[inline]
pub const fn from_millis(millis: u64) -> Option<Self> {
if millis > u64::MAX / 1_000 {
None
} else {
Self::from_micros(millis * 1_000)
}
}
#[inline]
pub const fn from_micros(micros: u64) -> Option<Self> {
match () {
() => {
if micros > abi::TMAX_RELTIM as u64 {
None
} else {
Some(unsafe { Self::from_raw(micros as u32) })
}
}
}
}
#[inline]
pub fn from_nanos(nanos: u128) -> Option<Self> {
u64::try_from(nanos / 1_000)
.ok()
.and_then(Self::from_micros)
}
}
impl TryFrom<StdDuration> for Duration {
type Error = super::TryFromDurationError;
#[inline]
fn try_from(d: StdDuration) -> Result<Self, Self::Error> {
Self::from_nanos(d.as_nanos()).ok_or(super::TryFromDurationError(()))
}
}
impl TryFrom<Timeout> for Duration {
type Error = super::TryFromDurationError;
#[inline]
fn try_from(d: Timeout) -> Result<Self, Self::Error> {
match () {
() => {
if d.is_finite() {
Ok(unsafe { Self::from_raw(d.as_raw()) })
} else {
Err(super::TryFromDurationError(()))
}
}
}
}
}
#[cfg(feature = "nightly")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "nightly")))]
pub macro duration {
($kind:tt: $value:literal) => {{
const VALUE: $crate::time::Duration = $crate::time::duration!($kind: ($value));
VALUE
}},
(s: $value:expr) => {
$crate::time::expect_valid_duration($crate::time::Duration::from_secs($value))
},
(ms: $value:expr) => {
$crate::time::expect_valid_duration($crate::time::Duration::from_millis($value))
},
(us: $value:expr) => {
$crate::time::expect_valid_duration($crate::time::Duration::from_micros($value))
},
(μs: $value:expr) => {
$crate::time::expect_valid_duration($crate::time::Duration::from_micros($value))
},
(ns: $value:expr) => {
$crate::time::expect_valid_duration($crate::time::Duration::from_nanos($value))
},
(0) => { $crate::time::Duration::ZERO },
}
#[cfg(feature = "nightly")]
#[doc(hidden)]
#[track_caller]
#[inline]
pub const fn expect_valid_duration(x: Option<Duration>) -> Duration {
if let Some(x) = x {
x
} else {
let zero = 0u32;
#[allow(unconditional_panic)]
let __the_specified_timeout_is_invalid_or_not_representable__ = 1 / zero;
#[allow(clippy::empty_loop)]
loop {}
}
}