Skip to content

Commit

Permalink
Rollup merge of rust-lang#103059 - beetrees:duration-from-negative-ze…
Browse files Browse the repository at this point in the history
…ro, r=thomcc

Fix `Duration::{try_,}from_secs_f{32,64}(-0.0)`

Make `Duration::{try_,}from_secs_f{32,64}(-0.0)` return `Duration::ZERO` (as they did before rust-lang#90247) instead of erroring/panicking.

I'll update this PR to remove the `#![feature(duration_checked_float)]` if rust-lang#102271 is merged before this PR.

Tracking issue for `try_from_secs_f{32,64}`: rust-lang#83400
  • Loading branch information
matthiaskrgr authored Oct 14, 2022
2 parents 62a9c32 + c9948f5 commit 03a521b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ macro_rules! try_from_secs {
const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;

if $secs.is_sign_negative() {
if $secs < 0.0 {
return Err(FromFloatSecsError { kind: FromFloatSecsErrorKind::Negative });
}

Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#![feature(provide_any)]
#![feature(utf8_chunks)]
#![feature(is_ascii_octdigit)]
#![feature(duration_checked_float)]
#![deny(unsafe_op_in_unsafe_fn)]

extern crate test;
Expand Down
8 changes: 8 additions & 0 deletions library/core/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,11 @@ fn duration_const() {
const SATURATING_MUL: Duration = MAX.saturating_mul(2);
assert_eq!(SATURATING_MUL, MAX);
}

#[test]
fn from_neg_zero() {
assert_eq!(Duration::try_from_secs_f32(-0.0), Ok(Duration::ZERO));
assert_eq!(Duration::try_from_secs_f64(-0.0), Ok(Duration::ZERO));
assert_eq!(Duration::from_secs_f32(-0.0), Duration::ZERO);
assert_eq!(Duration::from_secs_f64(-0.0), Duration::ZERO);
}

0 comments on commit 03a521b

Please sign in to comment.