+ // Value is either too big to be represented as `Duration` or `NaN`.
+ OverflowOrNan,
+}
+
+macro_rules! try_from_secs {
+ (
+ secs = $secs: expr,
+ mantissa_bits = $mant_bits: literal,
+ exponent_bits = $exp_bits: literal,
+ offset = $offset: literal,
+ bits_ty = $bits_ty:ty,
+ double_ty = $double_ty:ty,
+ ) => {{
+ const MIN_EXP: i16 = 1 - (1i16 << $exp_bits) / 2;
+ const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1;
+ const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1;
+
+ if $secs.is_sign_negative() {
+ return Err(FromFloatSecsError { kind: FromFloatSecsErrorKind::Negative });
+ }
+
+ let bits = $secs.to_bits();
+ let mant = (bits & MANT_MASK) | (MANT_MASK + 1);
+ let exp = ((bits >> $mant_bits) & EXP_MASK) as i16 + MIN_EXP;
+
+ let (secs, nanos) = if exp < -30 {
+ // the input represents less than 1ns.
+ (0u64, 0u32)
+ } else if exp < 0 {
+ // the input is less than 1 second
+ let t = <$double_ty>::from(mant) << ($offset + exp);
+ let nanos = (u128::from(NANOS_PER_SEC) * u128::from(t)) >> ($mant_bits + $offset);
+ (0, nanos as u32)
+ } else if exp < $mant_bits {
+ let secs = mant >> ($mant_bits - exp);
+ let t = <$double_ty>::from((mant << exp) & MANT_MASK);
+ let nanos = (<$double_ty>::from(NANOS_PER_SEC) * t) >> $mant_bits;
+ (u64::from(secs), nanos as u32)
+ } else if exp < 64 {
+ // the input has no fractional part
+ let secs = u64::from(mant) << (exp - $mant_bits);
+ (secs, 0)
+ } else {
+ return Err(FromFloatSecsError { kind: FromFloatSecsErrorKind::OverflowOrNan });
+ };
+
+ Ok(Duration { secs, nanos })
+ }};
+}
+
+impl Duration {
+ /// The checked version of [`from_secs_f32`].
+ ///
+ /// [`from_secs_f32`]: Duration::from_secs_f32
+ ///
+ /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
+ ///
+ /// # Examples
+ /// ```
+ /// #![feature(duration_checked_float)]
+ ///
+ /// use std::time::Duration;
+ ///
+ /// let res = Duration::try_from_secs_f32(0.0);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// let res = Duration::try_from_secs_f32(1e-20);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// let res = Duration::try_from_secs_f32(4.2e-7);
+ /// assert_eq!(res, Ok(Duration::new(0, 419)));
+ /// let res = Duration::try_from_secs_f32(2.7);
+ /// assert_eq!(res, Ok(Duration::new(2, 700_000_047)));
+ /// let res = Duration::try_from_secs_f32(3e10);
+ /// assert_eq!(res, Ok(Duration::new(30_000_001_024, 0)));
+ /// // subnormal float:
+ /// let res = Duration::try_from_secs_f32(f32::from_bits(1));
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// // conversion uses truncation, not rounding
+ /// let res = Duration::try_from_secs_f32(0.999e-9);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ ///
+ /// let res = Duration::try_from_secs_f32(-5.0);
+ /// assert!(res.is_err());
+ /// let res = Duration::try_from_secs_f32(f32::NAN);
+ /// assert!(res.is_err());
+ /// let res = Duration::try_from_secs_f32(2e19);
+ /// assert!(res.is_err());
+ /// ```
+ #[unstable(feature = "duration_checked_float", issue = "83400")]
+ #[inline]
+ pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, FromFloatSecsError> {
+ try_from_secs!(
+ secs = secs,
+ mantissa_bits = 23,
+ exponent_bits = 8,
+ offset = 41,
+ bits_ty = u32,
+ double_ty = u64,
+ )
+ }
+
+ /// The checked version of [`from_secs_f64`].
+ ///
+ /// [`from_secs_f64`]: Duration::from_secs_f64
+ ///
+ /// This constructor will return an `Err` if `secs` is negative, overflows `Duration` or not finite.
+ ///
+ /// # Examples
+ /// ```
+ /// #![feature(duration_checked_float)]
+ ///
+ /// use std::time::Duration;
+ ///
+ /// let res = Duration::try_from_secs_f64(0.0);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// let res = Duration::try_from_secs_f64(1e-20);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// let res = Duration::try_from_secs_f64(4.2e-7);
+ /// assert_eq!(res, Ok(Duration::new(0, 420)));
+ /// let res = Duration::try_from_secs_f64(2.7);
+ /// assert_eq!(res, Ok(Duration::new(2, 700_000_000)));
+ /// let res = Duration::try_from_secs_f64(3e10);
+ /// assert_eq!(res, Ok(Duration::new(30_000_000_000, 0)));
+ /// // subnormal float
+ /// let res = Duration::try_from_secs_f64(f64::from_bits(1));
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ /// // conversion uses truncation, not rounding
+ /// let res = Duration::try_from_secs_f32(0.999e-9);
+ /// assert_eq!(res, Ok(Duration::new(0, 0)));
+ ///
+ /// let res = Duration::try_from_secs_f64(-5.0);
+ /// assert!(res.is_err());
+ /// let res = Duration::try_from_secs_f64(f64::NAN);
+ /// assert!(res.is_err());
+ /// let res = Duration::try_from_secs_f64(2e19);
+ /// assert!(res.is_err());
+ /// ```
+ #[unstable(feature = "duration_checked_float", issue = "83400")]
+ #[inline]
+ pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, FromFloatSecsError> {
+ try_from_secs!(
+ secs = secs,
+ mantissa_bits = 52,
+ exponent_bits = 11,
+ offset = 44,
+ bits_ty = u64,
+ double_ty = u128,
+ )
+ }