Skip to content

Add Duration from nanos u128 #139243

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion library/core/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![stable(feature = "duration_core", since = "1.25.0")]

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a spurious removal

//! Temporal quantification.
//!
//! # Examples:
Expand Down Expand Up @@ -308,6 +307,39 @@ impl Duration {
Duration { secs, nanos: subsec_nanos }
}

/// Creates a new Duration from the specified number of nanoseconds.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Creates a new Duration from the specified number of nanoseconds.
/// Creates a new `Duration` from the specified number of nanoseconds.

///
/// # Panics
///
/// Panics if the given number of nanoseconds is greater than what Duration can handle,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Panics if the given number of nanoseconds is greater than what Duration can handle,
/// Panics if the given number of nanoseconds is greater than what `Duration` can handle,

/// which is `(u64::MAX * NANOS_PER_SEC) + NANOS_PER_SEC - 1`
/// Use this function if you need to specify time greater than what can fit in u64
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Use this function if you need to specify time greater than what can fit in u64
/// Use this function if you need to specify time greater than what can fit in `u64`

/// (around 584 years).
///
/// # Examples
///
/// ```
/// #![feature(duration_from_nanos_u128)]
/// use std::time::Duration;
/// let time_in_nanos = 2u128.pow(64);
/// let duration = Duration::from_nanos_u128(time_in_nanos);
/// ```
#[unstable(feature = "duration_from_nanos_u128", issue = "139201")]
#[must_use]
#[inline]
pub const fn from_nanos_u128(nanos: u128) -> Duration {
const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
let secs: u128 = nanos / NANOS_PER_SEC;
if secs > u64::MAX as u128 {
panic!("overflow in duration in Duration::from_nanos_u128");
}
let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
// SAFETY: x % 1_000_000_000 < 1_000_000_000 also, subsec_nanos >= 0 since u128 >=0 and u32 >=0
let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };

Duration { secs: secs as u64, nanos: subsec_nanos }
}

/// Creates a new `Duration` from the specified number of weeks.
///
/// # Panics
Expand Down
Loading