Skip to content

Commit 379c40f

Browse files
committed
Make Duration constructors and accessors const fns
1 parent 6fa61b8 commit 379c40f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/libstd/time/duration.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,28 @@ impl Duration {
5353
/// in a second), then it will carry over into the seconds provided.
5454
#[stable(feature = "duration", since = "1.3.0")]
5555
#[inline]
56-
pub fn new(secs: u64, nanos: u32) -> Duration {
57-
let secs = secs + (nanos / NANOS_PER_SEC) as u64;
58-
let nanos = nanos % NANOS_PER_SEC;
59-
Duration { secs: secs, nanos: nanos }
56+
pub const fn new(secs: u64, nanos: u32) -> Duration {
57+
Duration {
58+
secs: secs + (nanos / NANOS_PER_SEC) as u64,
59+
nanos: nanos % NANOS_PER_SEC,
60+
}
6061
}
6162

6263
/// Creates a new `Duration` from the specified number of seconds.
6364
#[stable(feature = "duration", since = "1.3.0")]
6465
#[inline]
65-
pub fn from_secs(secs: u64) -> Duration {
66+
pub const fn from_secs(secs: u64) -> Duration {
6667
Duration { secs: secs, nanos: 0 }
6768
}
6869

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

7880
/// Returns the number of whole seconds represented by this duration.
@@ -81,7 +83,7 @@ impl Duration {
8183
/// nanoseconds are not represented in the returned value).
8284
#[stable(feature = "duration", since = "1.3.0")]
8385
#[inline]
84-
pub fn as_secs(&self) -> u64 { self.secs }
86+
pub const fn as_secs(&self) -> u64 { self.secs }
8587

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

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

0 commit comments

Comments
 (0)