@@ -64,27 +64,25 @@ impl Timespec {
6464 } )
6565 }
6666
67- fn sub_duration ( & self , other : & Duration ) -> Timespec {
67+ fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
6868 let mut secs = other
6969 . as_secs ( )
7070 . try_into ( ) // <- target type would be `libc::time_t`
7171 . ok ( )
72- . and_then ( |secs| self . t . tv_sec . checked_sub ( secs) )
73- . expect ( "overflow when subtracting duration from time" ) ;
72+ . and_then ( |secs| self . t . tv_sec . checked_sub ( secs) ) ?;
7473
7574 // Similar to above, nanos can't overflow.
7675 let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
7776 if nsec < 0 {
7877 nsec += NSEC_PER_SEC as i32 ;
79- secs = secs. checked_sub ( 1 ) . expect ( "overflow when subtracting \
80- duration from time") ;
78+ secs = secs. checked_sub ( 1 ) ?;
8179 }
82- Timespec {
80+ Some ( Timespec {
8381 t : libc:: timespec {
8482 tv_sec : secs,
8583 tv_nsec : nsec as _ ,
8684 } ,
87- }
85+ } )
8886 }
8987}
9088
@@ -162,14 +160,15 @@ mod inner {
162160 }
163161
164162 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
165- checked_dur2intervals ( other) ?. checked_add ( self . t ) . map ( |t| Instant { t} )
163+ Some ( Instant {
164+ t : self . t . checked_add ( checked_dur2intervals ( other) ?) ?,
165+ } )
166166 }
167167
168- pub fn sub_duration ( & self , other : & Duration ) -> Instant {
169- Instant {
170- t : self . t . checked_sub ( dur2intervals ( other) )
171- . expect ( "overflow when subtracting duration from instant" ) ,
172- }
168+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Instant > {
169+ Some ( Instant {
170+ t : self . t . checked_sub ( checked_dur2intervals ( other) ?) ?,
171+ } )
173172 }
174173 }
175174
@@ -193,11 +192,11 @@ mod inner {
193192 }
194193
195194 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
196- self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
195+ Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
197196 }
198197
199- pub fn sub_duration ( & self , other : & Duration ) -> SystemTime {
200- 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) ? } )
201200 }
202201 }
203202
@@ -225,11 +224,6 @@ mod inner {
225224 }
226225 }
227226
228- fn dur2intervals ( dur : & Duration ) -> u64 {
229- checked_dur2intervals ( dur)
230- . expect ( "overflow converting duration to nanoseconds" )
231- }
232-
233227 fn checked_dur2intervals ( dur : & Duration ) -> Option < u64 > {
234228 let nanos = dur. as_secs ( )
235229 . checked_mul ( NSEC_PER_SEC ) ?
@@ -294,11 +288,11 @@ mod inner {
294288 }
295289
296290 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
297- self . t . checked_add_duration ( other) . map ( |t| Instant { t } )
291+ Some ( Instant { t : self . t . checked_add_duration ( other) ? } )
298292 }
299293
300- pub fn sub_duration ( & self , other : & Duration ) -> Instant {
301- 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) ? } )
302296 }
303297 }
304298
@@ -322,11 +316,11 @@ mod inner {
322316 }
323317
324318 pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
325- self . t . checked_add_duration ( other) . map ( |t| SystemTime { t } )
319+ Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
326320 }
327321
328- pub fn sub_duration ( & self , other : & Duration ) -> SystemTime {
329- 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) ? } )
330324 }
331325 }
332326
0 commit comments