-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add Duration::to_nanos #35868
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::to_nanos #35868
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (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. |
Alternatively, we can wait for rust-lang/rfcs#1504 to be implemented and return a |
There’s also an RFC for Duration::as_millis which got closed (though only because the RFC really proposed subsec_millis). I’m kinda against adding any more stuff to Duration before we get u128s. |
We should have this, but I'm afraid I agree with others to wait for 128bit datatypes. Option isn't exactly pretty, as most people would just be unwraping everywhere. |
Suspect we'll wait for u128 but maybe libs team should discuss. |
Discussed at libs triage today our conclusion was that there's enough possibilities here in flight that we'd prefer to have an RFC about this before moving forward with a PR. To be clear, the naming, presence of the methods, and arguments (none) all seem ok, it's purely just the return type. We're also very interested in indeed having these types! It just seemed that there were enough possibilities that an RFC is warranted to talk through them. |
@SimonSapin would you be interested in opening an RFC? |
Sorry, shepherding an RFC sounds like more time (!) than I want to put into this right now. I imagine that RFC would quickly expand to consider using |
Maybe we can add I'm assuming that since we have |
@arthurprs |
Yes, but that's the tricky decision. In practice it'll only fail due to bogus input. |
Another option is to make and document it as saturating, effectively returning u64::MAX on overflow. In humane terms that's 584,942,417 years. For the sake of efficiency I checked some assembly outputs, https://godbolt.org/g/y0H9u2 . In summary it's a couple of conditional moves in addition to the arithmetic ops. If we go the u128 route we're going to have a lot of |
@SimonSapin ah ok, no worries! @arthurprs the problem here is that we have a number of possible return values:
Each of these has various pros/cons, and this is what the libs team was thinking we'd need an RFC for (to weigh all of the decisions against one another) |
@alexcrichton Makes sense. I'll try to write something if someone doesn't beat me to it. |
Sometimes it is useful to extract a single number from a
Duration
, with better precision thanas_secs
. Overflow is possible but in many situation very unlikely. Of course users can do the math themselves withas_secs
andsubsec_nanos
, but it’s not as convenient and there’s a risk of using an incorrect number of zeros in the conversion. (I have written a bug much like that at least once.)This adds a
Duration::to_nanos(&self) -> Option<u64>
method.Result
could be used instead with a newOverflowError
empty struct, but there is no precedent for such a type.I’ve used the
to_*
naming convention because this feels more of a conversion thanas_secs
which just exposes a value as it is stored internally, but this is a very weak opinion.While we’re at it, and since
from_secs
andfrom_millis
already exist, this might be the occasion to also add:from_micros
from_nanos
(It would be the same asDuration::new(0, n)
… but then again we do havefrom_secs
which is the same asDuration::new(s, 0)
.)to_millis
to_micros
@alexcrichton, what do you think?