diff options
| author | ltdk <usr@ltdk.xyz> | 2025-07-26 18:51:13 -0400 |
|---|---|---|
| committer | ltdk <usr@ltdk.xyz> | 2025-08-12 14:05:47 -0400 |
| commit | 7ce620dd7c6fc3371290b40a1ea28146f0d37031 (patch) | |
| tree | d648382b0990037cb5a430eced44bf1adffb0a87 /library/std/src/time.rs | |
| parent | d9dba3a55476ae2da5d4e5bce8a81b341c675750 (diff) | |
| download | rust-7ce620dd7c6fc3371290b40a1ea28146f0d37031.tar.gz rust-7ce620dd7c6fc3371290b40a1ea28146f0d37031.zip | |
Constify SystemTime methods
Diffstat (limited to 'library/std/src/time.rs')
| -rw-r--r-- | library/std/src/time.rs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/library/std/src/time.rs b/library/std/src/time.rs index cd0683f44c9..07bb41f1496 100644 --- a/library/std/src/time.rs +++ b/library/std/src/time.rs @@ -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 } } |
