Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time units #52556

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#![feature(const_slice_len)]
#![feature(const_str_as_bytes)]
#![feature(const_str_len)]
#![feature(time_units)]

#[prelude_import]
#[allow(unused)]
Expand Down
25 changes: 25 additions & 0 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ const NANOS_PER_MICRO: u32 = 1_000;
const MILLIS_PER_SEC: u64 = 1_000;
const MICROS_PER_SEC: u64 = 1_000_000;

/// 1 nanosecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
pub const NANOSECOND: Duration = Duration::from_nanos(1);
/// 1 microsecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
pub const MICROSECOND: Duration = Duration::from_micros(1);
/// 1 millisecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
pub const MILLISECOND: Duration = Duration::from_millis(1);
/// 1 second `Duration`
#[unstable(feature = "time_units", issue = "0")]
pub const SECOND: Duration = Duration::from_secs(1);
/// 1 minute `Duration`
#[unstable(feature = "time_units", issue = "0")]
pub const MINUTE: Duration = Duration::from_secs(60);

/// A `Duration` type to represent a span of time, typically used for system
/// timeouts.
///
Expand Down Expand Up @@ -501,6 +517,15 @@ impl Mul<u32> for Duration {
}
}

#[unstable(feature = "time_units", issue = "0")]
impl Mul<Duration> for u32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This impl is going to be insta-stable unfortunately.

Copy link
Contributor Author

@newpavlov newpavlov Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I keep the attribute or use instead something like #[stable(feature = "time_units", since = "1.29.0")]?

Copy link
Member

@kennytm kennytm Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@newpavlov The latter (with a different feature name, otherwise tidy will reject it).

Copy link
Contributor Author

@newpavlov newpavlov Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what should I use for feature then? With time_units it can not be built.

UPD: Ah, haven't noticed your update. :)

type Output = Duration;

fn mul(self, rhs: Duration) -> Duration {
rhs.checked_mul(self).expect("overflow when multiplying scalar by duration")
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
#![feature(float_internals)]
#![feature(panic_info_message)]
#![feature(panic_implementation)]
#![feature(time_units)]

#![default_lib_allocator]

Expand Down
2 changes: 2 additions & 0 deletions src/libstd/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use sys_common::FromInner;

#[stable(feature = "time", since = "1.3.0")]
pub use core::time::Duration;
#[unstable(feature = "time_units", issue = "0")]
pub use core::time::{NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE};

/// A measurement of a monotonically nondecreasing clock.
/// Opaque and useful only with `Duration`.
Expand Down