Skip to content

Commit 677b517

Browse files
committed
Auto merge of rust-lang#87921 - kellerkindt:master, r=kennytm
Add Saturating type (based on Wrapping type) Tracking rust-lang#87920 ### Unresolved Questions <!-- Include any open questions that need to be answered before the feature can be stabilised. --> - [x] ~`impl Div for Saturating<T>` falls back on inner integer division - which seems alright?~ - [x] add `saturating_div`? (to respect division by `-1`) - [x] There is no `::saturating_shl` and `::saturating_shr`. (How to) implement `Shl`, `ShlAssign`, `Shr` and `ShrAssign`? - [naively](3f7d2ce) - [x] ~`saturating_neg` is only implemented on [signed integer types](https://doc.rust-lang.org/std/?search=saturating_n)~ - [x] Is the implementation copied over from the `Wrapping`-type correct for `Saturating`? - [x] `Saturating::rotate_left` - [x] `Saturating::rotate_right` - [x] `Not` - [x] `BitXorOr` and `BitXorOrAssign` - [x] `BitOr` and `BitOrAssign` - [x] `BitAnd` and `BitAndAssign` - [x] `Saturating::swap_bytes` - [x] `Saturating::reverse_bits`
2 parents 5eacec9 + ce636f2 commit 677b517

File tree

6 files changed

+1048
-0
lines changed

6 files changed

+1048
-0
lines changed

library/core/src/num/int_macros.rs

+34
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,40 @@ macro_rules! int_impl {
918918
}
919919
}
920920

921+
/// Saturating integer division. Computes `self / rhs`, saturating at the
922+
/// numeric bounds instead of overflowing.
923+
///
924+
/// # Examples
925+
///
926+
/// Basic usage:
927+
///
928+
/// ```
929+
/// #![feature(saturating_div)]
930+
///
931+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
932+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
933+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
934+
///
935+
/// ```
936+
///
937+
/// ```should_panic
938+
/// #![feature(saturating_div)]
939+
///
940+
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
941+
///
942+
/// ```
943+
#[unstable(feature = "saturating_div", issue = "87920")]
944+
#[rustc_const_unstable(feature = "saturating_div", issue = "87920")]
945+
#[must_use = "this returns the result of the operation, \
946+
without modifying the original"]
947+
#[inline]
948+
pub const fn saturating_div(self, rhs: Self) -> Self {
949+
match self.overflowing_div(rhs) {
950+
(result, false) => result,
951+
(_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow
952+
}
953+
}
954+
921955
/// Saturating integer exponentiation. Computes `self.pow(exp)`,
922956
/// saturating at the numeric bounds instead of overflowing.
923957
///

library/core/src/num/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ mod uint_macros; // import uint_impl!
4343
mod error;
4444
mod int_log10;
4545
mod nonzero;
46+
#[unstable(feature = "saturating_int_impl", issue = "87920")]
47+
mod saturating;
4648
mod wrapping;
4749

50+
#[unstable(feature = "saturating_int_impl", issue = "87920")]
51+
pub use saturating::Saturating;
4852
#[stable(feature = "rust1", since = "1.0.0")]
4953
pub use wrapping::Wrapping;
5054

0 commit comments

Comments
 (0)