-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Duration::zero() -> Duration::ZERO #78216
Conversation
This expands time's test suite to use more and in more places the range of methods and constants added to Duration in recent proposals for the sake of testing more API surface area and improving legibility.
Duration::ZERO composes better with match and various other things, at the cost of an occasional parens, and results in less work for the optimizer, so let's use that instead.
Makes sense to me! As I said in my comment you linked: I think either solution is fine, so moving forward with this seems like a good idea! Reassigning to start FCP: |
Woopsie, this is not a stabilization PR yet 😄 Sorry for the accidental ping. |
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.
Overall looks good to me, just a few minor nits.
Also, #76114 introduce Duration::MIN
. We probably want to replace it with ZERO
, but we can always do that later.
Duration::MIN is exactly why I figured I didn't want to open a stabilization PR for this just yet, because whether or not one should override or the two should alias was uncertain to me and I felt that should be an open question, and I felt the community should get half a second to respond to this change arriving in nightly. If you are in favor of removing Duration::MIN in favor of Duration::ZERO, then I will include that in this PR as another commit so it can be reverted individually. |
Duration::ZERO supercedes it in effect.
c986af5
to
82f3a23
Compare
@bors r+ |
📌 Commit 82f3a23 has been approved by |
MIN was introduced very recently, so replacing it should not be much of a problem. |
…as-schievink Rollup of 11 pull requests Successful merges: - rust-lang#78216 (Duration::zero() -> Duration::ZERO) - rust-lang#78354 (Support enable/disable sanitizers/profiler per target) - rust-lang#78417 (BTreeMap: split off most code of append) - rust-lang#78832 (look at assoc ct, check the type of nodes) - rust-lang#78873 (Add flags customizing behaviour of MIR inlining) - rust-lang#78899 (Support inlining diverging function calls) - rust-lang#78923 (Cleanup and comment intra-doc link pass) - rust-lang#78929 (rustc_target: Move target env "gnu" from `linux_base` to `linux_gnu_base`) - rust-lang#78930 (rustc_taret: Remove `TargetOptions::is_like_android`) - rust-lang#78942 (Fix typo in comment) - rust-lang#78947 (Ship llvm-cov through llvm-tools) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Hello! |
A "negative Duration" doesn't make much sense to me, but that's for libs to decide, I think. |
@marmeladema Ah, thanks for adding that note here. It's useful to have that context. Duration will most likely never allow for negative values, as that'd be a breaking change. Functions like |
I fully agree with this. And even if there were negative durations, I don't think it would be immediately apparent that a negative duration would be "smaller" than a duration of time zero. I don't think it makes much sense to talk about a "minimal" duration other than zero, and then the name zero makes way more sense. I also don't think it would be good to change the value of this type of constant after it had been stabilized. If |
Hello. I would like to add few notes about duration.
These two usecases can be covered by single type in case of 1.b or 1.c (preferably c) or by two types, in case of 1.a To prevent BC break, another type should be introduced, e.g. TimeDifference which some nice conversion to Duration and back. |
@polomsky New comments on already closed/merged PRs are easy to miss. If you found an issue with something in the language or libary, it'd be good to open a new issue for it instead. For new ideas, it's usually best to discuss it on the internals forum first. |
In review for #72790, whether or not a constant or a function should be favored for
#![feature(duration_zero)]
was seen as an open question. In #73544 (comment) an invitation was opened to either stabilize the methods or propose a switch to the constant value, supplemented with reasoning. Followup comments suggested community preference leans towards the const ZERO, which would be reason enough.ZERO also "makes sense" beside existing associated consts for Duration. It is ever so slightly awkward to have a series of constants specifying 1 of various units but leave 0 as a method, especially when they are side-by-side in code. It seems unintuitive for the one non-dynamic value (that isn't from Default) to be not-a-const, which could hurt discoverability of the associated constants overall. Elsewhere in
std
, methods for obtaining a constant value were even deprecated, as seen with std::u32::min_value.Most importantly, ZERO costs less to use. A match supports a const pattern, but const fn can only be used if evaluated through a const context such as an inline
const { const_fn() }
or aconst NAME: T = const_fn()
declaration elsewhere. Likewise, while #73544 (comment) notesDuration::zero()
can optimize to a constant value, "can" is not "will". Only const contexts have a strong promise of such. Even without that in mind, the comment in question still leans in favor of the constant for simplicity. As it costs less for a developer to use, may cost less to optimize, and seems to have more of a community consensus for it, the associated const seems best.r? @LukasKalbertodt