@@ -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 ( )
@@ -68,27 +64,25 @@ impl Timespec {
6864 } )
6965 }
7066
71- fn sub_duration ( & self , other : & Duration ) -> Timespec {
67+ fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
7268 let mut secs = other
7369 . as_secs ( )
7470 . try_into ( ) // <- target type would be `libc::time_t`
7571 . ok ( )
76- . and_then ( |secs| self . t . tv_sec . checked_sub ( secs) )
77- . expect ( "overflow when subtracting duration from time" ) ;
72+ . and_then ( |secs| self . t . tv_sec . checked_sub ( secs) ) ?;
7873
7974 // Similar to above, nanos can't overflow.
8075 let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
8176 if nsec < 0 {
8277 nsec += NSEC_PER_SEC as i32 ;
83- secs = secs. checked_sub ( 1 ) . expect ( "overflow when subtracting \
84- duration from time") ;
78+ secs = secs. checked_sub ( 1 ) ?;
8579 }
86- Timespec {
80+ Some ( Timespec {
8781 t : libc:: timespec {
8882 tv_sec : secs,
8983 tv_nsec : nsec as _ ,
9084 } ,
91- }
85+ } )
9286 }
9387}
9488
@@ -165,18 +159,16 @@ mod inner {
165159 Duration :: new ( nanos / NSEC_PER_SEC , ( nanos % NSEC_PER_SEC ) as u32 )
166160 }
167161
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- }
162+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
163+ Some ( Instant {
164+ t : self . t . checked_add ( checked_dur2intervals ( other) ?) ?,
165+ } )
173166 }
174167
175- pub fn sub_duration ( & self , other : & Duration ) -> Instant {
176- Instant {
177- t : self . t . checked_sub ( dur2intervals ( other) )
178- . expect ( "overflow when subtracting duration from instant" ) ,
179- }
168+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Instant > {
169+ Some ( Instant {
170+ t : self . t . checked_sub ( checked_dur2intervals ( other) ?) ?,
171+ } )
180172 }
181173 }
182174
@@ -199,16 +191,12 @@ mod inner {
199191 self . t . sub_timespec ( & other. t )
200192 }
201193
202- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
203- SystemTime { t : self . t . add_duration ( other) }
204- }
205-
206194 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
207- self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
195+ Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
208196 }
209197
210- pub fn sub_duration ( & self , other : & Duration ) -> SystemTime {
211- SystemTime { t : self . t . sub_duration ( other) }
198+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
199+ Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
212200 }
213201 }
214202
@@ -236,12 +224,12 @@ mod inner {
236224 }
237225 }
238226
239- fn dur2intervals ( dur : & Duration ) -> u64 {
227+ fn checked_dur2intervals ( dur : & Duration ) -> Option < u64 > {
228+ let nanos = dur. as_secs ( )
229+ . checked_mul ( NSEC_PER_SEC ) ?
230+ . checked_add ( dur. subsec_nanos ( ) as u64 ) ?;
240231 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 )
232+ Some ( mul_div_u64 ( nanos, info. denom as u64 , info. numer as u64 ) )
245233 }
246234
247235 fn info ( ) -> & ' static libc:: mach_timebase_info {
@@ -299,12 +287,12 @@ mod inner {
299287 } )
300288 }
301289
302- pub fn add_duration ( & self , other : & Duration ) -> Instant {
303- Instant { t : self . t . add_duration ( other) }
290+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
291+ Some ( Instant { t : self . t . checked_add_duration ( other) ? } )
304292 }
305293
306- pub fn sub_duration ( & self , other : & Duration ) -> Instant {
307- Instant { t : self . t . sub_duration ( other) }
294+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Instant > {
295+ Some ( Instant { t : self . t . checked_sub_duration ( other) ? } )
308296 }
309297 }
310298
@@ -327,16 +315,12 @@ mod inner {
327315 self . t . sub_timespec ( & other. t )
328316 }
329317
330- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
331- SystemTime { t : self . t . add_duration ( other) }
332- }
333-
334318 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
335- self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
319+ Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
336320 }
337321
338- pub fn sub_duration ( & self , other : & Duration ) -> SystemTime {
339- SystemTime { t : self . t . sub_duration ( other) }
322+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
323+ Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
340324 }
341325 }
342326
0 commit comments