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

Make duration_since documentation more clear #76162

Merged
merged 1 commit into from
Sep 9, 2020

Conversation

abrausch
Copy link

No description provided.

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 31, 2020
library/std/src/time.rs Outdated Show resolved Hide resolved
@abrausch abrausch force-pushed the documentation-fix-duration_since branch from ba4665b to a5c2733 Compare August 31, 2020 21:33
library/std/src/time.rs Outdated Show resolved Hide resolved
library/std/src/time.rs Outdated Show resolved Hide resolved
library/std/src/time.rs Outdated Show resolved Hide resolved
@abrausch abrausch force-pushed the documentation-fix-duration_since branch from a5c2733 to 69cf99c Compare September 1, 2020 06:59
library/std/src/time.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@pickfire pickfire left a 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.

@abrausch abrausch force-pushed the documentation-fix-duration_since branch from 69cf99c to 4c2d783 Compare September 1, 2020 18:33
library/std/src/time.rs Outdated Show resolved Hide resolved
@abrausch abrausch force-pushed the documentation-fix-duration_since branch from 4c2d783 to 875f39b Compare September 2, 2020 07:09
Copy link
Contributor

@pickfire pickfire left a 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.

@jyn514 jyn514 added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Sep 8, 2020
Comment on lines 464 to 466
/// 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");
Copy link
Member

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):

Suggested change
/// 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");

Copy link
Contributor

@pickfire pickfire Sep 8, 2020

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.

Copy link
Member

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.

Copy link
Contributor

@pickfire pickfire Sep 8, 2020

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.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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?

Suggested change
/// 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");

@jyn514 jyn514 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 8, 2020
@abrausch abrausch force-pushed the documentation-fix-duration_since branch from 875f39b to 7dcbf52 Compare September 8, 2020 20:51
@abrausch abrausch force-pushed the documentation-fix-duration_since branch from 7dcbf52 to 98231bf Compare September 8, 2020 21:27
@jyn514
Copy link
Member

jyn514 commented Sep 8, 2020

@bors r+ rollup

Thanks for your PR and for your patience!

@bors
Copy link
Contributor

bors commented Sep 8, 2020

📌 Commit 98231bf has been approved by jyn514

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 8, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 9, 2020
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`
@bors bors merged commit 87302a2 into rust-lang:master Sep 9, 2020
@rustbot rustbot added this to the 1.48.0 milestone Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants