Skip to content

Commit 578e994

Browse files
committed
Use try_from_secs_* in Duration::from_secs_* functions.
`Duration::from_secs_{f32, f64}` now use the results from the non-panicking functions and unwrap it.
1 parent e4e137f commit 578e994

File tree

1 file changed

+6
-30
lines changed

1 file changed

+6
-30
lines changed

core/src/time.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -679,21 +679,9 @@ impl Duration {
679679
#[inline]
680680
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
681681
pub const fn from_secs_f64(secs: f64) -> Duration {
682-
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f64;
683-
let nanos = secs * (NANOS_PER_SEC as f64);
684-
if !nanos.is_finite() {
685-
panic!("got non-finite value when converting float to duration");
686-
}
687-
if nanos >= MAX_NANOS_F64 {
688-
panic!("overflow when converting float to duration");
689-
}
690-
if nanos < 0.0 {
691-
panic!("underflow when converting float to duration");
692-
}
693-
let nanos = nanos as u128;
694-
Duration {
695-
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
696-
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
682+
match Duration::try_from_secs_f64(secs) {
683+
Ok(v) => v,
684+
Err(e) => crate::panicking::panic(e.description()),
697685
}
698686
}
699687

@@ -752,21 +740,9 @@ impl Duration {
752740
#[inline]
753741
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
754742
pub const fn from_secs_f32(secs: f32) -> Duration {
755-
const MAX_NANOS_F32: f32 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f32;
756-
let nanos = secs * (NANOS_PER_SEC as f32);
757-
if !nanos.is_finite() {
758-
panic!("got non-finite value when converting float to duration");
759-
}
760-
if nanos >= MAX_NANOS_F32 {
761-
panic!("overflow when converting float to duration");
762-
}
763-
if nanos < 0.0 {
764-
panic!("underflow when converting float to duration");
765-
}
766-
let nanos = nanos as u128;
767-
Duration {
768-
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
769-
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
743+
match Duration::try_from_secs_f32(secs) {
744+
Ok(v) => v,
745+
Err(e) => crate::panicking::panic(e.description()),
770746
}
771747
}
772748

0 commit comments

Comments
 (0)