@@ -38,15 +38,18 @@ impl SystemTime {
38
38
SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
39
39
}
40
40
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 > {
42
43
self . t . sub_timespec ( & other. t )
43
44
}
44
45
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 > {
46
48
Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
47
49
}
48
50
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 > {
50
53
Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
51
54
}
52
55
}
@@ -133,8 +136,15 @@ impl Timespec {
133
136
Timespec :: new ( t. tv_sec as i64 , t. tv_nsec as i64 ) . unwrap ( )
134
137
}
135
138
136
- pub fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
137
- if self >= other {
139
+ #[ rustc_const_unstable( feature = "const_system_time" , issue = "144517" ) ]
140
+ pub const fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
141
+ // FIXME: const PartialOrd
142
+ let mut cmp = self . tv_sec - other. tv_sec ;
143
+ if cmp == 0 {
144
+ cmp = self . tv_nsec . as_inner ( ) as i64 - other. tv_nsec . as_inner ( ) as i64 ;
145
+ }
146
+
147
+ if cmp >= 0 {
138
148
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
139
149
// to optimize it into a branchless form (see also #75545):
140
150
//
@@ -169,7 +179,8 @@ impl Timespec {
169
179
}
170
180
}
171
181
172
- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
182
+ #[ rustc_const_unstable( feature = "const_system_time" , issue = "144517" ) ]
183
+ pub const fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
173
184
let mut secs = self . tv_sec . checked_add_unsigned ( other. as_secs ( ) ) ?;
174
185
175
186
// Nano calculations can't overflow because nanos are <1B which fit
@@ -179,10 +190,11 @@ impl Timespec {
179
190
nsec -= NSEC_PER_SEC as u32 ;
180
191
secs = secs. checked_add ( 1 ) ?;
181
192
}
182
- Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
193
+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec as i64 ) } )
183
194
}
184
195
185
- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
196
+ #[ rustc_const_unstable( feature = "const_system_time" , issue = "144517" ) ]
197
+ pub const fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
186
198
let mut secs = self . tv_sec . checked_sub_unsigned ( other. as_secs ( ) ) ?;
187
199
188
200
// Similar to above, nanos can't overflow.
@@ -191,7 +203,7 @@ impl Timespec {
191
203
nsec += NSEC_PER_SEC as i32 ;
192
204
secs = secs. checked_sub ( 1 ) ?;
193
205
}
194
- Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
206
+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec as i64 ) } )
195
207
}
196
208
197
209
#[ allow( dead_code) ]
0 commit comments