@@ -42,10 +42,6 @@ impl Timespec {
4242 }
4343 }
4444
45- fn add_duration ( & self , other : & Duration ) -> Timespec {
46- self . checked_add_duration ( other) . expect ( "overflow when adding duration to time" )
47- }
48-
4945 fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
5046 let mut secs = other
5147 . as_secs ( )
@@ -165,11 +161,8 @@ mod inner {
165161 Duration :: new ( nanos / NSEC_PER_SEC , ( nanos % NSEC_PER_SEC ) as u32 )
166162 }
167163
168- pub fn add_duration ( & self , other : & Duration ) -> Instant {
169- Instant {
170- t : self . t . checked_add ( dur2intervals ( other) )
171- . expect ( "overflow when adding duration to instant" ) ,
172- }
164+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
165+ checked_dur2intervals ( other) ?. checked_add ( self . t ) . map ( |t| Instant { t} )
173166 }
174167
175168 pub fn sub_duration ( & self , other : & Duration ) -> Instant {
@@ -199,10 +192,6 @@ mod inner {
199192 self . t . sub_timespec ( & other. t )
200193 }
201194
202- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
203- SystemTime { t : self . t . add_duration ( other) }
204- }
205-
206195 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
207196 self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
208197 }
@@ -237,11 +226,16 @@ mod inner {
237226 }
238227
239228 fn dur2intervals ( dur : & Duration ) -> u64 {
229+ checked_dur2intervals ( dur)
230+ . expect ( "overflow converting duration to nanoseconds" )
231+ }
232+
233+ fn checked_dur2intervals ( dur : & Duration ) -> Option < u64 > {
234+ let nanos = dur. as_secs ( )
235+ . checked_mul ( NSEC_PER_SEC ) ?
236+ . checked_add ( dur. subsec_nanos ( ) as u64 ) ?;
240237 let info = info ( ) ;
241- let nanos = dur. as_secs ( ) . checked_mul ( NSEC_PER_SEC ) . and_then ( |nanos| {
242- nanos. checked_add ( dur. subsec_nanos ( ) as u64 )
243- } ) . expect ( "overflow converting duration to nanoseconds" ) ;
244- mul_div_u64 ( nanos, info. denom as u64 , info. numer as u64 )
238+ Some ( mul_div_u64 ( nanos, info. denom as u64 , info. numer as u64 ) )
245239 }
246240
247241 fn info ( ) -> & ' static libc:: mach_timebase_info {
@@ -299,8 +293,8 @@ mod inner {
299293 } )
300294 }
301295
302- pub fn add_duration ( & self , other : & Duration ) -> Instant {
303- Instant { t : self . t . add_duration ( other) }
296+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
297+ self . t . checked_add_duration ( other) . map ( |t| Instant { t } )
304298 }
305299
306300 pub fn sub_duration ( & self , other : & Duration ) -> Instant {
@@ -327,10 +321,6 @@ mod inner {
327321 self . t . sub_timespec ( & other. t )
328322 }
329323
330- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
331- SystemTime { t : self . t . add_duration ( other) }
332- }
333-
334324 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
335325 self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
336326 }
0 commit comments