-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 as_nanos function to Duration #50167
Conversation
r? @aidanhs (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
src/libcore/time.rs
Outdated
/// let duration = Duration::new(5, 730023852); | ||
/// assert_eq!(duration.as_nanos(), 5730023852); | ||
/// ``` | ||
#[unstable(feature = "duration_nanos", issue = "50167")] |
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.
The tracking issue number should be a new issue, not this PR.
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.
Done.
Reassigning this to someone from the libs team. |
We should add similar accessors for milliseconds and microseconds at the same time for consistency with the subsec methods. cc @rust-lang/libs |
@sfackler I can make that change. Any thoughts on what the feature should be called then? "duration_as_nanos" isn't really a great name if it also adds as_millis() and as_micros(). Perhaps "duration_as_u128"? |
Sure, seems reasonable. |
Ping from triage @fintelia! It's been a while since we heard from you, will you have time to work on this again soon? |
Sorry about that. I'm probably going to be busy for about a week longer, so if someone else wants to take over making those changes, feel free |
@fintelia don't worry, we just want to make sure you still remember about the PR |
src/libcore/time.rs
Outdated
#[unstable(feature = "duration_as_u128", issue = "50202")] | ||
#[inline] | ||
pub fn as_millis(&self) -> u128 { | ||
self.secs as u128 * MILLIS_PER_SEC as u128 + self.nanos as u128 / NANOS_PER_MILLI as u128 |
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.
Nit: self.nanos as u128 / NANOS_PER_MILLI as u128
should be replaced with (self.nanos / NANOS_PER_MILLI) as u128
. The former requires doing 128-bit arithmetic on a 32-bit number, which is redundant.
src/libcore/time.rs
Outdated
#[unstable(feature = "duration_as_u128", issue = "50202")] | ||
#[inline] | ||
pub fn as_micros(&self) -> u128 { | ||
self.secs as u128 * MICROS_PER_SEC as u128 + self.nanos as u128 / NANOS_PER_MICRO as u128 |
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.
See comment from as_nanos
#[unstable(feature = "duration_as_u128", issue = "50202")] | ||
#[inline] | ||
pub fn as_nanos(&self) -> u128 { | ||
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos as u128 |
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.
See comment from as_nanos
Ping from triage, @sfackler ! |
Oops - seems good to me! @bors r+ |
📌 Commit fc89566 has been approved by |
Add as_nanos function to Duration Duration has historically lacked a way to get the actual number of nanoseconds it contained as a normal Rust type because u64 was of insufficient range, and f64 of insufficient precision. The u128 type solves both issues, so I propose adding an `as_nanos` function to expose the capability.
Rollup of 6 pull requests Successful merges: - #50167 ( Add as_nanos function to Duration) - #50919 (Provide more context for what the {f32,f64}::EPSILON values represent.) - #51124 (Reword {ptr,mem}::replace docs.) - #51147 (Stabilize SliceIndex trait.) - #51291 (Fix typos of ‘ambiguous’) - #51302 (Permit building rustdoc without compiler artifacts) Failed merges:
💥 Test timed out |
Duration has historically lacked a way to get the actual number of nanoseconds it contained as a normal Rust type because u64 was of insufficient range, and f64 of insufficient precision. The u128 type solves both issues, so I propose adding an
as_nanos
function to expose the capability.