Skip to content

Commit e25df32

Browse files
committed
consistent naming for duration_float methods and additional f32 methods
1 parent cf3c9a7 commit e25df32

File tree

1 file changed

+121
-9
lines changed

1 file changed

+121
-9
lines changed

src/libcore/time.rs

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const NANOS_PER_MICRO: u32 = 1_000;
2222
const MILLIS_PER_SEC: u64 = 1_000;
2323
const MICROS_PER_SEC: u64 = 1_000_000;
2424
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64;
25+
const MAX_NANOS_F32: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f32;
2526

2627
/// A `Duration` type to represent a span of time, typically used for system
2728
/// timeouts.
@@ -510,15 +511,34 @@ impl Duration {
510511
/// use std::time::Duration;
511512
///
512513
/// let dur = Duration::new(2, 700_000_000);
513-
/// assert_eq!(dur.as_float_secs(), 2.7);
514+
/// assert_eq!(dur.as_secs_f64(), 2.7);
514515
/// ```
515516
#[unstable(feature = "duration_float", issue = "54361")]
516517
#[inline]
517-
pub const fn as_float_secs(&self) -> f64 {
518+
pub const fn as_secs_f64(&self) -> f64 {
518519
(self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
519520
}
520521

521-
/// Creates a new `Duration` from the specified number of seconds.
522+
/// Returns the number of seconds contained by this `Duration` as `f32`.
523+
///
524+
/// The returned value does include the fractional (nanosecond) part of the duration.
525+
///
526+
/// # Examples
527+
/// ```
528+
/// #![feature(duration_float)]
529+
/// use std::time::Duration;
530+
///
531+
/// let dur = Duration::new(2, 700_000_000);
532+
/// assert_eq!(dur.as_secs_f32(), 2.7);
533+
/// ```
534+
#[unstable(feature = "duration_float", issue = "54361")]
535+
#[inline]
536+
pub const fn as_secs_f32(&self) -> f32 {
537+
(self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32)
538+
}
539+
540+
/// Creates a new `Duration` from the specified number of seconds represented
541+
/// as `f64`.
522542
///
523543
/// # Panics
524544
/// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
@@ -528,12 +548,12 @@ impl Duration {
528548
/// #![feature(duration_float)]
529549
/// use std::time::Duration;
530550
///
531-
/// let dur = Duration::from_float_secs(2.7);
551+
/// let dur = Duration::from_secs_f64(2.7);
532552
/// assert_eq!(dur, Duration::new(2, 700_000_000));
533553
/// ```
534554
#[unstable(feature = "duration_float", issue = "54361")]
535555
#[inline]
536-
pub fn from_float_secs(secs: f64) -> Duration {
556+
pub fn from_secs_f64(secs: f64) -> Duration {
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,40 @@ 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+
let nanos = secs * (NANOS_PER_SEC as f32);
592+
if !nanos.is_finite() {
593+
panic!("got non-finite value when converting float to duration");
594+
}
595+
if nanos >= MAX_NANOS_F32 {
596+
panic!("overflow when converting float to duration");
597+
}
598+
if nanos < 0.0 {
599+
panic!("underflow when converting float to duration");
600+
}
601+
let nanos = nanos as u128;
602+
Duration {
603+
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
604+
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
605+
}
606+
}
607+
554608
/// Multiplies `Duration` by `f64`.
555609
///
556610
/// # Panics
@@ -568,7 +622,27 @@ impl Duration {
568622
#[unstable(feature = "duration_float", issue = "54361")]
569623
#[inline]
570624
pub fn mul_f64(self, rhs: f64) -> Duration {
571-
Duration::from_float_secs(rhs * self.as_float_secs())
625+
Duration::from_secs_f64(rhs * self.as_secs_f64())
626+
}
627+
628+
/// Multiplies `Duration` by `f32`.
629+
///
630+
/// # Panics
631+
/// This method will panic if result is not finite, negative or overflows `Duration`.
632+
///
633+
/// # Examples
634+
/// ```
635+
/// #![feature(duration_float)]
636+
/// use std::time::Duration;
637+
///
638+
/// let dur = Duration::new(2, 700_000_000);
639+
/// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_000));
640+
/// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847_800, 0));
641+
/// ```
642+
#[unstable(feature = "duration_float", issue = "54361")]
643+
#[inline]
644+
pub fn mul_f32(self, rhs: f32) -> Duration {
645+
Duration::from_secs_f32(rhs * self.as_secs_f32())
572646
}
573647

574648
/// Divide `Duration` by `f64`.
@@ -589,7 +663,28 @@ impl Duration {
589663
#[unstable(feature = "duration_float", issue = "54361")]
590664
#[inline]
591665
pub fn div_f64(self, rhs: f64) -> Duration {
592-
Duration::from_float_secs(self.as_float_secs() / rhs)
666+
Duration::from_secs_f64(self.as_secs_f64() / rhs)
667+
}
668+
669+
/// Divide `Duration` by `f32`.
670+
///
671+
/// # Panics
672+
/// This method will panic if result is not finite, negative or overflows `Duration`.
673+
///
674+
/// # Examples
675+
/// ```
676+
/// #![feature(duration_float)]
677+
/// use std::time::Duration;
678+
///
679+
/// let dur = Duration::new(2, 700_000_000);
680+
/// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_611));
681+
/// // note that truncation is used, not rounding
682+
/// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_598));
683+
/// ```
684+
#[unstable(feature = "duration_float", issue = "54361")]
685+
#[inline]
686+
pub fn div_f32(self, rhs: f32) -> Duration {
687+
Duration::from_secs_f32(self.as_secs_f32() / rhs)
593688
}
594689

595690
/// Divide `Duration` by `Duration` and return `f64`.
@@ -605,8 +700,25 @@ impl Duration {
605700
/// ```
606701
#[unstable(feature = "duration_float", issue = "54361")]
607702
#[inline]
608-
pub fn div_duration(self, rhs: Duration) -> f64 {
609-
self.as_float_secs() / rhs.as_float_secs()
703+
pub fn div_duration_f64(self, rhs: Duration) -> f64 {
704+
self.as_secs_f64() / rhs.as_secs_f64()
705+
}
706+
707+
/// Divide `Duration` by `Duration` and return `f32`.
708+
///
709+
/// # Examples
710+
/// ```
711+
/// #![feature(duration_float)]
712+
/// use std::time::Duration;
713+
///
714+
/// let dur1 = Duration::new(2, 700_000_000);
715+
/// let dur2 = Duration::new(5, 400_000_000);
716+
/// assert_eq!(dur1.div_duration(dur2), 0.5);
717+
/// ```
718+
#[unstable(feature = "duration_float", issue = "54361")]
719+
#[inline]
720+
pub fn div_duration_f32(self, rhs: Duration) -> f32 {
721+
self.as_secs_f32() / rhs.as_secs_f32()
610722
}
611723
}
612724

0 commit comments

Comments
 (0)