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.
This commit is contained in:
mbartlett21 2021-06-14 12:17:53 +00:00
parent c2c1ca071f
commit 7803955cae

View File

@ -679,21 +679,9 @@ pub const fn as_secs_f32(&self) -> f32 {
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
pub const fn from_secs_f64(secs: f64) -> Duration {
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f64;
let nanos = secs * (NANOS_PER_SEC as f64);
if !nanos.is_finite() {
panic!("got non-finite value when converting float to duration");
}
if nanos >= MAX_NANOS_F64 {
panic!("overflow when converting float to duration");
}
if nanos < 0.0 {
panic!("underflow when converting float to duration");
}
let nanos = nanos as u128;
Duration {
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
match Duration::try_from_secs_f64(secs) {
Ok(v) => v,
Err(e) => crate::panicking::panic(e.description()),
}
}
@ -752,21 +740,9 @@ pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, FromSecsError> {
#[inline]
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
pub const fn from_secs_f32(secs: f32) -> Duration {
const MAX_NANOS_F32: f32 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f32;
let nanos = secs * (NANOS_PER_SEC as f32);
if !nanos.is_finite() {
panic!("got non-finite value when converting float to duration");
}
if nanos >= MAX_NANOS_F32 {
panic!("overflow when converting float to duration");
}
if nanos < 0.0 {
panic!("underflow when converting float to duration");
}
let nanos = nanos as u128;
Duration {
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
match Duration::try_from_secs_f32(secs) {
Ok(v) => v,
Err(e) => crate::panicking::panic(e.description()),
}
}