mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Duration::zero() -> Duration::ZERO
Duration::ZERO composes better with match and various other things, at the cost of an occasional parens, and results in less work for the optimizer, so let's use that instead.
This commit is contained in:
parent
d72d5f48c2
commit
ef027a1eed
@ -108,6 +108,21 @@ impl Duration {
|
||||
#[unstable(feature = "duration_constants", issue = "57391")]
|
||||
pub const NANOSECOND: Duration = Duration::from_nanos(1);
|
||||
|
||||
/// A duration of zero time.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(duration_zero)]
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// let duration = Duration::ZERO;
|
||||
/// assert!(duration.is_zero());
|
||||
/// assert_eq!(duration.as_nanos(), 0);
|
||||
/// ```
|
||||
#[unstable(feature = "duration_zero", issue = "73544")]
|
||||
pub const ZERO: Duration = Duration::from_nanos(0);
|
||||
|
||||
/// The minimum duration.
|
||||
///
|
||||
/// # Examples
|
||||
@ -166,24 +181,6 @@ impl Duration {
|
||||
Duration { secs, nanos }
|
||||
}
|
||||
|
||||
/// Creates a new `Duration` that spans no time.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(duration_zero)]
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// let duration = Duration::zero();
|
||||
/// assert!(duration.is_zero());
|
||||
/// assert_eq!(duration.as_nanos(), 0);
|
||||
/// ```
|
||||
#[unstable(feature = "duration_zero", issue = "73544")]
|
||||
#[inline]
|
||||
pub const fn zero() -> Duration {
|
||||
Duration { secs: 0, nanos: 0 }
|
||||
}
|
||||
|
||||
/// Creates a new `Duration` from the specified number of whole seconds.
|
||||
///
|
||||
/// # Examples
|
||||
@ -277,7 +274,7 @@ impl Duration {
|
||||
/// #![feature(duration_zero)]
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// assert!(Duration::zero().is_zero());
|
||||
/// assert!(Duration::ZERO.is_zero());
|
||||
/// assert!(Duration::new(0, 0).is_zero());
|
||||
/// assert!(Duration::from_nanos(0).is_zero());
|
||||
/// assert!(Duration::from_secs(0).is_zero());
|
||||
|
@ -108,26 +108,24 @@ fn sub() {
|
||||
|
||||
#[test]
|
||||
fn checked_sub() {
|
||||
let zero = Duration::zero();
|
||||
assert_eq!(Duration::NANOSECOND.checked_sub(zero), Some(Duration::NANOSECOND));
|
||||
assert_eq!(Duration::NANOSECOND.checked_sub(Duration::ZERO), Some(Duration::NANOSECOND));
|
||||
assert_eq!(
|
||||
Duration::SECOND.checked_sub(Duration::NANOSECOND),
|
||||
Some(Duration::new(0, 999_999_999))
|
||||
);
|
||||
assert_eq!(zero.checked_sub(Duration::NANOSECOND), None);
|
||||
assert_eq!(zero.checked_sub(Duration::SECOND), None);
|
||||
assert_eq!(Duration::ZERO.checked_sub(Duration::NANOSECOND), None);
|
||||
assert_eq!(Duration::ZERO.checked_sub(Duration::SECOND), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saturating_sub() {
|
||||
let zero = Duration::new(0, 0);
|
||||
assert_eq!(Duration::NANOSECOND.saturating_sub(zero), Duration::NANOSECOND);
|
||||
assert_eq!(Duration::NANOSECOND.saturating_sub(Duration::ZERO), Duration::NANOSECOND);
|
||||
assert_eq!(
|
||||
Duration::SECOND.saturating_sub(Duration::NANOSECOND),
|
||||
Duration::new(0, 999_999_999)
|
||||
);
|
||||
assert_eq!(zero.saturating_sub(Duration::NANOSECOND), Duration::MIN);
|
||||
assert_eq!(zero.saturating_sub(Duration::SECOND), Duration::MIN);
|
||||
assert_eq!(Duration::ZERO.saturating_sub(Duration::NANOSECOND), Duration::MIN);
|
||||
assert_eq!(Duration::ZERO.saturating_sub(Duration::SECOND), Duration::MIN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -339,10 +337,7 @@ fn duration_const() {
|
||||
const SUB_SEC_NANOS: u32 = DURATION.subsec_nanos();
|
||||
assert_eq!(SUB_SEC_NANOS, 123_456_789);
|
||||
|
||||
const ZERO: Duration = Duration::zero();
|
||||
assert_eq!(ZERO, Duration::new(0, 0));
|
||||
|
||||
const IS_ZERO: bool = ZERO.is_zero();
|
||||
const IS_ZERO: bool = Duration::ZERO.is_zero();
|
||||
assert!(IS_ZERO);
|
||||
|
||||
const SECONDS: u64 = Duration::SECOND.as_secs();
|
||||
@ -386,7 +381,7 @@ fn duration_const() {
|
||||
const CHECKED_ADD: Option<Duration> = MAX.checked_add(Duration::SECOND);
|
||||
assert_eq!(CHECKED_ADD, None);
|
||||
|
||||
const CHECKED_SUB: Option<Duration> = ZERO.checked_sub(Duration::SECOND);
|
||||
const CHECKED_SUB: Option<Duration> = (Duration::ZERO).checked_sub(Duration::SECOND);
|
||||
assert_eq!(CHECKED_SUB, None);
|
||||
|
||||
const CHECKED_MUL: Option<Duration> = Duration::SECOND.checked_mul(1);
|
||||
@ -416,8 +411,8 @@ fn duration_const() {
|
||||
const SATURATING_ADD: Duration = MAX.saturating_add(Duration::SECOND);
|
||||
assert_eq!(SATURATING_ADD, MAX);
|
||||
|
||||
const SATURATING_SUB: Duration = ZERO.saturating_sub(Duration::SECOND);
|
||||
assert_eq!(SATURATING_SUB, ZERO);
|
||||
const SATURATING_SUB: Duration = (Duration::ZERO).saturating_sub(Duration::SECOND);
|
||||
assert_eq!(SATURATING_SUB, Duration::ZERO);
|
||||
|
||||
const SATURATING_MUL: Duration = MAX.saturating_mul(2);
|
||||
assert_eq!(SATURATING_MUL, MAX);
|
||||
|
@ -75,14 +75,14 @@ fn instant_checked_duration_since_nopanic() {
|
||||
let later = now + Duration::SECOND;
|
||||
assert_eq!(earlier.checked_duration_since(now), None);
|
||||
assert_eq!(later.checked_duration_since(now), Some(Duration::SECOND));
|
||||
assert_eq!(now.checked_duration_since(now), Some(Duration::zero()));
|
||||
assert_eq!(now.checked_duration_since(now), Some(Duration::ZERO));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instant_saturating_duration_since_nopanic() {
|
||||
let a = Instant::now();
|
||||
let ret = (a - Duration::SECOND).saturating_duration_since(a);
|
||||
assert_eq!(ret, Duration::zero());
|
||||
assert_eq!(ret, Duration::ZERO);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -90,7 +90,7 @@ fn system_time_math() {
|
||||
let a = SystemTime::now();
|
||||
let b = SystemTime::now();
|
||||
match b.duration_since(a) {
|
||||
Ok(dur) if dur == Duration::zero() => {
|
||||
Ok(Duration::ZERO) => {
|
||||
assert_almost_eq!(a, b);
|
||||
}
|
||||
Ok(dur) => {
|
||||
|
Loading…
Reference in New Issue
Block a user