@@ -130,10 +130,12 @@ impl Duration {
130
130
/// ```
131
131
#[ stable( feature = "duration" , since = "1.3.0" ) ]
132
132
#[ inline]
133
- #[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
134
- pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
135
- let secs =
136
- secs. checked_add ( ( nanos / NANOS_PER_SEC ) as u64 ) . expect ( "overflow in Duration::new" ) ;
133
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
134
+ pub const fn new ( secs : u64 , nanos : u32 ) -> Duration {
135
+ let secs = match secs. checked_add ( ( nanos / NANOS_PER_SEC ) as u64 ) {
136
+ Some ( secs) => secs,
137
+ None => panic ! ( "overflow in Duration::new" ) ,
138
+ } ;
137
139
let nanos = nanos % NANOS_PER_SEC ;
138
140
Duration { secs, nanos }
139
141
}
@@ -393,7 +395,8 @@ impl Duration {
393
395
/// ```
394
396
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
395
397
#[ inline]
396
- pub fn checked_add ( self , rhs : Duration ) -> Option < Duration > {
398
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
399
+ pub const fn checked_add ( self , rhs : Duration ) -> Option < Duration > {
397
400
if let Some ( mut secs) = self . secs . checked_add ( rhs. secs ) {
398
401
let mut nanos = self . nanos + rhs. nanos ;
399
402
if nanos >= NANOS_PER_SEC {
@@ -428,7 +431,8 @@ impl Duration {
428
431
/// ```
429
432
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
430
433
#[ inline]
431
- pub fn checked_sub ( self , rhs : Duration ) -> Option < Duration > {
434
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
435
+ pub const fn checked_sub ( self , rhs : Duration ) -> Option < Duration > {
432
436
if let Some ( mut secs) = self . secs . checked_sub ( rhs. secs ) {
433
437
let nanos = if self . nanos >= rhs. nanos {
434
438
self . nanos - rhs. nanos
@@ -464,19 +468,19 @@ impl Duration {
464
468
/// ```
465
469
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
466
470
#[ inline]
467
- pub fn checked_mul ( self , rhs : u32 ) -> Option < Duration > {
471
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
472
+ pub const fn checked_mul ( self , rhs : u32 ) -> Option < Duration > {
468
473
// Multiply nanoseconds as u64, because it cannot overflow that way.
469
474
let total_nanos = self . nanos as u64 * rhs as u64 ;
470
475
let extra_secs = total_nanos / ( NANOS_PER_SEC as u64 ) ;
471
476
let nanos = ( total_nanos % ( NANOS_PER_SEC as u64 ) ) as u32 ;
472
- if let Some ( secs) =
473
- self . secs . checked_mul ( rhs as u64 ) . and_then ( |s| s. checked_add ( extra_secs) )
474
- {
475
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
476
- Some ( Duration { secs, nanos } )
477
- } else {
478
- None
477
+ if let Some ( s) = self . secs . checked_mul ( rhs as u64 ) {
478
+ if let Some ( secs) = s. checked_add ( extra_secs) {
479
+ debug_assert ! ( nanos < NANOS_PER_SEC ) ;
480
+ return Some ( Duration { secs, nanos } ) ;
481
+ }
479
482
}
483
+ None
480
484
}
481
485
482
486
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
@@ -497,7 +501,8 @@ impl Duration {
497
501
/// ```
498
502
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
499
503
#[ inline]
500
- pub fn checked_div ( self , rhs : u32 ) -> Option < Duration > {
504
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
505
+ pub const fn checked_div ( self , rhs : u32 ) -> Option < Duration > {
501
506
if rhs != 0 {
502
507
let secs = self . secs / ( rhs as u64 ) ;
503
508
let carry = self . secs - secs * ( rhs as u64 ) ;
@@ -523,7 +528,8 @@ impl Duration {
523
528
/// ```
524
529
#[ stable( feature = "duration_float" , since = "1.38.0" ) ]
525
530
#[ inline]
526
- pub fn as_secs_f64 ( & self ) -> f64 {
531
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
532
+ pub const fn as_secs_f64 ( & self ) -> f64 {
527
533
( self . secs as f64 ) + ( self . nanos as f64 ) / ( NANOS_PER_SEC as f64 )
528
534
}
529
535
@@ -540,7 +546,8 @@ impl Duration {
540
546
/// ```
541
547
#[ stable( feature = "duration_float" , since = "1.38.0" ) ]
542
548
#[ inline]
543
- pub fn as_secs_f32 ( & self ) -> f32 {
549
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
550
+ pub const fn as_secs_f32 ( & self ) -> f32 {
544
551
( self . secs as f32 ) + ( self . nanos as f32 ) / ( NANOS_PER_SEC as f32 )
545
552
}
546
553
@@ -707,7 +714,8 @@ impl Duration {
707
714
/// ```
708
715
#[ unstable( feature = "div_duration" , issue = "63139" ) ]
709
716
#[ inline]
710
- pub fn div_duration_f64 ( self , rhs : Duration ) -> f64 {
717
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
718
+ pub const fn div_duration_f64 ( self , rhs : Duration ) -> f64 {
711
719
self . as_secs_f64 ( ) / rhs. as_secs_f64 ( )
712
720
}
713
721
@@ -724,7 +732,8 @@ impl Duration {
724
732
/// ```
725
733
#[ unstable( feature = "div_duration" , issue = "63139" ) ]
726
734
#[ inline]
727
- pub fn div_duration_f32 ( self , rhs : Duration ) -> f32 {
735
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
736
+ pub const fn div_duration_f32 ( self , rhs : Duration ) -> f32 {
728
737
self . as_secs_f32 ( ) / rhs. as_secs_f32 ( )
729
738
}
730
739
}
0 commit comments