Rollup merge of #144519 - clarfonthey:const-system-time, r=jhpratt
Constify `SystemTime` methods This is separated out from rust-lang/rust#143949 due to the fact that it touches nontrivial system code. While the same arithmetic methods on `Instant` could be made const, since that type explicitly cannot be constructed at const-time, we don't bother. However, due to the fact that `SystemTime::UNIX_EPOCH` exists, it can be useful to create other anchors at other points in time, and thus these methods should be possible to use in const context. > Side comment: I would honestly like to just move every single implementation which is a thin wrapper over `Duration` or some integer clock into their own module which is tested on all targets, so that we don't have to worry about code for lower-tier targets not being tested. However, the comments in `std::sys_common` pointing out the desire to move things to `std::sys::common` confused me, particularly due to the fact that `std::sys::common` is taken from the hellish asterisk-import of `std::sys::pal::common` and others. > > I think that, for trivial types like this, we should just have a platform-independent module that can be tested on all platforms, to both ensure that the code is properly tested regardless of whether higher-tier platforms needed, and to avoid the extreme mental gymnastics required to determine where in the code it lies. > > However, since I'm not on any of the teams maintaining this code and am not involved, I'll settle for just copy-pasting like everyone else has been doing. I just want to express how I'm not happy about that. **Reviewer note: please only pay attention to the last commit of this PR, until its blocker is merged.** Since this depends on the previous PR: `@rustbot` blocked
This commit is contained in:
commit
bd2db284fd
11 changed files with 166 additions and 68 deletions
|
|
@ -281,9 +281,11 @@
|
|||
#![feature(cfg_target_thread_local)]
|
||||
#![feature(cfi_encoding)]
|
||||
#![feature(char_max_len)]
|
||||
#![feature(const_trait_impl)]
|
||||
#![feature(core_float_math)]
|
||||
#![feature(decl_macro)]
|
||||
#![feature(deprecated_suggestion)]
|
||||
#![feature(derive_const)]
|
||||
#![feature(doc_cfg)]
|
||||
#![feature(doc_cfg_hide)]
|
||||
#![feature(doc_masked)]
|
||||
|
|
@ -329,6 +331,10 @@
|
|||
#![feature(bstr_internals)]
|
||||
#![feature(char_internals)]
|
||||
#![feature(clone_to_uninit)]
|
||||
#![feature(const_cmp)]
|
||||
#![feature(const_ops)]
|
||||
#![feature(const_option_ops)]
|
||||
#![feature(const_try)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(core_io_borrowed_buf)]
|
||||
#![feature(drop_guard)]
|
||||
|
|
|
|||
|
|
@ -25,8 +25,15 @@ impl Timespec {
|
|||
Timespec { t: timespec { tv_sec, tv_nsec } }
|
||||
}
|
||||
|
||||
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
|
||||
if self >= other {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
|
||||
// FIXME: const PartialOrd
|
||||
let mut cmp = self.t.tv_sec - other.t.tv_sec;
|
||||
if cmp == 0 {
|
||||
cmp = self.t.tv_nsec as i64 - other.t.tv_nsec as i64;
|
||||
}
|
||||
|
||||
if cmp >= 0 {
|
||||
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
|
||||
Duration::new(
|
||||
(self.t.tv_sec - other.t.tv_sec) as u64,
|
||||
|
|
@ -46,20 +53,22 @@ impl Timespec {
|
|||
}
|
||||
}
|
||||
|
||||
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
|
||||
|
||||
// Nano calculations can't overflow because nanos are <1B which fit
|
||||
// in a u32.
|
||||
let mut nsec = other.subsec_nanos() + u32::try_from(self.t.tv_nsec).unwrap();
|
||||
if nsec >= NSEC_PER_SEC.try_into().unwrap() {
|
||||
nsec -= u32::try_from(NSEC_PER_SEC).unwrap();
|
||||
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
|
||||
if nsec >= NSEC_PER_SEC as u32 {
|
||||
nsec -= NSEC_PER_SEC as u32;
|
||||
secs = secs.checked_add(1)?;
|
||||
}
|
||||
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
|
||||
}
|
||||
|
||||
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
|
||||
|
||||
// Similar to above, nanos can't overflow.
|
||||
|
|
@ -213,15 +222,18 @@ impl SystemTime {
|
|||
SystemTime(time)
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.sub_timespec(&other.0)
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add_duration(other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub_duration(other)?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,15 +32,22 @@ impl SystemTime {
|
|||
SystemTime(usercalls::insecure_time())
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
// FIXME: ok_or_else with const closures
|
||||
match self.0.checked_sub(other.0) {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err(other.0 - self.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,8 @@ impl SystemTime {
|
|||
Self(t)
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
if self.0 >= other.0 {
|
||||
Ok(Duration::from_secs((self.0 as u64).wrapping_sub(other.0 as u64)))
|
||||
} else {
|
||||
|
|
@ -47,11 +48,13 @@ impl SystemTime {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add_unsigned(other.as_secs())?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub_unsigned(other.as_secs())?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,19 +80,32 @@ impl SystemTime {
|
|||
.unwrap_or_else(|| panic!("time not implemented on this platform"))
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
// FIXME: ok_or_else with const closures
|
||||
match self.0.checked_sub(other.0) {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err(other.0 - self.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
let temp = Self(self.0.checked_add(*other)?);
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
let temp = self.0.checked_add(*other)?;
|
||||
|
||||
// Check if can be represented in UEFI
|
||||
if temp <= MAX_UEFI_TIME { Some(temp) } else { None }
|
||||
// FIXME: const PartialOrd
|
||||
let mut cmp = temp.as_secs() - MAX_UEFI_TIME.0.as_secs();
|
||||
if cmp == 0 {
|
||||
cmp = temp.subsec_nanos() as u64 - MAX_UEFI_TIME.0.subsec_nanos() as u64;
|
||||
}
|
||||
|
||||
if cmp <= 0 { Some(SystemTime(temp)) } else { None }
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
self.0.checked_sub(*other).map(Self)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,15 +38,18 @@ impl SystemTime {
|
|||
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.t.sub_timespec(&other.t)
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime { t: self.t.checked_add_duration(other)? })
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime { t: self.t.checked_sub_duration(other)? })
|
||||
}
|
||||
}
|
||||
|
|
@ -133,8 +136,15 @@ impl Timespec {
|
|||
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap()
|
||||
}
|
||||
|
||||
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
|
||||
if self >= other {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
|
||||
// FIXME: const PartialOrd
|
||||
let mut cmp = self.tv_sec - other.tv_sec;
|
||||
if cmp == 0 {
|
||||
cmp = self.tv_nsec.as_inner() as i64 - other.tv_nsec.as_inner() as i64;
|
||||
}
|
||||
|
||||
if cmp >= 0 {
|
||||
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
|
||||
// to optimize it into a branchless form (see also #75545):
|
||||
//
|
||||
|
|
@ -169,7 +179,8 @@ impl Timespec {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
|
||||
|
||||
// Nano calculations can't overflow because nanos are <1B which fit
|
||||
|
|
@ -179,10 +190,11 @@ impl Timespec {
|
|||
nsec -= NSEC_PER_SEC as u32;
|
||||
secs = secs.checked_add(1)?;
|
||||
}
|
||||
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
|
||||
Some(unsafe { Timespec::new_unchecked(secs, nsec as i64) })
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
|
||||
let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
|
||||
|
||||
// Similar to above, nanos can't overflow.
|
||||
|
|
@ -191,7 +203,7 @@ impl Timespec {
|
|||
nsec += NSEC_PER_SEC as i32;
|
||||
secs = secs.checked_sub(1)?;
|
||||
}
|
||||
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
|
||||
Some(unsafe { Timespec::new_unchecked(secs, nsec as i64) })
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
|
|||
|
|
@ -31,15 +31,22 @@ impl SystemTime {
|
|||
panic!("time not implemented on this platform")
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
// FIXME: ok_or_else with const closures
|
||||
match self.0.checked_sub(other.0) {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err(other.0 - self.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,23 +43,34 @@ impl SystemTime {
|
|||
SystemTime(current_time(wasi::CLOCKID_REALTIME))
|
||||
}
|
||||
|
||||
pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
|
||||
SystemTime(Duration::from_nanos(ts))
|
||||
}
|
||||
|
||||
pub fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
|
||||
self.0.as_nanos().try_into().ok()
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
|
||||
// FIXME: const TryInto
|
||||
let ns = self.0.as_nanos();
|
||||
if ns <= u64::MAX as u128 { Some(ns as u64) } else { None }
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
// FIXME: ok_or_else with const closures
|
||||
match self.0.checked_sub(other.0) {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err(other.0 - self.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,7 +72,8 @@ impl SystemTime {
|
|||
}
|
||||
}
|
||||
|
||||
fn from_intervals(intervals: i64) -> SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn from_intervals(intervals: i64) -> SystemTime {
|
||||
SystemTime {
|
||||
t: c::FILETIME {
|
||||
dwLowDateTime: intervals as u32,
|
||||
|
|
@ -81,11 +82,13 @@ impl SystemTime {
|
|||
}
|
||||
}
|
||||
|
||||
fn intervals(&self) -> i64 {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn intervals(&self) -> i64 {
|
||||
(self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
let me = self.intervals();
|
||||
let other = other.intervals();
|
||||
if me >= other {
|
||||
|
|
@ -95,12 +98,14 @@ impl SystemTime {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
|
||||
Some(SystemTime::from_intervals(intervals))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?;
|
||||
Some(SystemTime::from_intervals(intervals))
|
||||
}
|
||||
|
|
@ -150,15 +155,18 @@ impl Hash for SystemTime {
|
|||
}
|
||||
}
|
||||
|
||||
fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
|
||||
dur.as_secs()
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
|
||||
// FIXME: const TryInto
|
||||
let secs = dur
|
||||
.as_secs()
|
||||
.checked_mul(INTERVALS_PER_SEC)?
|
||||
.checked_add(dur.subsec_nanos() as u64 / 100)?
|
||||
.try_into()
|
||||
.ok()
|
||||
.checked_add(dur.subsec_nanos() as u64 / 100)?;
|
||||
if secs <= i64::MAX as u64 { Some(secs.cast_signed()) } else { None }
|
||||
}
|
||||
|
||||
fn intervals2dur(intervals: u64) -> Duration {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
const fn intervals2dur(intervals: u64) -> Duration {
|
||||
Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,15 +43,22 @@ impl SystemTime {
|
|||
SystemTime { 0: Duration::from_millis((upper as u64) << 32 | lower as u64) }
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
// FIXME: ok_or_else with const closures
|
||||
match self.0.checked_sub(other.0) {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err(other.0 - self.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -551,8 +551,13 @@ impl SystemTime {
|
|||
/// println!("{difference:?}");
|
||||
/// ```
|
||||
#[stable(feature = "time2", since = "1.8.0")]
|
||||
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
|
||||
self.0.sub_time(&earlier.0).map_err(SystemTimeError)
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
|
||||
// FIXME: map_err in const
|
||||
match self.0.sub_time(&earlier.0) {
|
||||
Ok(time) => Ok(time),
|
||||
Err(err) => Err(SystemTimeError(err)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the difference from this system time to the
|
||||
|
|
@ -589,7 +594,8 @@ impl SystemTime {
|
|||
/// `SystemTime` (which means it's inside the bounds of the underlying data structure), `None`
|
||||
/// otherwise.
|
||||
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
||||
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
|
||||
self.0.checked_add_duration(&duration).map(SystemTime)
|
||||
}
|
||||
|
||||
|
|
@ -597,13 +603,15 @@ impl SystemTime {
|
|||
/// `SystemTime` (which means it's inside the bounds of the underlying data structure), `None`
|
||||
/// otherwise.
|
||||
#[stable(feature = "time_checked_add", since = "1.34.0")]
|
||||
pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn checked_sub(&self, duration: Duration) -> Option<SystemTime> {
|
||||
self.0.checked_sub_duration(&duration).map(SystemTime)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "time2", since = "1.8.0")]
|
||||
impl Add<Duration> for SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
|
||||
impl const Add<Duration> for SystemTime {
|
||||
type Output = SystemTime;
|
||||
|
||||
/// # Panics
|
||||
|
|
@ -616,14 +624,16 @@ impl Add<Duration> for SystemTime {
|
|||
}
|
||||
|
||||
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
||||
impl AddAssign<Duration> for SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
|
||||
impl const AddAssign<Duration> for SystemTime {
|
||||
fn add_assign(&mut self, other: Duration) {
|
||||
*self = *self + other;
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "time2", since = "1.8.0")]
|
||||
impl Sub<Duration> for SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
|
||||
impl const Sub<Duration> for SystemTime {
|
||||
type Output = SystemTime;
|
||||
|
||||
fn sub(self, dur: Duration) -> SystemTime {
|
||||
|
|
@ -632,7 +642,8 @@ impl Sub<Duration> for SystemTime {
|
|||
}
|
||||
|
||||
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
|
||||
impl SubAssign<Duration> for SystemTime {
|
||||
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
|
||||
impl const SubAssign<Duration> for SystemTime {
|
||||
fn sub_assign(&mut self, other: Duration) {
|
||||
*self = *self - other;
|
||||
}
|
||||
|
|
@ -699,7 +710,8 @@ impl SystemTimeError {
|
|||
/// ```
|
||||
#[must_use]
|
||||
#[stable(feature = "time2", since = "1.8.0")]
|
||||
pub fn duration(&self) -> Duration {
|
||||
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
|
||||
pub const fn duration(&self) -> Duration {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue