@@ -130,10 +130,12 @@ impl Duration {
130130 /// ```
131131 #[ stable( feature = "duration" , since = "1.3.0" ) ]
132132 #[ 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+ } ;
137139 let nanos = nanos % NANOS_PER_SEC ;
138140 Duration { secs, nanos }
139141 }
@@ -393,7 +395,8 @@ impl Duration {
393395 /// ```
394396 #[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
395397 #[ 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 > {
397400 if let Some ( mut secs) = self . secs . checked_add ( rhs. secs ) {
398401 let mut nanos = self . nanos + rhs. nanos ;
399402 if nanos >= NANOS_PER_SEC {
@@ -428,7 +431,8 @@ impl Duration {
428431 /// ```
429432 #[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
430433 #[ 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 > {
432436 if let Some ( mut secs) = self . secs . checked_sub ( rhs. secs ) {
433437 let nanos = if self . nanos >= rhs. nanos {
434438 self . nanos - rhs. nanos
@@ -464,19 +468,19 @@ impl Duration {
464468 /// ```
465469 #[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
466470 #[ 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 > {
468473 // Multiply nanoseconds as u64, because it cannot overflow that way.
469474 let total_nanos = self . nanos as u64 * rhs as u64 ;
470475 let extra_secs = total_nanos / ( NANOS_PER_SEC as u64 ) ;
471476 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+ }
479482 }
483+ None
480484 }
481485
482486 /// Checked `Duration` division. Computes `self / other`, returning [`None`]
@@ -497,7 +501,8 @@ impl Duration {
497501 /// ```
498502 #[ stable( feature = "duration_checked_ops" , since = "1.16.0" ) ]
499503 #[ 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 > {
501506 if rhs != 0 {
502507 let secs = self . secs / ( rhs as u64 ) ;
503508 let carry = self . secs - secs * ( rhs as u64 ) ;
@@ -523,7 +528,8 @@ impl Duration {
523528 /// ```
524529 #[ stable( feature = "duration_float" , since = "1.38.0" ) ]
525530 #[ 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 {
527533 ( self . secs as f64 ) + ( self . nanos as f64 ) / ( NANOS_PER_SEC as f64 )
528534 }
529535
@@ -540,7 +546,8 @@ impl Duration {
540546 /// ```
541547 #[ stable( feature = "duration_float" , since = "1.38.0" ) ]
542548 #[ 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 {
544551 ( self . secs as f32 ) + ( self . nanos as f32 ) / ( NANOS_PER_SEC as f32 )
545552 }
546553
@@ -707,7 +714,8 @@ impl Duration {
707714 /// ```
708715 #[ unstable( feature = "div_duration" , issue = "63139" ) ]
709716 #[ 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 {
711719 self . as_secs_f64 ( ) / rhs. as_secs_f64 ( )
712720 }
713721
@@ -724,7 +732,8 @@ impl Duration {
724732 /// ```
725733 #[ unstable( feature = "div_duration" , issue = "63139" ) ]
726734 #[ 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 {
728737 self . as_secs_f32 ( ) / rhs. as_secs_f32 ( )
729738 }
730739}
0 commit comments