-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Make duration_since documentation more clear #76162
Make duration_since documentation more clear #76162
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
ba4665b
to
a5c2733
Compare
a5c2733
to
69cf99c
Compare
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.
Other than the last suggestion. Everything looks good to me.
69cf99c
to
4c2d783
Compare
4c2d783
to
875f39b
Compare
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.
Looks good to me.
library/std/src/time.rs
Outdated
/// let new_sys_time = sys_time.checked_add(Duration::new(1, 0)).unwrap(); | ||
/// let difference = new_sys_time.duration_since(sys_time) | ||
/// .expect("Clock may have gone backwards"); |
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.
Hmm, I know @pickfire suggested checked_add
but I don't think it really gets the point of the example across - this will never be an Err
because the checked_add
will mean it will always be later than before.
I think I'd prefer no_run
and having the sleep from before (plus add use std::thread
at the top):
/// let new_sys_time = sys_time.checked_add(Duration::new(1, 0)).unwrap(); | |
/// let difference = new_sys_time.duration_since(sys_time) | |
/// .expect("Clock may have gone backwards"); | |
/// thread::sleep(Duration::from_secs(1)); | |
/// let new_sys_time = SystemTime::now(); | |
/// let difference = new_sys_time.duration_since(sys_time) | |
/// .expect("Clock may have gone backwards"); |
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.
@jyn514 I know the previous example is better but it cannot be run with no_run
, if any I would prefer to see a mock instead of both of the solutions we have right now. It can both tell the sleep
as well as teaching the readers how to mock time in tests, we don't have any of it written right now.
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.
Right, but I think having no_run
is better than having an example that doesn't make sense.
Another alternative is to remove the sleep
altogether, but then it really needs to be no_run because otherwise it will sometimes fail.
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.
Right, but I think having no_run is better than having an example that doesn't make sense.
My suggestion for this is to add a comment that saying it is used to replace sleep
.
Another alternative is to remove the sleep altogether, but then it really needs to be no_run because otherwise it will sometimes fail.
If we do that then this patch does not do anything at all. So it remains the same.
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.
No, the previous code used the same duration for the comparison, which would never fail because it would never go backwards. This now uses a new duration, which might fail because system time is not monotonic.
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.
This now uses a new duration, which might fail because system time is not monotonic.
Oh, I forgot about this. Yeah, this is why time may go backwards.
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.
Glad we're in agreement 😆
@abrausch do you mind updating this to use no_run
to make it more clear why this is a potential footgun?
/// let new_sys_time = sys_time.checked_add(Duration::new(1, 0)).unwrap(); | |
/// let difference = new_sys_time.duration_since(sys_time) | |
/// .expect("Clock may have gone backwards"); | |
/// let new_sys_time = SystemTime::now(); | |
/// let difference = new_sys_time.duration_since(sys_time) | |
/// .expect("Clock may have gone backwards"); |
875f39b
to
7dcbf52
Compare
7dcbf52
to
98231bf
Compare
@bors r+ rollup Thanks for your PR and for your patience! |
📌 Commit 98231bf has been approved by |
Rollup of 10 pull requests Successful merges: - rust-lang#76162 (Make duration_since documentation more clear) - rust-lang#76355 (remove public visibility previously needed for rustfmt) - rust-lang#76374 (Improve ayu doc source line number contrast) - rust-lang#76379 (rustbuild: Remove `Mode::Codegen`) - rust-lang#76389 (Fix HashMap visualizers in Visual Studio (Code)) - rust-lang#76396 (Fix typo in tracking issue template) - rust-lang#76401 (Add help note to unconstrained const parameter) - rust-lang#76402 (Update linker-plugin-lto.md to contain up to rust 1.46) - rust-lang#76403 (Fix documentation for TyCtxt::all_impls) - rust-lang#76498 (Update cargo) Failed merges: - rust-lang#76458 (Add drain_filter method to HashMap and HashSet) r? `@ghost`
No description provided.