@@ -108,6 +108,34 @@ impl Duration {
108
108
#[ unstable( feature = "duration_constants" , issue = "57391" ) ]
109
109
pub const NANOSECOND : Duration = Duration :: from_nanos ( 1 ) ;
110
110
111
+ /// The minimum duration.
112
+ ///
113
+ /// # Examples
114
+ ///
115
+ /// ```
116
+ /// #![feature(duration_constants)]
117
+ /// use std::time::Duration;
118
+ ///
119
+ /// assert_eq!(Duration::MIN, Duration::new(0, 0));
120
+ /// ```
121
+ #[ unstable( feature = "duration_constants" , issue = "57391" ) ]
122
+ pub const MIN : Duration = Duration :: from_nanos ( 0 ) ;
123
+
124
+ /// The maximum duration.
125
+ ///
126
+ /// It is roughly equal to a duration of 584,942,417,355 years.
127
+ ///
128
+ /// # Examples
129
+ ///
130
+ /// ```
131
+ /// #![feature(duration_constants)]
132
+ /// use std::time::Duration;
133
+ ///
134
+ /// assert_eq!(Duration::MAX, Duration::new(u64::MAX, 1_000_000_000 - 1));
135
+ /// ```
136
+ #[ unstable( feature = "duration_constants" , issue = "57391" ) ]
137
+ pub const MAX : Duration = Duration :: new ( u64:: MAX , NANOS_PER_SEC - 1 ) ;
138
+
111
139
/// Creates a new `Duration` from the specified number of whole seconds and
112
140
/// additional nanoseconds.
113
141
///
@@ -450,6 +478,29 @@ impl Duration {
450
478
}
451
479
}
452
480
481
+ /// Saturating `Duration` addition. Computes `self + other`, returning [`Duration::MAX`]
482
+ /// if overflow occurred.
483
+ ///
484
+ /// # Examples
485
+ ///
486
+ /// ```
487
+ /// #![feature(duration_saturating_ops)]
488
+ /// #![feature(duration_constants)]
489
+ /// use std::time::Duration;
490
+ ///
491
+ /// assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
492
+ /// assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
493
+ /// ```
494
+ #[ unstable( feature = "duration_saturating_ops" , issue = "76416" ) ]
495
+ #[ inline]
496
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
497
+ pub const fn saturating_add ( self , rhs : Duration ) -> Duration {
498
+ match self . checked_add ( rhs) {
499
+ Some ( res) => res,
500
+ None => Duration :: MAX ,
501
+ }
502
+ }
503
+
453
504
/// Checked `Duration` subtraction. Computes `self - other`, returning [`None`]
454
505
/// if the result would be negative or if overflow occurred.
455
506
///
@@ -485,6 +536,29 @@ impl Duration {
485
536
}
486
537
}
487
538
539
+ /// Saturating `Duration` subtraction. Computes `self - other`, returning [`Duration::MIN`]
540
+ /// if the result would be negative or if overflow occurred.
541
+ ///
542
+ /// # Examples
543
+ ///
544
+ /// ```
545
+ /// #![feature(duration_saturating_ops)]
546
+ /// #![feature(duration_constants)]
547
+ /// use std::time::Duration;
548
+ ///
549
+ /// assert_eq!(Duration::new(0, 1).saturating_sub(Duration::new(0, 0)), Duration::new(0, 1));
550
+ /// assert_eq!(Duration::new(0, 0).saturating_sub(Duration::new(0, 1)), Duration::MIN);
551
+ /// ```
552
+ #[ unstable( feature = "duration_saturating_ops" , issue = "76416" ) ]
553
+ #[ inline]
554
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
555
+ pub const fn saturating_sub ( self , rhs : Duration ) -> Duration {
556
+ match self . checked_sub ( rhs) {
557
+ Some ( res) => res,
558
+ None => Duration :: MIN ,
559
+ }
560
+ }
561
+
488
562
/// Checked `Duration` multiplication. Computes `self * other`, returning
489
563
/// [`None`] if overflow occurred.
490
564
///
@@ -515,6 +589,29 @@ impl Duration {
515
589
None
516
590
}
517
591
592
+ /// Saturating `Duration` multiplication. Computes `self * other`, returning
593
+ /// [`Duration::MAX`] if overflow occurred.
594
+ ///
595
+ /// # Examples
596
+ ///
597
+ /// ```
598
+ /// #![feature(duration_saturating_ops)]
599
+ /// #![feature(duration_constants)]
600
+ /// use std::time::Duration;
601
+ ///
602
+ /// assert_eq!(Duration::new(0, 500_000_001).saturating_mul(2), Duration::new(1, 2));
603
+ /// assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
604
+ /// ```
605
+ #[ unstable( feature = "duration_saturating_ops" , issue = "76416" ) ]
606
+ #[ inline]
607
+ #[ rustc_const_unstable( feature = "duration_consts_2" , issue = "72440" ) ]
608
+ pub const fn saturating_mul ( self , rhs : u32 ) -> Duration {
609
+ match self . checked_mul ( rhs) {
610
+ Some ( res) => res,
611
+ None => Duration :: MAX ,
612
+ }
613
+ }
614
+
518
615
/// Checked `Duration` division. Computes `self / other`, returning [`None`]
519
616
/// if `other == 0`.
520
617
///
0 commit comments