-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
core/time: Add Duration methods for zero #72790
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
I considered using "immediate" instead of "zero", but figured the latter was both shorter and (perhaps) more intuitive, even though the former is more technically accurate. Happy to change that though. |
On a similar subject, I wanted to do a PR for |
Punting to randomly chosen T-libs person because I'm currently too busy: r? @LukasKalbertodt |
@marmeladema It feels a little odd to me to have |
That's fair :) |
Oh, yes, completely agree that |
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.
Sorry for the long delay!
I am all in for adding these methods. I am not sure if you are aware, but there was an RFC about exactly these methods. It was only closed because such a small change does not require an RFC.
ZERO
as associated constant was discussed as well. I don't oppose it, but I think the zero
associated function is what I would try out first. These kinds of "constructor functions" are pretty common, so I don't think adding this would hurt. The constant can still be added later.
(And for the protocol, this feature is so small that I think the doc tests are absolutely sufficient and we don't need extra tests.)
I also created a tracking issue: #73544
Please add its ID to the #[unstable]
attribute.
Thanks!
This patch adds two methods to `Duration`. The first, `Duration::zero`, provides a `const` constructor for getting an zero-length duration. This is also what `Default` provides (this was clarified in the docs), though `default` is not `const`. The second, `Duration::is_zero`, returns true if a `Duration` spans no time (i.e., because its components are all zero). Previously, the way to do this was either to compare both `as_secs` and `subsec_nanos` to 0, to compare against `Duration::new(0, 0)`, or to use the `u128` method `as_nanos`, none of which were particularly elegant.
Oh, nice, didn't know about that RFC! I've revised according to your comments. Thanks for creating the tracking issue :) |
@bors r+ |
📌 Commit 386114b has been approved by |
CC @ghost in case you didn't see this PR implementing your RFC. |
…bertodt core/time: Add Duration methods for zero This patch adds two methods to `Duration`. The first, `Duration::zero`, provides a `const` constructor for getting an zero-length duration. This is also what `Default` provides (this was clarified in the docs), though `default` is not `const`. The second, `Duration::is_zero`, returns true if a `Duration` spans no time (i.e., because its components are all zero). Previously, the way to do this was either to compare both `as_secs` and `subsec_nanos` to 0, to compare against `Duration::new(0, 0)`, or to use the `u128` method `as_nanos`, none of which were particularly elegant.
…arth Rollup of 9 pull requests Successful merges: - rust-lang#72456 (Try to suggest dereferences on trait selection failed) - rust-lang#72788 (Projection bound validation) - rust-lang#72790 (core/time: Add Duration methods for zero) - rust-lang#73227 (Allow multiple `asm!` options groups and report an error on duplicate options) - rust-lang#73287 (lint: normalize projections using opaque types) - rust-lang#73291 (Pre-compute `LocalDefId` <-> `HirId` mappings and remove `NodeId` <-> `HirId` conversion APIs) - rust-lang#73378 (Remove use of specialization from librustc_arena) - rust-lang#73411 (Update bootstrap to rustc 1.45.0-beta.2 (1dc0f6d 2020-06-15)) - rust-lang#73443 (ci: allow gating GHA on everything but macOS) Failed merges: r? @ghost
…, r=shepmaster Add saturating methods for `Duration` In some project, I needed a `saturating_add` method for `Duration`. I implemented it myself but i thought it would be a nice addition to the standard library as it matches closely with the integers types. 3 new methods have been introduced and are gated by the new `duration_saturating_ops` unstable feature: * `Duration::saturating_add` * `Duration::saturating_sub` * `Duration::saturating_mul` If have left the tracking issue to `none` for now as I want first to understand if those methods would be acceptable at all. If agreed, I'll update the PR with the tracking issue. Further more, to match the behavior of integers types, I introduced 2 associated constants: * `Duration::MIN`: this one is somehow a duplicate from `Duration::zero()` method, but at the time this method was added, `MIN` was rejected as it was considered a different semantic (see rust-lang#72790 (comment)). * `Duration::MAX` Both have been gated by the already existing unstable feature `duration_constants`, I can introduce a new unstable feature if needed or just re-use the `duration_saturating_ops`. We might have to decide whether: * `MIN` should be replaced by `ZERO`? * associated constants over methods?
…, r=shepmaster Add saturating methods for `Duration` In some project, I needed a `saturating_add` method for `Duration`. I implemented it myself but i thought it would be a nice addition to the standard library as it matches closely with the integers types. 3 new methods have been introduced and are gated by the new `duration_saturating_ops` unstable feature: * `Duration::saturating_add` * `Duration::saturating_sub` * `Duration::saturating_mul` If have left the tracking issue to `none` for now as I want first to understand if those methods would be acceptable at all. If agreed, I'll update the PR with the tracking issue. Further more, to match the behavior of integers types, I introduced 2 associated constants: * `Duration::MIN`: this one is somehow a duplicate from `Duration::zero()` method, but at the time this method was added, `MIN` was rejected as it was considered a different semantic (see rust-lang#72790 (comment)). * `Duration::MAX` Both have been gated by the already existing unstable feature `duration_constants`, I can introduce a new unstable feature if needed or just re-use the `duration_saturating_ops`. We might have to decide whether: * `MIN` should be replaced by `ZERO`? * associated constants over methods?
This patch adds two methods to
Duration
. The first,Duration::zero
,provides a
const
constructor for getting an zero-length duration. Thisis also what
Default
provides (this was clarified in the docs), thoughdefault
is notconst
.The second,
Duration::is_zero
, returns true if aDuration
spans notime (i.e., because its components are all zero). Previously, the way to
do this was either to compare both
as_secs
andsubsec_nanos
to 0, tocompare against
Duration::new(0, 0)
, or to use theu128
methodas_nanos
, none of which were particularly elegant.