Skip to content

Commit

Permalink
Don't call Duration::new unnecessarily in Duration::from_secs
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed May 17, 2024
1 parent b30cbc8 commit e703bb1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ const DAYS_PER_WEEK: u64 = 7;
#[rustc_layout_scalar_valid_range_end(999_999_999)]
struct Nanoseconds(u32);

impl Nanoseconds {
// SAFETY: 0 is within the valid range
const ZERO: Self = unsafe { Nanoseconds(0) };
}

impl Default for Nanoseconds {
#[inline]
fn default() -> Self {
// SAFETY: 0 is within the valid range
unsafe { Nanoseconds(0) }
Self::ZERO
}
}

Expand Down Expand Up @@ -236,7 +240,7 @@ impl Duration {
#[inline]
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
pub const fn from_secs(secs: u64) -> Duration {
Duration::new(secs, 0)
Duration { secs, nanos: Nanoseconds::ZERO }
}

/// Creates a new `Duration` from the specified number of milliseconds.
Expand Down

0 comments on commit e703bb1

Please sign in to comment.