Skip to content

Commit

Permalink
Auto merge of rust-lang#92356 - kellerkindt:saturating_int_assign_imp…
Browse files Browse the repository at this point in the history
…l, r=dtolnay

Add {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}{,Assign}<$t> to Saturat…

Tracking issue rust-lang#92354
  • Loading branch information
bors committed Jan 16, 2022
2 parents a0984b4 + 1c0dc18 commit 26c06cf
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions library/core/src/num/saturating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl Add<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn add(self, other: $t) -> Saturating<$t> {
Saturating(self.0.saturating_add(other))
}
}
forward_ref_binop! { impl Add, add for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl AddAssign for Saturating<$t> {
#[inline]
Expand All @@ -226,6 +238,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl AddAssign<$t> for Saturating<$t> {
#[inline]
fn add_assign(&mut self, other: $t) {
*self = *self + other;
}
}
forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Sub for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -238,6 +259,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl Sub<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn sub(self, other: $t) -> Saturating<$t> {
Saturating(self.0.saturating_sub(other))
}
}
forward_ref_binop! { impl Sub, sub for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl SubAssign for Saturating<$t> {
#[inline]
Expand All @@ -247,6 +280,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl SubAssign<$t> for Saturating<$t> {
#[inline]
fn sub_assign(&mut self, other: $t) {
*self = *self - other;
}
}
forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Mul for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -259,6 +301,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl Mul<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn mul(self, other: $t) -> Saturating<$t> {
Saturating(self.0.saturating_mul(other))
}
}
forward_ref_binop! { impl Mul, mul for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl MulAssign for Saturating<$t> {
#[inline]
Expand All @@ -268,6 +322,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl MulAssign<$t> for Saturating<$t> {
#[inline]
fn mul_assign(&mut self, other: $t) {
*self = *self * other;
}
}
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, $t }

/// # Examples
///
/// Basic usage:
Expand Down Expand Up @@ -299,6 +362,37 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_int_impl, saturating_int_assign_impl)]
/// use std::num::Saturating;
///
#[doc = concat!("assert_eq!(Saturating(2", stringify!($t), "), Saturating(5", stringify!($t), ") / 2);")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / 1);")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / 1);")]
/// ```
///
/// ```should_panic
/// #![feature(saturating_int_impl, saturating_int_assign_impl)]
/// use std::num::Saturating;
///
#[doc = concat!("let _ = Saturating(0", stringify!($t), ") / 0;")]
/// ```
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl Div<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn div(self, other: $t) -> Saturating<$t> {
Saturating(self.0.saturating_div(other))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl DivAssign for Saturating<$t> {
#[inline]
Expand All @@ -308,6 +402,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl DivAssign<$t> for Saturating<$t> {
#[inline]
fn div_assign(&mut self, other: $t) {
*self = *self / other;
}
}
forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Rem for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -320,6 +423,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl Rem, rem for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl Rem<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn rem(self, other: $t) -> Saturating<$t> {
Saturating(self.0.rem(other))
}
}
forward_ref_binop! { impl Rem, rem for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl RemAssign for Saturating<$t> {
#[inline]
Expand All @@ -329,6 +444,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl RemAssign<$t> for Saturating<$t> {
#[inline]
fn rem_assign(&mut self, other: $t) {
*self = *self % other;
}
}
forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Not for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -353,6 +477,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitXor<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn bitxor(self, other: $t) -> Saturating<$t> {
Saturating(self.0 ^ other)
}
}
forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl BitXorAssign for Saturating<$t> {
#[inline]
Expand All @@ -362,6 +498,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitXorAssign<$t> for Saturating<$t> {
#[inline]
fn bitxor_assign(&mut self, other: $t) {
*self = *self ^ other;
}
}
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl BitOr for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -374,6 +519,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitOr<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn bitor(self, other: $t) -> Saturating<$t> {
Saturating(self.0 | other)
}
}
forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl BitOrAssign for Saturating<$t> {
#[inline]
Expand All @@ -383,6 +540,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitOrAssign<$t> for Saturating<$t> {
#[inline]
fn bitor_assign(&mut self, other: $t) {
*self = *self | other;
}
}
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, $t }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl BitAnd for Saturating<$t> {
type Output = Saturating<$t>;
Expand All @@ -395,6 +561,18 @@ macro_rules! saturating_impl {
forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitAnd<$t> for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn bitand(self, other: $t) -> Saturating<$t> {
Saturating(self.0 & other)
}
}
forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, $t,
#[unstable(feature = "saturating_int_assign_impl", issue = "92354")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl BitAndAssign for Saturating<$t> {
#[inline]
Expand All @@ -404,6 +582,15 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t> }

#[unstable(feature = "saturating_int_assign_impl", issue = "92354")]
impl BitAndAssign<$t> for Saturating<$t> {
#[inline]
fn bitand_assign(&mut self, other: $t) {
*self = *self & other;
}
}
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, $t }

)*)
}

Expand Down

0 comments on commit 26c06cf

Please sign in to comment.