@@ -43,11 +43,15 @@ const DAYS_PER_WEEK: u64 = 7;
4343#[ rustc_layout_scalar_valid_range_end( 999_999_999 ) ]
4444struct Nanoseconds ( u32 ) ;
4545
46+ impl Nanoseconds {
47+ // SAFETY: 0 is within the valid range
48+ const ZERO : Self = unsafe { Nanoseconds ( 0 ) } ;
49+ }
50+
4651impl Default for Nanoseconds {
4752 #[ inline]
4853 fn default ( ) -> Self {
49- // SAFETY: 0 is within the valid range
50- unsafe { Nanoseconds ( 0 ) }
54+ Self :: ZERO
5155 }
5256}
5357
@@ -236,7 +240,7 @@ impl Duration {
236240 #[ inline]
237241 #[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
238242 pub const fn from_secs ( secs : u64 ) -> Duration {
239- Duration :: new ( secs, 0 )
243+ Duration { secs, nanos : Nanoseconds :: ZERO }
240244 }
241245
242246 /// Creates a new `Duration` from the specified number of milliseconds.
@@ -256,7 +260,13 @@ impl Duration {
256260 #[ inline]
257261 #[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
258262 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 }
260270 }
261271
262272 /// Creates a new `Duration` from the specified number of microseconds.
@@ -276,7 +286,13 @@ impl Duration {
276286 #[ inline]
277287 #[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
278288 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 }
280296 }
281297
282298 /// Creates a new `Duration` from the specified number of nanoseconds.
@@ -301,7 +317,13 @@ impl Duration {
301317 #[ inline]
302318 #[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
303319 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 }
305327 }
306328
307329 /// Creates a new `Duration` from the specified number of weeks.
0 commit comments