Skip to content

Commit 6dea840

Browse files
authored
Unrolled build for rust-lang#122479
Rollup merge of rust-lang#122479 - GrigorenkoPV:duration_millis_float, r=scottmcm Implement `Duration::as_millis_{f64,f32}` Implementation of rust-lang#122451. Linked const-unstability to rust-lang#72440, so the post there should probably be updated to mentions the 2 new methods when/if this PR is merged.
2 parents f4b771b + f2ec0d3 commit 6dea840

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/core/src/time.rs

+42
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,48 @@ impl Duration {
856856
(self.secs as f32) + (self.nanos.0 as f32) / (NANOS_PER_SEC as f32)
857857
}
858858

859+
/// Returns the number of milliseconds contained by this `Duration` as `f64`.
860+
///
861+
/// The returned value does include the fractional (nanosecond) part of the duration.
862+
///
863+
/// # Examples
864+
/// ```
865+
/// #![feature(duration_millis_float)]
866+
/// use std::time::Duration;
867+
///
868+
/// let dur = Duration::new(2, 345_678_000);
869+
/// assert_eq!(dur.as_millis_f64(), 2345.678);
870+
/// ```
871+
#[unstable(feature = "duration_millis_float", issue = "122451")]
872+
#[must_use]
873+
#[inline]
874+
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
875+
pub const fn as_millis_f64(&self) -> f64 {
876+
(self.secs as f64) * (MILLIS_PER_SEC as f64)
877+
+ (self.nanos.0 as f64) / (NANOS_PER_MILLI as f64)
878+
}
879+
880+
/// Returns the number of milliseconds contained by this `Duration` as `f32`.
881+
///
882+
/// The returned value does include the fractional (nanosecond) part of the duration.
883+
///
884+
/// # Examples
885+
/// ```
886+
/// #![feature(duration_millis_float)]
887+
/// use std::time::Duration;
888+
///
889+
/// let dur = Duration::new(2, 345_678_000);
890+
/// assert_eq!(dur.as_millis_f32(), 2345.678);
891+
/// ```
892+
#[unstable(feature = "duration_millis_float", issue = "122451")]
893+
#[must_use]
894+
#[inline]
895+
#[rustc_const_unstable(feature = "duration_consts_float", issue = "72440")]
896+
pub const fn as_millis_f32(&self) -> f32 {
897+
(self.secs as f32) * (MILLIS_PER_SEC as f32)
898+
+ (self.nanos.0 as f32) / (NANOS_PER_MILLI as f32)
899+
}
900+
859901
/// Creates a new `Duration` from the specified number of seconds represented
860902
/// as `f64`.
861903
///

0 commit comments

Comments
 (0)