diff options
| author | bors <bors@rust-lang.org> | 2019-03-26 17:25:16 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-03-26 17:25:16 +0000 |
| commit | fbd34efb32b9efb574899e4335bdc8c6525ac27e (patch) | |
| tree | 53ca160f6c731c05e092002bae40e03b22dae376 /src/libstd | |
| parent | 07d350897c7f95bb40ae9762ad1e945f95fc37ae (diff) | |
| parent | 822b4fcb3bb583240c47158ee91d8ed6ae805e45 (diff) | |
| download | rust-fbd34efb32b9efb574899e4335bdc8c6525ac27e.tar.gz rust-fbd34efb32b9efb574899e4335bdc8c6525ac27e.zip | |
Auto merge of #59433 - Centril:rollup, r=Centril
Rollup of 10 pull requests
Successful merges:
- #59150 (Expand suggestions for type ascription parse errors)
- #59232 (Merge `Promoted` and `Static` in `mir::Place`)
- #59267 (Provide suggestion when using field access instead of path)
- #59315 (Add no_hash to query macro and move some queries over)
- #59334 (Update build instructions in README.md)
- #59362 (Demo `FromIterator` short-circuiting)
- #59374 (Simplify checked_duration_since)
- #59389 (replace redundant note in deprecation warning)
- #59410 (Clarify `{Ord,f32,f64}::clamp` docs a little)
- #59419 (Utilize `?` instead of `return None`.)
Failed merges:
r? @ghost
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/f32.rs | 22 | ||||
| -rw-r--r-- | src/libstd/f64.rs | 21 | ||||
| -rw-r--r-- | src/libstd/sys/cloudabi/time.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sys/redox/time.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/sgx/time.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/unix/time.rs | 13 | ||||
| -rw-r--r-- | src/libstd/sys/wasm/time.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/windows/time.rs | 8 | ||||
| -rw-r--r-- | src/libstd/time.rs | 23 |
9 files changed, 60 insertions, 49 deletions
diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 688d9c1aabb..796908b0df9 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -960,17 +960,27 @@ impl f32 { pub fn atanh(self) -> f32 { 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() } - /// Returns max if self is greater than max, and min if self is less than min. - /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN. + + /// Restrict a value to a certain interval unless it is NaN. + /// + /// Returns `max` if `self` is greater than `max`, and `min` if `self` is + /// less than `min`. Otherwise this returns `self`. + /// + /// Not that this function returns NaN if the initial value was NaN as + /// well. + /// + /// # Panics + /// + /// Panics if `min > max`, `min` is NaN, or `max` is NaN. /// /// # Examples /// /// ``` /// #![feature(clamp)] - /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32); - /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32); - /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32); - /// assert!((std::f32::NAN).clamp(-2.0f32, 1.0f32).is_nan()); + /// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0); + /// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0); + /// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0); + /// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan()); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index b171e1c7ac9..e679a7d2e8c 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -882,17 +882,26 @@ impl f64 { 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() } - /// Returns max if self is greater than max, and min if self is less than min. - /// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN. + /// Restrict a value to a certain interval unless it is NaN. + /// + /// Returns `max` if `self` is greater than `max`, and `min` if `self` is + /// less than `min`. Otherwise this returns `self`. + /// + /// Not that this function returns NaN if the initial value was NaN as + /// well. + /// + /// # Panics + /// + /// Panics if `min > max`, `min` is NaN, or `max` is NaN. /// /// # Examples /// /// ``` /// #![feature(clamp)] - /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64); - /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64); - /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64); - /// assert!((std::f64::NAN).clamp(-2.0f64, 1.0f64).is_nan()); + /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0); + /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0); + /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0); + /// assert!((std::f64::NAN).clamp(-2.0, 1.0).is_nan()); /// ``` #[unstable(feature = "clamp", issue = "44095")] #[inline] diff --git a/src/libstd/sys/cloudabi/time.rs b/src/libstd/sys/cloudabi/time.rs index d7502c61eff..49a234e1158 100644 --- a/src/libstd/sys/cloudabi/time.rs +++ b/src/libstd/sys/cloudabi/time.rs @@ -33,11 +33,9 @@ impl Instant { Instant { t: 0 } } - pub fn sub_instant(&self, other: &Instant) -> Duration { - let diff = self.t - .checked_sub(other.t) - .expect("second instant is later than self"); - Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + let diff = self.t.checked_sub(other.t)?; + Some(Duration::new(diff / NSEC_PER_SEC, (diff % NSEC_PER_SEC) as u32)) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/redox/time.rs b/src/libstd/sys/redox/time.rs index 9db3e85ca9c..881ad5c0aeb 100644 --- a/src/libstd/sys/redox/time.rs +++ b/src/libstd/sys/redox/time.rs @@ -137,10 +137,8 @@ impl Instant { false } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.t.sub_timespec(&other.t).unwrap_or_else(|_| { - panic!("specified instant was later than self") - }) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.t.sub_timespec(&other.t).ok() } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/sgx/time.rs b/src/libstd/sys/sgx/time.rs index e4f789c3e36..4659f7ba71f 100644 --- a/src/libstd/sys/sgx/time.rs +++ b/src/libstd/sys/sgx/time.rs @@ -14,8 +14,8 @@ impl Instant { Instant(usercalls::insecure_time()) } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.0 - other.0 + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.0.checked_sub(other.0) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/unix/time.rs b/src/libstd/sys/unix/time.rs index cbb0615911a..6b5a89aee7d 100644 --- a/src/libstd/sys/unix/time.rs +++ b/src/libstd/sys/unix/time.rs @@ -149,12 +149,11 @@ mod inner { true } - pub fn sub_instant(&self, other: &Instant) -> Duration { + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + let diff = self.t.checked_sub(other.t)?; let info = info(); - let diff = self.t.checked_sub(other.t) - .expect("second instant is later than self"); let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64); - Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32) + Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32)) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { @@ -285,10 +284,8 @@ mod inner { false // last clause, used so `||` is always trailing above } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.t.sub_timespec(&other.t).unwrap_or_else(|_| { - panic!("specified instant was later than self") - }) + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.t.sub_timespec(&other.t).ok() } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/wasm/time.rs b/src/libstd/sys/wasm/time.rs index c1228a1b75e..3f71461eea4 100644 --- a/src/libstd/sys/wasm/time.rs +++ b/src/libstd/sys/wasm/time.rs @@ -22,8 +22,8 @@ impl Instant { false } - pub fn sub_instant(&self, other: &Instant) -> Duration { - self.0 - other.0 + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { + self.0.checked_sub(other.0) } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index 2c99bca7009..aa53f1194fd 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -49,17 +49,17 @@ impl Instant { Instant { t: Duration::from_secs(0) } } - pub fn sub_instant(&self, other: &Instant) -> Duration { + pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { // On windows there's a threshold below which we consider two timestamps // equivalent due to measurement error. For more details + doc link, // check the docs on epsilon. let epsilon = perf_counter::PerformanceCounterInstant::epsilon(); if other.t > self.t && other.t - self.t <= epsilon { - return Duration::new(0, 0) + Some(Duration::new(0, 0)) + } else { + self.t.checked_sub(other.t) } - self.t.checked_sub(other.t) - .expect("specified instant was later than self") } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 4c86f70ad87..ab1a43d6672 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -212,7 +212,7 @@ impl Instant { /// ``` #[stable(feature = "time2", since = "1.8.0")] pub fn duration_since(&self, earlier: Instant) -> Duration { - self.0.sub_instant(&earlier.0) + self.0.checked_sub_instant(&earlier.0).expect("supplied instant is later than self") } /// Returns the amount of time elapsed from another instant to this one, @@ -233,11 +233,7 @@ impl Instant { /// ``` #[unstable(feature = "checked_duration_since", issue = "58402")] pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> { - if self >= &earlier { - Some(self.0.sub_instant(&earlier.0)) - } else { - None - } + self.0.checked_sub_instant(&earlier.0) } /// Returns the amount of time elapsed from another instant to this one, @@ -664,20 +660,23 @@ mod tests { #[test] #[should_panic] - fn instant_duration_panic() { + fn instant_duration_since_panic() { let a = Instant::now(); (a - Duration::new(1, 0)).duration_since(a); } #[test] - fn checked_instant_duration_nopanic() { - let a = Instant::now(); - let ret = (a - Duration::new(1, 0)).checked_duration_since(a); - assert_eq!(ret, None); + fn instant_checked_duration_since_nopanic() { + let now = Instant::now(); + let earlier = now - Duration::new(1, 0); + let later = now + Duration::new(1, 0); + assert_eq!(earlier.checked_duration_since(now), None); + assert_eq!(later.checked_duration_since(now), Some(Duration::new(1, 0))); + assert_eq!(now.checked_duration_since(now), Some(Duration::new(0, 0))); } #[test] - fn saturating_instant_duration_nopanic() { + fn instant_saturating_duration_since_nopanic() { let a = Instant::now(); let ret = (a - Duration::new(1, 0)).saturating_duration_since(a); assert_eq!(ret, Duration::new(0,0)); |
