-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Proposal
Problem statement
The acception of #692 and the merge of rust-lang/rust#148825 led to the inclusion of a MAX and MIN constant to SystemTime.
Naturally it would only make sense to add saturating arithmetic to SystemTime as a follow-up to it, as it is already (partially) present in Instant and Duration, the other primitives found in the std::time module.
Next to the greater consistency with the standard library, a motivating factor for this is, that SystemTime::MAX and SystemTime::MIN are often times the most useful in context of saturating artihmetic, i.e. when handling failures of SystemTime::checked_add and SystemTime::checked_sub.
Instead of letting users repeat themselves, it is useful to provide this as part of the standard library.
Motivating examples or use cases
// Let `expiration` and `valid_after` be arbitrary.
let expiration: SystemTime = ...;
let valid_after: SystemTime = ...;
// Now calculate the lifetime for the object as expiration - valid_after.
let lifetime: Duration = expiration.duration_since(valid_after).unwrap_or(Duration::ZERO);
// Do some addition/subtraction as further examples.
let foo = expiration.checked_add(valid_after).unwrap_or(SystemTime::MAX);
let bar = expiration.checked_sub(valid_after).unwrap_or(SystemTime::MIN);
// Convert a SystemTime to a Unix timestamp
let timestamp: u64 = expiration.duration_since(SystemTime::UNIX_EPOCH).unwrap_or(Duration::ZERO).as_secs();Solution sketch
// As part of std.
impl SystemTime {
// ...
fn saturating_add(&self, rhs: Duration) -> Self {
self.checked_add(rhs).unwrap_or(SystemTime::MAX)
}
fn saturating_sub(&self, rhs: Duration) -> Self {
self.checked_sub(rhs).unwrap_or(SystemTime::MIN)
}
fn saturating_duration_since(&self, rhs: Self) -> Duration {
self.duration_since(rhs).unwrap_or(Duration::ZERO)
}
}Alternatives
With #![feature(time_systemtime_limits)]:
See Solution sketch above which effectively outlines a solution except that one may not extend SystemTime easily except with a custom (sealed) trait.
Without #![feature(time_systemtime_limits)]::
See the saturating-time crate which implements this outside of the standard library.
It has implements saturating_add, saturating_sub, and saturating_duration_since for SystemTime and Instant using a custom sealed trait.
Links and related work
#692
rust-lang/rust#148825
rust-lang/rust#71224
https://internals.rust-lang.org/t/instant-systemtime-min-max/21375
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.