-
Notifications
You must be signed in to change notification settings - Fork 13.8k
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
Add Duration::from_nanos_u128
#139243
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -308,6 +308,39 @@ impl Duration { | |||||||
Duration { secs, nanos: subsec_nanos } | ||||||||
} | ||||||||
|
||||||||
/// 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, | ||||||||
/// which is `(u64::MAX * NANOS_PER_SEC) + NANOS_PER_SEC - 1` | ||||||||
Comment on lines
+315
to
+316
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Technically we say this can vary by platform, so we don't need to give specifics. |
||||||||
/// Use this function if you need to specify time greater than what can fit in u64 | ||||||||
/// (around 584 years). | ||||||||
Comment on lines
+317
to
+318
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this is reasonably straightforward |
||||||||
/// | ||||||||
/// # Examples | ||||||||
/// | ||||||||
/// ``` | ||||||||
/// #![feature(duration_from_nanos_u128)] | ||||||||
/// use std::time::Duration; | ||||||||
omanirudh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
/// let time_in_nanos = 2u128.pow(64); | ||||||||
/// let duration = Duration::from_nanos_u128(time_in_nanos); | ||||||||
/// ``` | ||||||||
omanirudh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
#[unstable(feature = "duration_from_nanos_u128", issue = "139201")] | ||||||||
#[must_use] | ||||||||
#[inline] | ||||||||
pub const fn from_nanos_u128(nanos: u128) -> Duration { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Makes the panic message show where this was called, rather than reporting std's |
||||||||
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"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
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 | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to add |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,13 @@ fn from_weeks_overflow() { | |
let _ = Duration::from_weeks(overflow); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn from_nanos_u128_overflow() { | ||
let overflow = (u64::MAX * NANOS_PER_SEC) + (NANOS_PER_SEC - 1) + 1; | ||
let _ = Duration::from_nanos_u128(overflow); | ||
} | ||
|
||
#[test] | ||
fn constructor_weeks() { | ||
assert_eq!(Duration::from_weeks(1), Duration::from_secs(7 * 24 * 60 * 60)); | ||
|
@@ -81,6 +88,8 @@ fn secs() { | |
assert_eq!(Duration::from_micros(1_000_001).as_secs(), 1); | ||
assert_eq!(Duration::from_nanos(999_999_999).as_secs(), 0); | ||
assert_eq!(Duration::from_nanos(1_000_000_001).as_secs(), 1); | ||
assert_eq!(Duration::from_nanos_u128(999_999_999).as_secs(), 0); | ||
assert_eq!(Duration::from_nanos_u128(1_000_000_001).as_secs(), 1); | ||
} | ||
|
||
#[test] | ||
|
@@ -95,6 +104,8 @@ fn millis() { | |
assert_eq!(Duration::from_micros(1_001_000).subsec_millis(), 1); | ||
assert_eq!(Duration::from_nanos(999_999_999).subsec_millis(), 999); | ||
assert_eq!(Duration::from_nanos(1_001_000_000).subsec_millis(), 1); | ||
assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_millis(), 999); | ||
assert_eq!(Duration::from_nanos_u128(1_001_000_001).subsec_millis(), 1); | ||
} | ||
|
||
#[test] | ||
|
@@ -109,6 +120,8 @@ fn micros() { | |
assert_eq!(Duration::from_micros(1_000_001).subsec_micros(), 1); | ||
assert_eq!(Duration::from_nanos(999_999_999).subsec_micros(), 999_999); | ||
assert_eq!(Duration::from_nanos(1_000_001_000).subsec_micros(), 1); | ||
assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_micros(), 999_999); | ||
assert_eq!(Duration::from_nanos_u128(1_000_001_000).subsec_micros(), 1); | ||
} | ||
|
||
#[test] | ||
|
@@ -123,6 +136,8 @@ fn nanos() { | |
assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000); | ||
assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999); | ||
assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1); | ||
assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_nanos(), 999_999_999); | ||
assert_eq!(Duration::from_nanos_u128(1_000_000_001).subsec_nanos(), 1); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add a test to |
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.