diff options
| author | bors <bors@rust-lang.org> | 2018-09-20 22:47:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-09-20 22:47:34 +0000 |
| commit | 2fa1390f6c1ca36b0cc1c126e6d295d360f42b6c (patch) | |
| tree | 92f894274de68c2b4bf0a7789ddb79848d1a378d /src/libcore | |
| parent | 3bc2ca7e4f8507f82a4c357ee19300166bcd8099 (diff) | |
| parent | ec085962c6f511c654575522309ef832e6d3160f (diff) | |
| download | rust-2fa1390f6c1ca36b0cc1c126e6d295d360f42b6c.tar.gz rust-2fa1390f6c1ca36b0cc1c126e6d295d360f42b6c.zip | |
Auto merge of #54389 - kennytm:rollup, r=kennytm
Rollup of 15 pull requests Successful merges: - #52813 (Duration div mul extras) - #53470 (Warn about metadata loader errors) - #54233 (Remove LLVM 3.9 workaround.) - #54257 (Switch wasm math symbols to their original names) - #54258 (Enable fatal warnings for the wasm32 linker) - #54266 (Update LLVM to fix "bool" arguments on PPC32) - #54290 (Switch linker for aarch64-pc-windows-msvc from LLD to MSVC) - #54292 (Suggest array indexing when tuple indexing on an array) - #54295 (A few cleanups and minor improvements to rustc/traits) - #54298 (miri: correctly compute expected alignment for field) - #54333 (Update The Book to latest) - #54337 (Remove unneeded clone() from tests in librustdoc) - #54346 (rustc: future-proof error reporting for polymorphic constants in types.) - #54362 (Pass --batch to gdb) - #54367 (Add regression test for thread local static mut borrows)
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/time.rs | 121 |
1 files changed, 120 insertions, 1 deletions
diff --git a/src/libcore/time.rs b/src/libcore/time.rs index b58920224eb..1aed5a7b426 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -21,7 +21,7 @@ //! assert_eq!(Duration::new(5, 0), Duration::from_secs(5)); //! ``` -use fmt; +use {fmt, u64}; use iter::Sum; use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign}; @@ -30,6 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000; const NANOS_PER_MICRO: u32 = 1_000; const MILLIS_PER_SEC: u64 = 1_000; const MICROS_PER_SEC: u64 = 1_000_000; +const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64; /// A `Duration` type to represent a span of time, typically used for system /// timeouts. @@ -458,6 +459,115 @@ impl Duration { None } } + + /// Returns the number of seconds contained by this `Duration` as `f64`. + /// + /// The returned value does include the fractional (nanosecond) part of the duration. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.as_float_secs(), 2.7); + /// ``` + #[unstable(feature = "duration_float", issue = "54361")] + #[inline] + pub fn as_float_secs(&self) -> f64 { + (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64) + } + + /// Creates a new `Duration` from the specified number of seconds. + /// + /// # Panics + /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::from_float_secs(2.7); + /// assert_eq!(dur, Duration::new(2, 700_000_000)); + /// ``` + #[unstable(feature = "duration_float", issue = "54361")] + #[inline] + pub fn from_float_secs(secs: f64) -> Duration { + let nanos = secs * (NANOS_PER_SEC as f64); + if !nanos.is_finite() { + panic!("got non-finite value when converting float to duration"); + } + if nanos >= MAX_NANOS_F64 { + panic!("overflow when converting float to duration"); + } + if nanos < 0.0 { + panic!("underflow when converting float to duration"); + } + let nanos = nanos as u128; + Duration { + secs: (nanos / (NANOS_PER_SEC as u128)) as u64, + nanos: (nanos % (NANOS_PER_SEC as u128)) as u32, + } + } + + /// Multiply `Duration` by `f64`. + /// + /// # Panics + /// This method will panic if result is not finite, negative or overflows `Duration`. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000)); + /// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0)); + /// ``` + #[unstable(feature = "duration_float", issue = "54361")] + #[inline] + pub fn mul_f64(self, rhs: f64) -> Duration { + Duration::from_float_secs(rhs * self.as_float_secs()) + } + + /// Divide `Duration` by `f64`. + /// + /// # Panics + /// This method will panic if result is not finite, negative or overflows `Duration`. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur = Duration::new(2, 700_000_000); + /// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611)); + /// // note that truncation is used, not rounding + /// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598)); + /// ``` + #[unstable(feature = "duration_float", issue = "54361")] + #[inline] + pub fn div_f64(self, rhs: f64) -> Duration { + Duration::from_float_secs(self.as_float_secs() / rhs) + } + + /// Divide `Duration` by `Duration` and return `f64`. + /// + /// # Examples + /// ``` + /// #![feature(duration_float)] + /// use std::time::Duration; + /// + /// let dur1 = Duration::new(2, 700_000_000); + /// let dur2 = Duration::new(5, 400_000_000); + /// assert_eq!(dur1.div_duration(dur2), 0.5); + /// ``` + #[unstable(feature = "duration_float", issue = "54361")] + #[inline] + pub fn div_duration(self, rhs: Duration) -> f64 { + self.as_float_secs() / rhs.as_float_secs() + } } #[stable(feature = "duration", since = "1.3.0")] @@ -501,6 +611,15 @@ impl Mul<u32> for Duration { } } +#[stable(feature = "symmetric_u32_duration_mul", since = "1.31.0")] +impl Mul<Duration> for u32 { + type Output = Duration; + + fn mul(self, rhs: Duration) -> Duration { + rhs * self + } +} + #[stable(feature = "time_augmented_assignment", since = "1.9.0")] impl MulAssign<u32> for Duration { fn mul_assign(&mut self, rhs: u32) { |
