Skip to content

Commit 744a99f

Browse files
CryZeTheTedder
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 744a99f

File tree

3 files changed

+338
-22
lines changed

3 files changed

+338
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ fn seconds_f64() {
189189
assert_panic!(Duration::seconds_f64(f64::NAN));
190190
}
191191

192+
#[test]
193+
fn saturating_seconds_f64() {
194+
assert_eq!(Duration::saturating_seconds_f64(0.5), 0.5.seconds());
195+
assert_eq!(Duration::saturating_seconds_f64(-0.5), (-0.5).seconds());
196+
197+
assert_eq!(Duration::saturating_seconds_f64(f64::MAX), Duration::MAX);
198+
assert_eq!(Duration::saturating_seconds_f64(f64::MIN), Duration::MIN);
199+
assert_eq!(Duration::saturating_seconds_f64(f64::NAN), Duration::ZERO);
200+
}
201+
202+
#[test]
203+
fn checked_seconds_f64() {
204+
assert_eq!(Duration::checked_seconds_f64(0.5), Some(0.5.seconds()));
205+
assert_eq!(Duration::checked_seconds_f64(-0.5), Some((-0.5).seconds()));
206+
207+
assert_eq!(Duration::checked_seconds_f64(f64::MAX), None);
208+
assert_eq!(Duration::checked_seconds_f64(f64::MIN), None);
209+
assert_eq!(Duration::checked_seconds_f64(f64::NAN), None);
210+
}
211+
192212
#[test]
193213
#[allow(clippy::float_cmp)]
194214
fn as_seconds_f64() {
@@ -210,6 +230,26 @@ fn seconds_f32() {
210230
assert_panic!(Duration::seconds_f32(f32::NAN));
211231
}
212232

233+
#[test]
234+
fn saturating_seconds_f32() {
235+
assert_eq!(Duration::saturating_seconds_f32(0.5), 0.5.seconds());
236+
assert_eq!(Duration::saturating_seconds_f32(-0.5), (-0.5).seconds());
237+
238+
assert_eq!(Duration::saturating_seconds_f32(f32::MAX), Duration::MAX);
239+
assert_eq!(Duration::saturating_seconds_f32(f32::MIN), Duration::MIN);
240+
assert_eq!(Duration::saturating_seconds_f32(f32::NAN), Duration::ZERO);
241+
}
242+
243+
#[test]
244+
fn checked_seconds_f32() {
245+
assert_eq!(Duration::checked_seconds_f32(0.5), Some(0.5.seconds()));
246+
assert_eq!(Duration::checked_seconds_f32(-0.5), Some((-0.5).seconds()));
247+
248+
assert_eq!(Duration::checked_seconds_f32(f32::MAX), None);
249+
assert_eq!(Duration::checked_seconds_f32(f32::MIN), None);
250+
assert_eq!(Duration::checked_seconds_f32(f32::NAN), None);
251+
}
252+
213253
#[test]
214254
#[allow(clippy::float_cmp)]
215255
fn as_seconds_f32() {

0 commit comments

Comments
 (0)