Skip to content

Commit 03a521b

Browse files
authored
Rollup merge of rust-lang#103059 - beetrees:duration-from-negative-zero, 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
2 parents 62a9c32 + c9948f5 commit 03a521b

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

library/core/src/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ macro_rules! try_from_secs {
12791279
const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
12801280
const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
12811281

1282-
if $secs.is_sign_negative() {
1282+
if $secs < 0.0 {
12831283
return Err(FromFloatSecsError { kind: FromFloatSecsErrorKind::Negative });
12841284
}
12851285

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
#![feature(provide_any)]
102102
#![feature(utf8_chunks)]
103103
#![feature(is_ascii_octdigit)]
104+
#![feature(duration_checked_float)]
104105
#![deny(unsafe_op_in_unsafe_fn)]
105106

106107
extern crate test;

library/core/tests/time.rs

+8
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,11 @@ fn duration_const() {
467467
const SATURATING_MUL: Duration = MAX.saturating_mul(2);
468468
assert_eq!(SATURATING_MUL, MAX);
469469
}
470+
471+
#[test]
472+
fn from_neg_zero() {
473+
assert_eq!(Duration::try_from_secs_f32(-0.0), Ok(Duration::ZERO));
474+
assert_eq!(Duration::try_from_secs_f64(-0.0), Ok(Duration::ZERO));
475+
assert_eq!(Duration::from_secs_f32(-0.0), Duration::ZERO);
476+
assert_eq!(Duration::from_secs_f64(-0.0), Duration::ZERO);
477+
}

0 commit comments

Comments
 (0)