diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index d9b14c82e96ba..317555034756d 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -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] @@ -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>; @@ -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] @@ -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>; @@ -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] @@ -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: @@ -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] @@ -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>; @@ -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] @@ -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>; @@ -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] @@ -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>; @@ -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] @@ -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>; @@ -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] @@ -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 } + )*) }