@@ -42,10 +42,6 @@ impl Timespec {
42
42
}
43
43
}
44
44
45
- fn add_duration ( & self , other : & Duration ) -> Timespec {
46
- self . checked_add_duration ( other) . expect ( "overflow when adding duration to time" )
47
- }
48
-
49
45
fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
50
46
let mut secs = other
51
47
. as_secs ( )
@@ -68,27 +64,25 @@ impl Timespec {
68
64
} )
69
65
}
70
66
71
- fn sub_duration ( & self , other : & Duration ) -> Timespec {
67
+ fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
72
68
let mut secs = other
73
69
. as_secs ( )
74
70
. try_into ( ) // <- target type would be `libc::time_t`
75
71
. 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) ) ?;
78
73
79
74
// Similar to above, nanos can't overflow.
80
75
let mut nsec = self . t . tv_nsec as i32 - other. subsec_nanos ( ) as i32 ;
81
76
if nsec < 0 {
82
77
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 ) ?;
85
79
}
86
- Timespec {
80
+ Some ( Timespec {
87
81
t : libc:: timespec {
88
82
tv_sec : secs,
89
83
tv_nsec : nsec as _ ,
90
84
} ,
91
- }
85
+ } )
92
86
}
93
87
}
94
88
@@ -165,18 +159,16 @@ mod inner {
165
159
Duration :: new ( nanos / NSEC_PER_SEC , ( nanos % NSEC_PER_SEC ) as u32 )
166
160
}
167
161
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
+ } )
173
166
}
174
167
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
+ } )
180
172
}
181
173
}
182
174
@@ -199,16 +191,12 @@ mod inner {
199
191
self . t . sub_timespec ( & other. t )
200
192
}
201
193
202
- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
203
- SystemTime { t : self . t . add_duration ( other) }
204
- }
205
-
206
194
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) ? } )
208
196
}
209
197
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) ? } )
212
200
}
213
201
}
214
202
@@ -236,12 +224,12 @@ mod inner {
236
224
}
237
225
}
238
226
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 ) ?;
240
231
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 ) )
245
233
}
246
234
247
235
fn info ( ) -> & ' static libc:: mach_timebase_info {
@@ -299,12 +287,12 @@ mod inner {
299
287
} )
300
288
}
301
289
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) ? } )
304
292
}
305
293
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) ? } )
308
296
}
309
297
}
310
298
@@ -327,16 +315,12 @@ mod inner {
327
315
self . t . sub_timespec ( & other. t )
328
316
}
329
317
330
- pub fn add_duration ( & self , other : & Duration ) -> SystemTime {
331
- SystemTime { t : self . t . add_duration ( other) }
332
- }
333
-
334
318
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) ? } )
336
320
}
337
321
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) ? } )
340
324
}
341
325
}
342
326
0 commit comments