Skip to content

Commit 0019f4b

Browse files
committed
Constify SystemTime methods
1 parent 336eab7 commit 0019f4b

File tree

10 files changed

+117
-50
lines changed

10 files changed

+117
-50
lines changed

library/std/src/sys/pal/hermit/time.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl Timespec {
2525
Timespec { t: timespec { tv_sec, tv_nsec } }
2626
}
2727

28-
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
28+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
29+
const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
2930
if self >= other {
3031
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
3132
Duration::new(
@@ -46,7 +47,8 @@ impl Timespec {
4647
}
4748
}
4849

49-
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
50+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
51+
const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
5052
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
5153

5254
// Nano calculations can't overflow because nanos are <1B which fit
@@ -59,7 +61,8 @@ impl Timespec {
5961
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
6062
}
6163

62-
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
64+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
65+
const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
6366
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
6467

6568
// Similar to above, nanos can't overflow.
@@ -213,15 +216,18 @@ impl SystemTime {
213216
SystemTime(time)
214217
}
215218

216-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
219+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
220+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
217221
self.0.sub_timespec(&other.0)
218222
}
219223

220-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
224+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
225+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
221226
Some(SystemTime(self.0.checked_add_duration(other)?))
222227
}
223228

224-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
229+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
230+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
225231
Some(SystemTime(self.0.checked_sub_duration(other)?))
226232
}
227233
}

library/std/src/sys/pal/sgx/time.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@ impl SystemTime {
3232
SystemTime(usercalls::insecure_time())
3333
}
3434

35-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
35+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
36+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
37+
// FIXME: ok_or_else with const closures
38+
match self.0.checked_sub(other.0) {
39+
Some(duration) => Ok(duration),
40+
None => Err(other.0 - self.0),
41+
}
3742
}
3843

39-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
44+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
45+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4046
Some(SystemTime(self.0.checked_add(*other)?))
4147
}
4248

43-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
49+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
50+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
4451
Some(SystemTime(self.0.checked_sub(*other)?))
4552
}
4653
}

library/std/src/sys/pal/solid/time.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@ impl SystemTime {
3939
Self(t)
4040
}
4141

42-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
42+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
43+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
4344
if self.0 >= other.0 {
4445
Ok(Duration::from_secs((self.0 as u64).wrapping_sub(other.0 as u64)))
4546
} else {
4647
Err(Duration::from_secs((other.0 as u64).wrapping_sub(self.0 as u64)))
4748
}
4849
}
4950

50-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
51+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
5153
Some(SystemTime(self.0.checked_add_unsigned(other.as_secs())?))
5254
}
5355

54-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
56+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
57+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5558
Some(SystemTime(self.0.checked_sub_unsigned(other.as_secs())?))
5659
}
5760
}

library/std/src/sys/pal/uefi/time.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ impl SystemTime {
4545
.unwrap_or_else(|| panic!("time not implemented on this platform"))
4646
}
4747

48-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
49-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
48+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
49+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
50+
// FIXME: ok_or_else with const closures
51+
match self.0.checked_sub(other.0) {
52+
Some(duration) => Ok(duration),
53+
None => Err(other.0 - self.0),
54+
}
5055
}
5156

52-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
57+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
58+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
5359
Some(SystemTime(self.0.checked_add(*other)?))
5460
}
5561

56-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
62+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
63+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5764
Some(SystemTime(self.0.checked_sub(*other)?))
5865
}
5966
}

library/std/src/sys/pal/unix/time.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ impl SystemTime {
3838
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
3939
}
4040

41-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
41+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
42+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
4243
self.t.sub_timespec(&other.t)
4344
}
4445

45-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
46+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
47+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4648
Some(SystemTime { t: self.t.checked_add_duration(other)? })
4749
}
4850

49-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
51+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5053
Some(SystemTime { t: self.t.checked_sub_duration(other)? })
5154
}
5255
}
@@ -133,7 +136,8 @@ impl Timespec {
133136
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap()
134137
}
135138

136-
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
139+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
140+
pub const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
137141
if self >= other {
138142
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
139143
// to optimize it into a branchless form (see also #75545):
@@ -169,7 +173,8 @@ impl Timespec {
169173
}
170174
}
171175

172-
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
176+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
177+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
173178
let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
174179

175180
// Nano calculations can't overflow because nanos are <1B which fit
@@ -182,7 +187,8 @@ impl Timespec {
182187
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
183188
}
184189

185-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
190+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
191+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
186192
let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
187193

188194
// Similar to above, nanos can't overflow.

library/std/src/sys/pal/unsupported/time.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ impl SystemTime {
3131
panic!("time not implemented on this platform")
3232
}
3333

34-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
35-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
34+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
35+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36+
// FIXME: ok_or_else with const closures
37+
match self.0.checked_sub(other.0) {
38+
Some(duration) => Ok(duration),
39+
None => Err(other.0 - self.0),
40+
}
3641
}
3742

38-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
43+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
44+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
3945
Some(SystemTime(self.0.checked_add(*other)?))
4046
}
4147

42-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
48+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
49+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
4350
Some(SystemTime(self.0.checked_sub(*other)?))
4451
}
4552
}

library/std/src/sys/pal/wasi/time.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,32 @@ impl SystemTime {
4343
SystemTime(current_time(wasi::CLOCKID_REALTIME))
4444
}
4545

46-
pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
46+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
47+
pub const fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
4748
SystemTime(Duration::from_nanos(ts))
4849
}
4950

50-
pub fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
51+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52+
pub const fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
5153
self.0.as_nanos().try_into().ok()
5254
}
5355

54-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
55-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
56+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
57+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
58+
// FIXME: ok_or_else with const closures
59+
match self.0.checked_sub(other.0) {
60+
Some(duration) => Ok(duration),
61+
None => Err(other.0 - self.0),
62+
}
5663
}
5764

58-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
65+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
66+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
5967
Some(SystemTime(self.0.checked_add(*other)?))
6068
}
6169

62-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
70+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
71+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
6372
Some(SystemTime(self.0.checked_sub(*other)?))
6473
}
6574
}

library/std/src/sys/pal/windows/time.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ impl SystemTime {
7272
}
7373
}
7474

75-
fn from_intervals(intervals: i64) -> SystemTime {
75+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
76+
const fn from_intervals(intervals: i64) -> SystemTime {
7677
SystemTime {
7778
t: c::FILETIME {
7879
dwLowDateTime: intervals as u32,
@@ -81,11 +82,13 @@ impl SystemTime {
8182
}
8283
}
8384

84-
fn intervals(&self) -> i64 {
85+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
86+
const fn intervals(&self) -> i64 {
8587
(self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
8688
}
8789

88-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
90+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
91+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
8992
let me = self.intervals();
9093
let other = other.intervals();
9194
if me >= other {
@@ -95,12 +98,14 @@ impl SystemTime {
9598
}
9699
}
97100

98-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
101+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
102+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
99103
let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
100104
Some(SystemTime::from_intervals(intervals))
101105
}
102106

103-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
107+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
108+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
104109
let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?;
105110
Some(SystemTime::from_intervals(intervals))
106111
}
@@ -150,15 +155,17 @@ impl Hash for SystemTime {
150155
}
151156
}
152157

153-
fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
158+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
159+
const fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
154160
dur.as_secs()
155161
.checked_mul(INTERVALS_PER_SEC)?
156162
.checked_add(dur.subsec_nanos() as u64 / 100)?
157163
.try_into()
158164
.ok()
159165
}
160166

161-
fn intervals2dur(intervals: u64) -> Duration {
167+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
168+
const fn intervals2dur(intervals: u64) -> Duration {
162169
Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32)
163170
}
164171

library/std/src/sys/pal/xous/time.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ impl SystemTime {
4343
SystemTime { 0: Duration::from_millis((upper as u64) << 32 | lower as u64) }
4444
}
4545

46-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
47-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
46+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
47+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
48+
// FIXME: ok_or_else with const closures
49+
match self.0.checked_sub(other.0) {
50+
Some(duration) => Ok(duration),
51+
None => Err(other.0 - self.0),
52+
}
4853
}
4954

55+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
5056
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
5157
Some(SystemTime(self.0.checked_add(*other)?))
5258
}
5359

60+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
5461
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5562
Some(SystemTime(self.0.checked_sub(*other)?))
5663
}

0 commit comments

Comments
 (0)