Skip to content
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

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

omanirudh
Copy link

@omanirudh omanirudh commented Apr 2, 2025

What does this PR do?

This draft PR adds the Duration::from_nanos_u128 function to handle durations that exceed the range of u64, allowing for time spans greater than ~584 years.

Motivation

The current Duration API does not support creating durations from nanoseconds represented as u128. This addition addresses that limitation.

Tracking Issue

Fixes #139201

Details

  • Introduced Duration::from_nanos_u128 as a const fn similar to other functions in the file.

  • Ensured safety by validating the nanoseconds before using unsafe code.

  • To do : complete the documentation and examples for the new function.

    r? @RalfJung

@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 2, 2025
@rustbot rustbot assigned RalfJung and unassigned tgross35 Apr 2, 2025
@rust-log-analyzer

This comment has been minimized.

Add unstable attribute for `from_nanos_u128` with tracking issue rust-lang#139201

Co-authored-by: Madhav Madhusoodanan <[email protected]>
@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Apr 2, 2025

Thanks for taking this over!
However, this needs a libs reviewer. I just proposed the API. :)

r? libs

@rustbot rustbot assigned joboet and unassigned RalfJung Apr 2, 2025
@omanirudh
Copy link
Author

omanirudh commented Apr 2, 2025

Thanks for taking this over! However, this needs a libs reviewer. I just proposed the API. :)

r? libs

Happy to take this up. noted.

Comment on lines 312 to 314
/// creates a new Duration from the specified number of nano seconds.
/// Use this function if you need to specify time greater than what can fit in u64
/// which is around 584 years and above 584.94 years to be a little more precise.
Copy link
Contributor

Choose a reason for hiding this comment

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

    /// Creates a new Duration from the specified number of nanoseconds.
    ///
    /// Use this function if you need to specify time greater than what can fit in u64 
    /// (around 584 years).

Nanoseconds is one word, summary line needs to be separate from the rest of the description, and I don't think we need to say the number of years twice.

Comment on lines 318 to 323
/// ```
/// use std::time::Duration;
/// let time_in_nanos = 2.pow(64);
/// let duration = Duration::from_nanos_u128(time_in_nanos);
///
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs the #![feature(...)] gate to work, also has a trailing newline.

Copy link
Author

Choose a reason for hiding this comment

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

Hey @tgross35 thanks for the review.
will make changes and get get back.

@tgross35
Copy link
Contributor

tgross35 commented Apr 2, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

- add feature gate
- remove trailing whitespace
@rust-log-analyzer

This comment has been minimized.

Co-authored-by: Madhav Madhusoodanan <[email protected]>
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Co-authored-by: Madhav Madhusoodanan <[email protected]>
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] build_script_build test:false 0.172
[RUSTC-TIMING] cc test:false 0.572
   Compiling compiler_builtins v0.1.152
[RUSTC-TIMING] build_script_build test:false 0.279
error[E0015]: cannot call non-const associated function `<u64 as TryFrom<u128>>::try_from` in constant functions
   --> library/core/src/time.rs:327:20
    |
327 |         let secs = u64::try_from(nanos / NANOS_PER_SEC).unwrap_or(u64::MAX);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const method `result::Result::<u64, TryFromIntError>::unwrap_or` in constant functions
   --> library/core/src/time.rs:327:57
    |
327 |         let secs = u64::try_from(nanos / NANOS_PER_SEC).unwrap_or(u64::MAX);
    |                                                         ^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

For more information about this error, try `rustc --explain E0015`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking Issue for Duration::from_nanos_u128
9 participants