@@ -228,15 +228,7 @@ impl Add for Duration {
228
228
type Output = Duration ;
229
229
230
230
fn add ( self , rhs : Duration ) -> Duration {
231
- let mut secs = self . secs . checked_add ( rhs. secs )
232
- . expect ( "overflow when adding durations" ) ;
233
- let mut nanos = self . nanos + rhs. nanos ;
234
- if nanos >= NANOS_PER_SEC {
235
- nanos -= NANOS_PER_SEC ;
236
- secs = secs. checked_add ( 1 ) . expect ( "overflow when adding durations" ) ;
237
- }
238
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
239
- Duration { secs : secs, nanos : nanos }
231
+ self . checked_add ( rhs) . expect ( "overflow when adding durations" )
240
232
}
241
233
}
242
234
@@ -252,17 +244,7 @@ impl Sub for Duration {
252
244
type Output = Duration ;
253
245
254
246
fn sub ( self , rhs : Duration ) -> Duration {
255
- let mut secs = self . secs . checked_sub ( rhs. secs )
256
- . expect ( "overflow when subtracting durations" ) ;
257
- let nanos = if self . nanos >= rhs. nanos {
258
- self . nanos - rhs. nanos
259
- } else {
260
- secs = secs. checked_sub ( 1 )
261
- . expect ( "overflow when subtracting durations" ) ;
262
- self . nanos + NANOS_PER_SEC - rhs. nanos
263
- } ;
264
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
265
- Duration { secs : secs, nanos : nanos }
247
+ self . checked_sub ( rhs) . expect ( "overflow when subtracting durations" )
266
248
}
267
249
}
268
250
@@ -278,15 +260,7 @@ impl Mul<u32> for Duration {
278
260
type Output = Duration ;
279
261
280
262
fn mul ( self , rhs : u32 ) -> Duration {
281
- // Multiply nanoseconds as u64, because it cannot overflow that way.
282
- let total_nanos = self . nanos as u64 * rhs as u64 ;
283
- let extra_secs = total_nanos / ( NANOS_PER_SEC as u64 ) ;
284
- let nanos = ( total_nanos % ( NANOS_PER_SEC as u64 ) ) as u32 ;
285
- let secs = self . secs . checked_mul ( rhs as u64 )
286
- . and_then ( |s| s. checked_add ( extra_secs) )
287
- . expect ( "overflow when multiplying duration" ) ;
288
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
289
- Duration { secs : secs, nanos : nanos }
263
+ self . checked_mul ( rhs) . expect ( "overflow when multiplying duration by scalar" )
290
264
}
291
265
}
292
266
@@ -302,12 +276,7 @@ impl Div<u32> for Duration {
302
276
type Output = Duration ;
303
277
304
278
fn div ( self , rhs : u32 ) -> Duration {
305
- let secs = self . secs / ( rhs as u64 ) ;
306
- let carry = self . secs - secs * ( rhs as u64 ) ;
307
- let extra_nanos = carry * ( NANOS_PER_SEC as u64 ) / ( rhs as u64 ) ;
308
- let nanos = self . nanos / rhs + ( extra_nanos as u32 ) ;
309
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
310
- Duration { secs : secs, nanos : nanos }
279
+ self . checked_div ( rhs) . expect ( "divide by zero error when dividing duration by scalar" )
311
280
}
312
281
}
313
282
0 commit comments