@@ -22,6 +22,7 @@ const NANOS_PER_MICRO: u32 = 1_000;
22
22
const MILLIS_PER_SEC : u64 = 1_000 ;
23
23
const MICROS_PER_SEC : u64 = 1_000_000 ;
24
24
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 ;
25
26
26
27
/// A `Duration` type to represent a span of time, typically used for system
27
28
/// timeouts.
@@ -510,15 +511,34 @@ impl Duration {
510
511
/// use std::time::Duration;
511
512
///
512
513
/// 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);
514
515
/// ```
515
516
#[ unstable( feature = "duration_float" , issue = "54361" ) ]
516
517
#[ inline]
517
- pub const fn as_float_secs ( & self ) -> f64 {
518
+ pub const fn as_secs_f64 ( & self ) -> f64 {
518
519
( self . secs as f64 ) + ( self . nanos as f64 ) / ( NANOS_PER_SEC as f64 )
519
520
}
520
521
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`.
522
542
///
523
543
/// # Panics
524
544
/// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
@@ -528,12 +548,12 @@ impl Duration {
528
548
/// #![feature(duration_float)]
529
549
/// use std::time::Duration;
530
550
///
531
- /// let dur = Duration::from_float_secs (2.7);
551
+ /// let dur = Duration::from_secs_f64 (2.7);
532
552
/// assert_eq!(dur, Duration::new(2, 700_000_000));
533
553
/// ```
534
554
#[ unstable( feature = "duration_float" , issue = "54361" ) ]
535
555
#[ inline]
536
- pub fn from_float_secs ( secs : f64 ) -> Duration {
556
+ pub fn from_secs_f64 ( secs : f64 ) -> Duration {
537
557
let nanos = secs * ( NANOS_PER_SEC as f64 ) ;
538
558
if !nanos. is_finite ( ) {
539
559
panic ! ( "got non-finite value when converting float to duration" ) ;
@@ -551,6 +571,40 @@ impl Duration {
551
571
}
552
572
}
553
573
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
+
554
608
/// Multiplies `Duration` by `f64`.
555
609
///
556
610
/// # Panics
@@ -568,7 +622,27 @@ impl Duration {
568
622
#[ unstable( feature = "duration_float" , issue = "54361" ) ]
569
623
#[ inline]
570
624
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 ( ) )
572
646
}
573
647
574
648
/// Divide `Duration` by `f64`.
@@ -589,7 +663,28 @@ impl Duration {
589
663
#[ unstable( feature = "duration_float" , issue = "54361" ) ]
590
664
#[ inline]
591
665
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)
593
688
}
594
689
595
690
/// Divide `Duration` by `Duration` and return `f64`.
@@ -605,8 +700,25 @@ impl Duration {
605
700
/// ```
606
701
#[ unstable( feature = "duration_float" , issue = "54361" ) ]
607
702
#[ 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 ( )
610
722
}
611
723
}
612
724
0 commit comments