@@ -43,11 +43,15 @@ const DAYS_PER_WEEK: u64 = 7;
43
43
#[ rustc_layout_scalar_valid_range_end( 999_999_999 ) ]
44
44
struct Nanoseconds ( u32 ) ;
45
45
46
+ impl Nanoseconds {
47
+ // SAFETY: 0 is within the valid range
48
+ const ZERO : Self = unsafe { Nanoseconds ( 0 ) } ;
49
+ }
50
+
46
51
impl Default for Nanoseconds {
47
52
#[ inline]
48
53
fn default ( ) -> Self {
49
- // SAFETY: 0 is within the valid range
50
- unsafe { Nanoseconds ( 0 ) }
54
+ Self :: ZERO
51
55
}
52
56
}
53
57
@@ -236,7 +240,7 @@ impl Duration {
236
240
#[ inline]
237
241
#[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
238
242
pub const fn from_secs ( secs : u64 ) -> Duration {
239
- Duration :: new ( secs, 0 )
243
+ Duration { secs, nanos : Nanoseconds :: ZERO }
240
244
}
241
245
242
246
/// Creates a new `Duration` from the specified number of milliseconds.
@@ -256,7 +260,13 @@ impl Duration {
256
260
#[ inline]
257
261
#[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
258
262
pub const fn from_millis ( millis : u64 ) -> Duration {
259
- Duration :: new ( millis / MILLIS_PER_SEC , ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI )
263
+ let secs = millis / MILLIS_PER_SEC ;
264
+ let subsec_millis = ( millis % MILLIS_PER_SEC ) as u32 ;
265
+ // SAFETY: (x % 1_000) * 1_000_000 < 1_000_000_000
266
+ // => x % 1_000 < 1_000
267
+ let subsec_nanos = unsafe { Nanoseconds ( subsec_millis * NANOS_PER_MILLI ) } ;
268
+
269
+ Duration { secs, nanos : subsec_nanos }
260
270
}
261
271
262
272
/// Creates a new `Duration` from the specified number of microseconds.
@@ -276,7 +286,13 @@ impl Duration {
276
286
#[ inline]
277
287
#[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
278
288
pub const fn from_micros ( micros : u64 ) -> Duration {
279
- Duration :: new ( micros / MICROS_PER_SEC , ( ( micros % MICROS_PER_SEC ) as u32 ) * NANOS_PER_MICRO )
289
+ let secs = micros / MICROS_PER_SEC ;
290
+ let subsec_micros = ( micros % MICROS_PER_SEC ) as u32 ;
291
+ // SAFETY: (x % 1_000_000) * 1_000 < 1_000_000_000
292
+ // => x % 1_000_000 < 1_000_000
293
+ let subsec_nanos = unsafe { Nanoseconds ( subsec_micros * NANOS_PER_MICRO ) } ;
294
+
295
+ Duration { secs, nanos : subsec_nanos }
280
296
}
281
297
282
298
/// Creates a new `Duration` from the specified number of nanoseconds.
@@ -301,7 +317,13 @@ impl Duration {
301
317
#[ inline]
302
318
#[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
303
319
pub const fn from_nanos ( nanos : u64 ) -> Duration {
304
- Duration :: new ( nanos / ( NANOS_PER_SEC as u64 ) , ( nanos % ( NANOS_PER_SEC as u64 ) ) as u32 )
320
+ const NANOS_PER_SEC : u64 = self :: NANOS_PER_SEC as u64 ;
321
+ let secs = nanos / NANOS_PER_SEC ;
322
+ let subsec_nanos = ( nanos % NANOS_PER_SEC ) as u32 ;
323
+ // SAFETY: x % 1_000_000_000 < 1_000_000_000
324
+ let subsec_nanos = unsafe { Nanoseconds ( subsec_nanos) } ;
325
+
326
+ Duration { secs, nanos : subsec_nanos }
305
327
}
306
328
307
329
/// Creates a new `Duration` from the specified number of weeks.
0 commit comments