Skip to content

Commit e83d01f

Browse files
CryZeTheTedder
authored andcommitted
Port Duration::from_secs_f32|64 from std
This ports the algorithm for constructing `Duration` from floating point seconds from `std`. The new algorithm in `std` uses bit manipulation to construct the `Duration` with the most precision possible. Additionally this adds `saturating` and `checked` variants of those functions. Original `std` PR: rust-lang/rust#90247 Co-authored-by: Ted Wollman <[email protected]>
1 parent 22ae579 commit e83d01f

File tree

3 files changed

+360
-22
lines changed

3 files changed

+360
-22
lines changed

benchmarks/duration.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ setup_benchmark! {
127127
ben.iter(|| Duration::seconds_f32(-0.5));
128128
}
129129

130+
fn saturating_seconds_f64(ben: &mut Bencher<'_>) {
131+
ben.iter(|| Duration::saturating_seconds_f64(0.5));
132+
ben.iter(|| Duration::saturating_seconds_f64(-0.5));
133+
}
134+
135+
fn saturating_seconds_f32(ben: &mut Bencher<'_>) {
136+
ben.iter(|| Duration::saturating_seconds_f32(0.5));
137+
ben.iter(|| Duration::saturating_seconds_f32(-0.5));
138+
}
139+
140+
fn checked_seconds_f64(ben: &mut Bencher<'_>) {
141+
ben.iter(|| Duration::checked_seconds_f64(0.5));
142+
ben.iter(|| Duration::checked_seconds_f64(-0.5));
143+
}
144+
145+
fn checked_seconds_f32(ben: &mut Bencher<'_>) {
146+
ben.iter(|| Duration::checked_seconds_f32(0.5));
147+
ben.iter(|| Duration::checked_seconds_f32(-0.5));
148+
}
149+
130150
fn milliseconds(ben: &mut Bencher<'_>) {
131151
ben.iter(|| Duration::milliseconds(1));
132152
ben.iter(|| Duration::milliseconds(-1));

tests/duration.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,47 @@ fn whole_seconds() {
183183
fn seconds_f64() {
184184
assert_eq!(Duration::seconds_f64(0.5), 0.5.seconds());
185185
assert_eq!(Duration::seconds_f64(-0.5), (-0.5).seconds());
186+
assert_eq!(Duration::seconds_f64(123.250), 123.250.seconds());
187+
assert_eq!(Duration::seconds_f64(0.000_000_000_012), Duration::ZERO);
186188

187189
assert_panic!(Duration::seconds_f64(f64::MAX));
188190
assert_panic!(Duration::seconds_f64(f64::MIN));
189191
assert_panic!(Duration::seconds_f64(f64::NAN));
190192
}
191193

194+
#[test]
195+
fn saturating_seconds_f64() {
196+
assert_eq!(Duration::saturating_seconds_f64(0.5), 0.5.seconds());
197+
assert_eq!(Duration::saturating_seconds_f64(-0.5), (-0.5).seconds());
198+
assert_eq!(Duration::saturating_seconds_f64(123.250), 123.250.seconds());
199+
assert_eq!(
200+
Duration::saturating_seconds_f64(0.000_000_000_012),
201+
Duration::ZERO
202+
);
203+
204+
assert_eq!(Duration::saturating_seconds_f64(f64::MAX), Duration::MAX);
205+
assert_eq!(Duration::saturating_seconds_f64(f64::MIN), Duration::MIN);
206+
assert_eq!(Duration::saturating_seconds_f64(f64::NAN), Duration::ZERO);
207+
}
208+
209+
#[test]
210+
fn checked_seconds_f64() {
211+
assert_eq!(Duration::checked_seconds_f64(0.5), Some(0.5.seconds()));
212+
assert_eq!(Duration::checked_seconds_f64(-0.5), Some((-0.5).seconds()));
213+
assert_eq!(
214+
Duration::checked_seconds_f64(123.250),
215+
Some(123.250.seconds())
216+
);
217+
assert_eq!(
218+
Duration::checked_seconds_f64(0.000_000_000_012),
219+
Some(Duration::ZERO)
220+
);
221+
222+
assert_eq!(Duration::checked_seconds_f64(f64::MAX), None);
223+
assert_eq!(Duration::checked_seconds_f64(f64::MIN), None);
224+
assert_eq!(Duration::checked_seconds_f64(f64::NAN), None);
225+
}
226+
192227
#[test]
193228
#[allow(clippy::float_cmp)]
194229
fn as_seconds_f64() {
@@ -204,12 +239,47 @@ fn as_seconds_f64() {
204239
fn seconds_f32() {
205240
assert_eq!(Duration::seconds_f32(0.5), 0.5.seconds());
206241
assert_eq!(Duration::seconds_f32(-0.5), (-0.5).seconds());
242+
assert_eq!(Duration::seconds_f32(123.250), 123.250.seconds());
243+
assert_eq!(Duration::seconds_f32(0.000_000_000_012), Duration::ZERO);
207244

208245
assert_panic!(Duration::seconds_f32(f32::MAX));
209246
assert_panic!(Duration::seconds_f32(f32::MIN));
210247
assert_panic!(Duration::seconds_f32(f32::NAN));
211248
}
212249

250+
#[test]
251+
fn saturating_seconds_f32() {
252+
assert_eq!(Duration::saturating_seconds_f32(0.5), 0.5.seconds());
253+
assert_eq!(Duration::saturating_seconds_f32(-0.5), (-0.5).seconds());
254+
assert_eq!(Duration::saturating_seconds_f32(123.250), 123.250.seconds());
255+
assert_eq!(
256+
Duration::saturating_seconds_f32(0.000_000_000_012),
257+
Duration::ZERO
258+
);
259+
260+
assert_eq!(Duration::saturating_seconds_f32(f32::MAX), Duration::MAX);
261+
assert_eq!(Duration::saturating_seconds_f32(f32::MIN), Duration::MIN);
262+
assert_eq!(Duration::saturating_seconds_f32(f32::NAN), Duration::ZERO);
263+
}
264+
265+
#[test]
266+
fn checked_seconds_f32() {
267+
assert_eq!(Duration::checked_seconds_f32(0.5), Some(0.5.seconds()));
268+
assert_eq!(Duration::checked_seconds_f32(-0.5), Some((-0.5).seconds()));
269+
assert_eq!(
270+
Duration::checked_seconds_f32(123.250),
271+
Some(123.250.seconds())
272+
);
273+
assert_eq!(
274+
Duration::checked_seconds_f32(0.000_000_000_012),
275+
Some(Duration::ZERO)
276+
);
277+
278+
assert_eq!(Duration::checked_seconds_f32(f32::MAX), None);
279+
assert_eq!(Duration::checked_seconds_f32(f32::MIN), None);
280+
assert_eq!(Duration::checked_seconds_f32(f32::NAN), None);
281+
}
282+
213283
#[test]
214284
#[allow(clippy::float_cmp)]
215285
fn as_seconds_f32() {

0 commit comments

Comments
 (0)