@@ -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,8 +136,15 @@ 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 > {
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 {
138148 // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
139149 // to optimize it into a branchless form (see also #75545):
140150 //
@@ -169,7 +179,8 @@ impl Timespec {
169179 }
170180 }
171181
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 > {
173184 let mut secs = self . tv_sec . checked_add_unsigned ( other. as_secs ( ) ) ?;
174185
175186 // Nano calculations can't overflow because nanos are <1B which fit
@@ -179,10 +190,11 @@ impl Timespec {
179190 nsec -= NSEC_PER_SEC as u32 ;
180191 secs = secs. checked_add ( 1 ) ?;
181192 }
182- Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
193+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec as i64 ) } )
183194 }
184195
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 > {
186198 let mut secs = self . tv_sec . checked_sub_unsigned ( other. as_secs ( ) ) ?;
187199
188200 // Similar to above, nanos can't overflow.
@@ -191,7 +203,7 @@ impl Timespec {
191203 nsec += NSEC_PER_SEC as i32 ;
192204 secs = secs. checked_sub ( 1 ) ?;
193205 }
194- Some ( unsafe { Timespec :: new_unchecked ( secs, nsec. into ( ) ) } )
206+ Some ( unsafe { Timespec :: new_unchecked ( secs, nsec as i64 ) } )
195207 }
196208
197209 #[ allow( dead_code) ]
0 commit comments