-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for taking this over! r? libs |
Happy to take this up. noted. |
@rustbot author |
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It looks like you did a merge rather than a rebase - that will need to get fixed since this branch still contains some weird history. Were you able to try what was suggested in rustc-dev-guide (linked above)? |
Hey Trevor
Yes I did do a rebase.
However CI wasn't getting triggered.
Core Test's
time.rs file had an additional #test
which I resolved and merged on github
The same thing I did in local and pushed to my branch but CI wasn't picking
it up.
It was stuck with "conflicts" in the file.
No worries I will take a look and rebase again if needed.
The other weird thing would be two commits
with cargo.lock both times my repo was 500+ commits behind upstream master.
I will squish these and keep just two commits
1) for the function
2) for the tests
…On Fri, 4 Jul, 2025, 1:19 am Trevor Gross, ***@***.***> wrote:
*tgross35* left a comment (rust-lang/rust#139243)
<#139243 (comment)>
It looks like you did a merge rather than a rebase - that will need to get
fixed since this branch still contains some weird history. Were you able to
try what was suggested in rustc-dev-guide (linked above)?
—
Reply to this email directly, view it on GitHub
<#139243 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPJEXXODJKQGXOSI7ALFVRD3GWCNLAVCNFSM6AAAAAB2I6KI7OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTAMZTGQZTCOJYHE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@omanirudh |
Hey all.
I'm unable to manage the git merge conflict
the logic is simple and done.
I tried figuring out the git part in vain.
Please take this over.
I no longer can give time to this.
Thanks all for the assistance
…On Sun, 10 Aug, 2025, 12:19 pm alexey semenyuk, ***@***.***> wrote:
*alex-semenyuk* left a comment (rust-lang/rust#139243)
<#139243 (comment)>
@omanirudh <https://github.com/omanirudh>
Thanks for your contribution.
Form wg-triage. Any updates on this PR?
—
Reply to this email directly, view it on GitHub
<#139243 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPJEXXMEJYBTE5Y76HJVZJT3M3TOPAVCNFSM6AAAAAB2I6KI7OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCNZSGQYTMMRSGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Tracking issue: RUST-139201
18ce05f
to
27d11bc
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
Okay yeah that history was a mess, there were two merge commits going opposite directions. For future reference, you can usually fix these kind of situations with git reset "$(git merge-base HEAD master)" Which wipes away history but leaves the files unstaged. So your repo will be reset to wherever you branched from master (the merge-base), and it will look like you just edited the files. Then you recreate a single commit with I did this for you and force pushed your branch so you don't need to worry about it. This just needs a few small changes if you are interested in finishing it up, now that git shouldn't be a problem. |
/// 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` |
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.
/// 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` | |
/// Panics if the given number of nanoseconds is greater than [`Duration::MAX`]. |
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). |
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.
/// Use this function if you need to specify time greater than what can fit in u64 | |
/// (around 584 years). |
I think this is reasonably straightforward
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 comment
The reason will be displayed to describe this comment to others. Learn more.
panic!("overflow in duration in Duration::from_nanos_u128"); | |
panic!("overflow in `Duration::from_nanos_u128`"); |
#[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 comment
The reason will be displayed to describe this comment to others. Learn more.
pub const fn from_nanos_u128(nanos: u128) -> Duration { | |
#[track_caller] | |
pub const fn from_nanos_u128(nanos: u128) -> Duration { |
Makes the panic message show where this was called, rather than reporting std's time.rs
as the panic location
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.
You'll need to add #![feature(duration_from_nanos_u128)]
to coretests/tests/lib.rs
, even for testing the unstable feature needs to be enabled (this is your CI failure)
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a test to duration_const
below (look for const NANOS
)
…28, r=tgross35 Add Duration::from_nanos_u128 Feature Gate: `#![feature(duration_from_nanos_u128)]` ACP: rust-lang/libs-team#567 Tracking issue: rust-lang#139201 Recreated from rust-lang#139243
Rollup merge of #145969 - actuallylost:duration-from-nanos-128, r=tgross35 Add Duration::from_nanos_u128 Feature Gate: `#![feature(duration_from_nanos_u128)]` ACP: rust-lang/libs-team#567 Tracking issue: #139201 Recreated from #139243
☔ The latest upstream changes (presumably #146026) made this pull request unmergeable. Please resolve the merge conflicts. |
It was done in #145969 so this PR can be closed. |
…28, r=tgross35 Add Duration::from_nanos_u128 Feature Gate: `#![feature(duration_from_nanos_u128)]` ACP: rust-lang/libs-team#567 Tracking issue: rust-lang#139201 Recreated from rust-lang#139243
Feature gate:
#![feature(duration_from_nanos_u128)]
ACP: rust-lang/libs-team#567
Tracking issue: #139201