diff options
| author | mbartlett21 <29034492+mbartlett21@users.noreply.github.com> | 2021-06-14 12:17:53 +0000 |
|---|---|---|
| committer | mbartlett21 <29034492+mbartlett21@users.noreply.github.com> | 2021-06-14 12:17:53 +0000 |
| commit | 7803955cae3dffb37c315e95764df8e949eea590 (patch) | |
| tree | 5df4be77fb756fc09038b8070abe112a93ea3a2b | |
| parent | c2c1ca071f242c3e84775bba2a05a6c924f44b1e (diff) | |
| download | rust-7803955cae3dffb37c315e95764df8e949eea590.tar.gz rust-7803955cae3dffb37c315e95764df8e949eea590.zip | |
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.
| -rw-r--r-- | library/core/src/time.rs | 36 |
1 files changed, 6 insertions, 30 deletions
diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 22e8e143986..cc37000bb14 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -679,21 +679,9 @@ impl Duration { #[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 @@ impl Duration { #[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()), } } |
