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

Const-ify certain methods on std::time::Duration #34918

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const MILLIS_PER_SEC: u64 = 1_000;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Duration {
secs: u64,
nanos: u32, // Always 0 <= nanos < NANOS_PER_SEC
/// Always 0 <= nanos < NANOS_PER_SEC
nanos: u32,
}

impl Duration {
Expand All @@ -68,17 +69,18 @@ impl Duration {
/// Creates a new `Duration` from the specified number of seconds.
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn from_secs(secs: u64) -> Duration {
pub const fn from_secs(secs: u64) -> Duration {
Duration { secs: secs, nanos: 0 }
}

/// Creates a new `Duration` from the specified number of milliseconds.
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn from_millis(millis: u64) -> Duration {
let secs = millis / MILLIS_PER_SEC;
let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI;
Duration { secs: secs, nanos: nanos }
pub const fn from_millis(millis: u64) -> Duration {
Duration {
secs: millis / MILLIS_PER_SEC
nanos: ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI
}
}

/// Returns the number of whole seconds represented by this duration.
Expand Down