diff --git a/src/libcore/time.rs b/src/libcore/time.rs index ed1d5d46db5c4..e6a21cab0d338 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -110,6 +110,19 @@ impl Duration { #[unstable(feature = "duration_constants", issue = "57391")] pub const NANOSECOND: Duration = Duration::from_nanos(1); + /// The largest value a duration can hold. + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_constants)] + /// use std::time::Duration; + /// + /// assert_eq!(Duration::MAX, Duration::from_new(u64::MAX, 1_000_000_000-1)); + /// ``` + #[unstable(feature = "duration_constants", issue = "57391")] + pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC-1); + /// Creates a new `Duration` from the specified number of whole seconds and /// additional nanoseconds. /// @@ -130,10 +143,12 @@ impl Duration { /// ``` #[stable(feature = "duration", since = "1.3.0")] #[inline] - #[rustc_const_stable(feature = "duration_consts", since = "1.32.0")] - pub fn new(secs: u64, nanos: u32) -> Duration { - let secs = - secs.checked_add((nanos / NANOS_PER_SEC) as u64).expect("overflow in Duration::new"); + #[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")] + pub const fn new(secs: u64, nanos: u32) -> Duration { + let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { + Some(secs) => secs, + None => panic!("overflow in Duration::new"), + }; let nanos = nanos % NANOS_PER_SEC; Duration { secs, nanos } }