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

Make Duration constructors and accessors const fns #33033

Closed
wants to merge 1 commit 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
24 changes: 13 additions & 11 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,28 @@ impl Duration {
/// in a second), then it will carry over into the seconds provided.
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn new(secs: u64, nanos: u32) -> Duration {
let secs = secs + (nanos / NANOS_PER_SEC) as u64;
let nanos = nanos % NANOS_PER_SEC;
Duration { secs: secs, nanos: nanos }
pub const fn new(secs: u64, nanos: u32) -> Duration {
Duration {
secs: secs + (nanos / NANOS_PER_SEC) as u64,
nanos: nanos % NANOS_PER_SEC,
}
}

/// 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 All @@ -81,7 +83,7 @@ impl Duration {
/// nanoseconds are not represented in the returned value).
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn as_secs(&self) -> u64 { self.secs }
pub const fn as_secs(&self) -> u64 { self.secs }
Copy link
Contributor

Choose a reason for hiding this comment

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

These kind of const fns only work in statics, they don't work in array lengths and other true constants (yet). Is there a use case for this? If not, I suggest to delay this until we have a unified MIR-powered const evaluator


/// Returns the nanosecond precision represented by this duration.
///
Expand All @@ -90,7 +92,7 @@ impl Duration {
/// fractional portion of a second (e.g. it is less than one billion).
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn subsec_nanos(&self) -> u32 { self.nanos }
pub const fn subsec_nanos(&self) -> u32 { self.nanos }
}

#[stable(feature = "duration", since = "1.3.0")]
Expand Down