@@ -64,27 +64,25 @@ impl Timespec {
64
64
} )
65
65
}
66
66
67
- fn sub_duration ( & self , other : & Duration ) -> Timespec {
67
+ fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
68
68
let mut secs = other
69
69
. as_secs ( )
70
70
. try_into ( ) // <- target type would be `libc::time_t`
71
71
. 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) ) ?;
74
73
75
74
// Similar to above, nanos can't overflow.
76
75
let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
77
76
if nsec < 0 {
78
77
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 ) ?;
81
79
}
82
- Timespec {
80
+ Some ( Timespec {
83
81
t : libc:: timespec {
84
82
tv_sec : secs,
85
83
tv_nsec : nsec as _ ,
86
84
} ,
87
- }
85
+ } )
88
86
}
89
87
}
90
88
@@ -162,14 +160,15 @@ mod inner {
162
160
}
163
161
164
162
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
+ } )
166
166
}
167
167
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
+ } )
173
172
}
174
173
}
175
174
@@ -193,11 +192,11 @@ mod inner {
193
192
}
194
193
195
194
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) ? } )
197
196
}
198
197
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) ? } )
201
200
}
202
201
}
203
202
@@ -225,11 +224,6 @@ mod inner {
225
224
}
226
225
}
227
226
228
- fn dur2intervals ( dur : & Duration ) -> u64 {
229
- checked_dur2intervals ( dur)
230
- . expect ( "overflow converting duration to nanoseconds" )
231
- }
232
-
233
227
fn checked_dur2intervals ( dur : & Duration ) -> Option < u64 > {
234
228
let nanos = dur. as_secs ( )
235
229
. checked_mul ( NSEC_PER_SEC ) ?
@@ -294,11 +288,11 @@ mod inner {
294
288
}
295
289
296
290
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) ? } )
298
292
}
299
293
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) ? } )
302
296
}
303
297
}
304
298
@@ -322,11 +316,11 @@ mod inner {
322
316
}
323
317
324
318
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) ? } )
326
320
}
327
321
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) ? } )
330
324
}
331
325
}
332
326
0 commit comments