Skip to content

Commit a7ecf5f

Browse files
authored
Rollup merge of rust-lang#59102 - newpavlov:duration_float, r=alexcrichton
Consistent naming for duration_float methods and additional f32 methods `duration_float` tracking issue: rust-lang#54361 New list of methods: - `as_secs_f64(&self) -> f64` - `as_secs_f32(&self) -> f32` - `from_secs_f64(secs: f64) -> Duration` - `from_secs_f32(secs: f32) -> Duration` - `mul_f64(self, rhs: f64) -> Duration` - `mul_f32(self, rhs: f32) -> Duration` - `div_f64(self, rhs: f64) -> Duration` - `div_f32(self, rhs: f64) -> Duration` - `div_duration_f64(self, rhs: Duration) -> f64` - `div_duration_f32(self, rhs: Duration) -> f32` With [`num_traits::Float`](https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html) we could've reduced number of methods by factor of two, but unfortunately it's not part of `std`.
2 parents 153fd15 + 78b248d commit a7ecf5f

File tree

1 file changed

+129
-11
lines changed

1 file changed

+129
-11
lines changed

src/libcore/time.rs

+129-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
2121
const NANOS_PER_MICRO: u32 = 1_000;
2222
const MILLIS_PER_SEC: u64 = 1_000;
2323
const MICROS_PER_SEC: u64 = 1_000_000;
24-
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64;
2524

2625
/// A `Duration` type to represent a span of time, typically used for system
2726
/// timeouts.
@@ -510,15 +509,34 @@ impl Duration {
510509
/// use std::time::Duration;
511510
///
512511
/// let dur = Duration::new(2, 700_000_000);
513-
/// assert_eq!(dur.as_float_secs(), 2.7);
512+
/// assert_eq!(dur.as_secs_f64(), 2.7);
514513
/// ```
515514
#[unstable(feature = "duration_float", issue = "54361")]
516515
#[inline]
517-
pub const fn as_float_secs(&self) -> f64 {
516+
pub const fn as_secs_f64(&self) -> f64 {
518517
(self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
519518
}
520519

521-
/// Creates a new `Duration` from the specified number of seconds.
520+
/// Returns the number of seconds contained by this `Duration` as `f32`.
521+
///
522+
/// The returned value does include the fractional (nanosecond) part of the duration.
523+
///
524+
/// # Examples
525+
/// ```
526+
/// #![feature(duration_float)]
527+
/// use std::time::Duration;
528+
///
529+
/// let dur = Duration::new(2, 700_000_000);
530+
/// assert_eq!(dur.as_secs_f32(), 2.7);
531+
/// ```
532+
#[unstable(feature = "duration_float", issue = "54361")]
533+
#[inline]
534+
pub const fn as_secs_f32(&self) -> f32 {
535+
(self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32)
536+
}
537+
538+
/// Creates a new `Duration` from the specified number of seconds represented
539+
/// as `f64`.
522540
///
523541
/// # Panics
524542
/// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
@@ -528,12 +546,14 @@ impl Duration {
528546
/// #![feature(duration_float)]
529547
/// use std::time::Duration;
530548
///
531-
/// let dur = Duration::from_float_secs(2.7);
549+
/// let dur = Duration::from_secs_f64(2.7);
532550
/// assert_eq!(dur, Duration::new(2, 700_000_000));
533551
/// ```
534552
#[unstable(feature = "duration_float", issue = "54361")]
535553
#[inline]
536-
pub fn from_float_secs(secs: f64) -> Duration {
554+
pub fn from_secs_f64(secs: f64) -> Duration {
555+
const MAX_NANOS_F64: f64 =
556+
((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64;
537557
let nanos = secs * (NANOS_PER_SEC as f64);
538558
if !nanos.is_finite() {
539559
panic!("got non-finite value when converting float to duration");
@@ -551,6 +571,42 @@ impl Duration {
551571
}
552572
}
553573

574+
/// Creates a new `Duration` from the specified number of seconds represented
575+
/// as `f32`.
576+
///
577+
/// # Panics
578+
/// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
579+
///
580+
/// # Examples
581+
/// ```
582+
/// #![feature(duration_float)]
583+
/// use std::time::Duration;
584+
///
585+
/// let dur = Duration::from_secs_f32(2.7);
586+
/// assert_eq!(dur, Duration::new(2, 700_000_000));
587+
/// ```
588+
#[unstable(feature = "duration_float", issue = "54361")]
589+
#[inline]
590+
pub fn from_secs_f32(secs: f32) -> Duration {
591+
const MAX_NANOS_F32: f32 =
592+
((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f32;
593+
let nanos = secs * (NANOS_PER_SEC as f32);
594+
if !nanos.is_finite() {
595+
panic!("got non-finite value when converting float to duration");
596+
}
597+
if nanos >= MAX_NANOS_F32 {
598+
panic!("overflow when converting float to duration");
599+
}
600+
if nanos < 0.0 {
601+
panic!("underflow when converting float to duration");
602+
}
603+
let nanos = nanos as u128;
604+
Duration {
605+
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
606+
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
607+
}
608+
}
609+
554610
/// Multiplies `Duration` by `f64`.
555611
///
556612
/// # Panics
@@ -568,7 +624,29 @@ impl Duration {
568624
#[unstable(feature = "duration_float", issue = "54361")]
569625
#[inline]
570626
pub fn mul_f64(self, rhs: f64) -> Duration {
571-
Duration::from_float_secs(rhs * self.as_float_secs())
627+
Duration::from_secs_f64(rhs * self.as_secs_f64())
628+
}
629+
630+
/// Multiplies `Duration` by `f32`.
631+
///
632+
/// # Panics
633+
/// This method will panic if result is not finite, negative or overflows `Duration`.
634+
///
635+
/// # Examples
636+
/// ```
637+
/// #![feature(duration_float)]
638+
/// use std::time::Duration;
639+
///
640+
/// let dur = Duration::new(2, 700_000_000);
641+
/// // note that due to rounding errors result is slightly different
642+
/// // from 8.478 and 847800.0
643+
/// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_640));
644+
/// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847799, 969_120_256));
645+
/// ```
646+
#[unstable(feature = "duration_float", issue = "54361")]
647+
#[inline]
648+
pub fn mul_f32(self, rhs: f32) -> Duration {
649+
Duration::from_secs_f32(rhs * self.as_secs_f32())
572650
}
573651

574652
/// Divide `Duration` by `f64`.
@@ -589,7 +667,30 @@ impl Duration {
589667
#[unstable(feature = "duration_float", issue = "54361")]
590668
#[inline]
591669
pub fn div_f64(self, rhs: f64) -> Duration {
592-
Duration::from_float_secs(self.as_float_secs() / rhs)
670+
Duration::from_secs_f64(self.as_secs_f64() / rhs)
671+
}
672+
673+
/// Divide `Duration` by `f32`.
674+
///
675+
/// # Panics
676+
/// This method will panic if result is not finite, negative or overflows `Duration`.
677+
///
678+
/// # Examples
679+
/// ```
680+
/// #![feature(duration_float)]
681+
/// use std::time::Duration;
682+
///
683+
/// let dur = Duration::new(2, 700_000_000);
684+
/// // note that due to rounding errors result is slightly
685+
/// // different from 0.859_872_611
686+
/// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_576));
687+
/// // note that truncation is used, not rounding
688+
/// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_598));
689+
/// ```
690+
#[unstable(feature = "duration_float", issue = "54361")]
691+
#[inline]
692+
pub fn div_f32(self, rhs: f32) -> Duration {
693+
Duration::from_secs_f32(self.as_secs_f32() / rhs)
593694
}
594695

595696
/// Divide `Duration` by `Duration` and return `f64`.
@@ -601,12 +702,29 @@ impl Duration {
601702
///
602703
/// let dur1 = Duration::new(2, 700_000_000);
603704
/// let dur2 = Duration::new(5, 400_000_000);
604-
/// assert_eq!(dur1.div_duration(dur2), 0.5);
705+
/// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
706+
/// ```
707+
#[unstable(feature = "duration_float", issue = "54361")]
708+
#[inline]
709+
pub fn div_duration_f64(self, rhs: Duration) -> f64 {
710+
self.as_secs_f64() / rhs.as_secs_f64()
711+
}
712+
713+
/// Divide `Duration` by `Duration` and return `f32`.
714+
///
715+
/// # Examples
716+
/// ```
717+
/// #![feature(duration_float)]
718+
/// use std::time::Duration;
719+
///
720+
/// let dur1 = Duration::new(2, 700_000_000);
721+
/// let dur2 = Duration::new(5, 400_000_000);
722+
/// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
605723
/// ```
606724
#[unstable(feature = "duration_float", issue = "54361")]
607725
#[inline]
608-
pub fn div_duration(self, rhs: Duration) -> f64 {
609-
self.as_float_secs() / rhs.as_float_secs()
726+
pub fn div_duration_f32(self, rhs: Duration) -> f32 {
727+
self.as_secs_f32() / rhs.as_secs_f32()
610728
}
611729
}
612730

0 commit comments

Comments
 (0)