@@ -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
}
@@ -433,7 +435,8 @@ impl Duration {
433
435
/// ```
434
436
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
435
437
#[ inline]
436
- pub fn checked_add ( self , rhs : Duration ) -> Option < Duration > {
438
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
439
+ pub const fn checked_add ( self , rhs : Duration ) -> Option < Duration > {
437
440
if let Some ( mut secs) = self . secs . checked_add ( rhs. secs ) {
438
441
let mut nanos = self . nanos + rhs. nanos ;
439
442
if nanos >= NANOS_PER_SEC {
@@ -468,7 +471,8 @@ impl Duration {
468
471
/// ```
469
472
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
470
473
#[ inline]
471
- pub fn checked_sub ( self , rhs : Duration ) -> Option < Duration > {
474
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
475
+ pub const fn checked_sub ( self , rhs : Duration ) -> Option < Duration > {
472
476
if let Some ( mut secs) = self . secs . checked_sub ( rhs. secs ) {
473
477
let nanos = if self . nanos >= rhs. nanos {
474
478
self . nanos - rhs. nanos
@@ -504,19 +508,19 @@ impl Duration {
504
508
/// ```
505
509
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
506
510
#[ inline]
507
- pub fn checked_mul ( self , rhs : u32 ) -> Option < Duration > {
511
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
512
+ pub const fn checked_mul ( self , rhs : u32 ) -> Option < Duration > {
508
513
// Multiply nanoseconds as u64, because it cannot overflow that way.
509
514
let total_nanos = self . nanos as u64 * rhs as u64 ;
510
515
let extra_secs = total_nanos / ( NANOS_PER_SEC as u64 ) ;
511
516
let nanos = ( total_nanos % ( NANOS_PER_SEC as u64 ) ) as u32 ;
512
- if let Some ( secs) =
513
- self . secs . checked_mul ( rhs as u64 ) . and_then ( |s| s. checked_add ( extra_secs) )
514
- {
515
- debug_assert ! ( nanos < NANOS_PER_SEC ) ;
516
- Some ( Duration { secs, nanos } )
517
- } else {
518
- None
517
+ if let Some ( s) = self . secs . checked_mul ( rhs as u64 ) {
518
+ if let Some ( secs) = s. checked_add ( extra_secs) {
519
+ debug_assert ! ( nanos < NANOS_PER_SEC ) ;
520
+ return Some ( Duration { secs, nanos } ) ;
521
+ }
519
522
}
523
+ None
520
524
}
521
525
522
526
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
@@ -537,7 +541,8 @@ impl Duration {
537
541
/// ```
538
542
#[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
539
543
#[ inline]
540
- pub fn checked_div ( self , rhs : u32 ) -> Option < Duration > {
544
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
545
+ pub const fn checked_div ( self , rhs : u32 ) -> Option < Duration > {
541
546
if rhs != 0 {
542
547
let secs = self . secs / ( rhs as u64 ) ;
543
548
let carry = self . secs - secs * ( rhs as u64 ) ;
@@ -563,7 +568,8 @@ impl Duration {
563
568
/// ```
564
569
#[ stable( feature = "duration_float" , since = "1.38.0" ) ]
565
570
#[ inline]
566
- pub fn as_secs_f64 ( & self ) -> f64 {
571
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
572
+ pub const fn as_secs_f64 ( & self ) -> f64 {
567
573
( self . secs as f64 ) + ( self . nanos as f64 ) / ( NANOS_PER_SEC as f64 )
568
574
}
569
575
@@ -580,7 +586,8 @@ impl Duration {
580
586
/// ```
581
587
#[ stable( feature = "duration_float" , since = "1.38.0" ) ]
582
588
#[ inline]
583
- pub fn as_secs_f32 ( & self ) -> f32 {
589
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
590
+ pub const fn as_secs_f32 ( & self ) -> f32 {
584
591
( self . secs as f32 ) + ( self . nanos as f32 ) / ( NANOS_PER_SEC as f32 )
585
592
}
586
593
@@ -747,7 +754,8 @@ impl Duration {
747
754
/// ```
748
755
#[ unstable( feature = "div_duration" , issue = "63139" ) ]
749
756
#[ inline]
750
- pub fn div_duration_f64 ( self , rhs : Duration ) -> f64 {
757
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
758
+ pub const fn div_duration_f64 ( self , rhs : Duration ) -> f64 {
751
759
self . as_secs_f64 ( ) / rhs. as_secs_f64 ( )
752
760
}
753
761
@@ -764,7 +772,8 @@ impl Duration {
764
772
/// ```
765
773
#[ unstable( feature = "div_duration" , issue = "63139" ) ]
766
774
#[ inline]
767
- pub fn div_duration_f32 ( self , rhs : Duration ) -> f32 {
775
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
776
+ pub const fn div_duration_f32 ( self , rhs : Duration ) -> f32 {
768
777
self . as_secs_f32 ( ) / rhs. as_secs_f32 ( )
769
778
}
770
779
}
0 commit comments