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 683 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.

@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.

@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.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@omanirudh omanirudh marked this pull request as ready for review April 10, 2025 15:53
@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Apr 10, 2025
matthiaskrgr and others added 3 commits April 26, 2025 07:13
…r-errors

handle specialization in the new trait solver

fixes rust-lang/trait-system-refactor-initiative#187
also fixes the regression in `plonky2_field` from rust-lang/trait-system-refactor-initiative#188

cc rust-lang#111994

r? ```@compiler-errors```
stall generator witness obligations: add regression test

fixes rust-lang/trait-system-refactor-initiative#180

r? ```@compiler-errors```
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139865 (Stabilize proc_macro::Span::{start,end,line,column}.)
 - rust-lang#140086 (If creating a temporary directory fails with permission denied then retry with backoff)
 - rust-lang#140216 (Document that "extern blocks must be unsafe" in Rust 2024)
 - rust-lang#140253 (Add XtensaAsmPrinter)
 - rust-lang#140272 (Improve error message for `||` (or) in let chains)
 - rust-lang#140305 (Track per-obligation recursion depth only if there is inference in the new solver)
 - rust-lang#140306 (handle specialization in the new trait solver)
 - rust-lang#140308 (stall generator witness obligations: add regression test)

r? `@ghost`
`@rustbot` modify labels: rollup
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Implementation and docs look good to me, left a few small comments about adding backticks around code types. However, this needs some tests. Could you update library/coretests/tests/time.rs to include from_nanos_u128 everywhere that from_nanos is tested? Also make sure some large values are tested, and one or two #[should_panic] tests for the overflow.

@@ -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,

///
/// 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`

@@ -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

@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 26, 2025
@tgross35
Copy link
Contributor

I looked into it. This PR now has just one commit. I preserved the commit messages of earlier commits though.

Also fyi - pedantically, there is no need to preserve the messages unless they are still meaningful. It's best if the message describes the contained change, it's not really useful to rust's forever history that you had "dummy commit" and "remove parentheses" at some point :)

@omanirudh
Copy link
Author

I looked into it. This PR now has just one commit. I preserved the commit messages of earlier commits though.

Also fyi - pedantically, there is no need to preserve the messages unless they are still meaningful. It's best if the message describes the contained change, it's not really useful to rust's forever history that you had "dummy commit" and "remove parentheses" at some point :)

noted.

@tgross35
Copy link
Contributor

tgross35 commented Jun 9, 2025

@omanirudh are you able to do the updates to get this over the line? Mostly just test updates and a few nits.

@omanirudh
Copy link
Author

omanirudh commented Jun 10, 2025 via email

@tgross35
Copy link
Contributor

It's not a problem! This is open source, there are no deadlines 🙂 we just try to make sure changes that are pretty close to done don't fall off the map.

@omanirudh
Copy link
Author

omanirudh commented Jun 10, 2025 via email

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic F-autodiff `#![feature(autodiff)]` O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like PG-exploit-mitigations Project group: Exploit mitigations WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred in coverage instrumentation.

cc @Zalathar

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

These commits modify the library/Cargo.lock file. Unintentional changes to library/Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in compiler/rustc_codegen_llvm/src/builder/autodiff.rs

cc @ZuseZ4

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @TaKO8Ki

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

changes to the core type system

cc @compiler-errors, @lcnr

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

These commits modify compiler targets.
(See the Target Tier Policy.)

HIR ty lowering was modified

cc @fmease

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2025

⚠️ Warning ⚠️

@tgross35
Copy link
Contributor

@omanirudh it looks like you wound up with bad commit history somehow. You should be able to fix this by rebasing your branch, check out https://rustc-dev-guide.rust-lang.org/git.html#rebasing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic F-autodiff `#![feature(autodiff)]` O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like PG-exploit-mitigations Project group: Exploit mitigations 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking Issue for Duration::from_nanos_u128