@@ -4,7 +4,7 @@ use crate::fmt;
44use crate :: ops:: { Add , AddAssign , BitAnd , BitAndAssign , BitOr , BitOrAssign } ;
55use crate :: ops:: { BitXor , BitXorAssign , Div , DivAssign } ;
66use crate :: ops:: { Mul , MulAssign , Neg , Not , Rem , RemAssign } ;
7- use crate :: ops:: { Shl , ShlAssign , Shr , ShrAssign , Sub , SubAssign } ;
7+ use crate :: ops:: { Sub , SubAssign } ;
88
99/// Provides intentionally-saturating arithmetic on `T`.
1010///
@@ -79,129 +79,132 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Saturating<T> {
7979 self . 0 . fmt ( f)
8080 }
8181}
82- #[ allow( unused_macros) ]
83- macro_rules! sh_impl_signed {
84- ( $t: ident, $f: ident) => {
85- // FIXME what is the correct implementation here? see discussion https://github.com/rust-lang/rust/pull/87921#discussion_r695870065
86- //
87- // #[unstable(feature = "saturating_int_impl", issue = "87920")]
88- // impl Shl<$f> for Saturating<$t> {
89- // type Output = Saturating<$t>;
90- //
91- // #[inline]
92- // fn shl(self, other: $f) -> Saturating<$t> {
93- // if other < 0 {
94- // Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32))
95- // } else {
96- // Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32))
97- // }
98- // }
99- // }
100- // forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f,
101- // #[unstable(feature = "saturating_int_impl", issue = "87920")] }
102- //
103- // #[unstable(feature = "saturating_int_impl", issue = "87920")]
104- // impl ShlAssign<$f> for Saturating<$t> {
105- // #[inline]
106- // fn shl_assign(&mut self, other: $f) {
107- // *self = *self << other;
108- // }
109- // }
110- // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f }
11182
112- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
113- impl Shr <$f> for Saturating <$t> {
114- type Output = Saturating <$t>;
115-
116- #[ inline]
117- fn shr( self , other: $f) -> Saturating <$t> {
118- if other < 0 {
119- Saturating ( self . 0 . shl( ( -other & self :: shift_max:: $t as $f) as u32 ) )
120- } else {
121- Saturating ( self . 0 . shr( ( other & self :: shift_max:: $t as $f) as u32 ) )
122- }
123- }
124- }
125- forward_ref_binop! { impl Shr , shr for Saturating <$t>, $f,
126- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ] }
127-
128- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
129- impl ShrAssign <$f> for Saturating <$t> {
130- #[ inline]
131- fn shr_assign( & mut self , other: $f) {
132- * self = * self >> other;
133- }
134- }
135- forward_ref_op_assign! { impl ShrAssign , shr_assign for Saturating <$t>, $f }
136- } ;
137- }
138-
139- macro_rules! sh_impl_unsigned {
140- ( $t: ident, $f: ident) => {
141- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
142- impl Shl <$f> for Saturating <$t> {
143- type Output = Saturating <$t>;
144-
145- #[ inline]
146- fn shl( self , other: $f) -> Saturating <$t> {
147- Saturating ( self . 0 . wrapping_shl( other as u32 ) )
148- }
149- }
150- forward_ref_binop! { impl Shl , shl for Saturating <$t>, $f,
151- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ] }
152-
153- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
154- impl ShlAssign <$f> for Saturating <$t> {
155- #[ inline]
156- fn shl_assign( & mut self , other: $f) {
157- * self = * self << other;
158- }
159- }
160- forward_ref_op_assign! { impl ShlAssign , shl_assign for Saturating <$t>, $f }
161-
162- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
163- impl Shr <$f> for Saturating <$t> {
164- type Output = Saturating <$t>;
165-
166- #[ inline]
167- fn shr( self , other: $f) -> Saturating <$t> {
168- Saturating ( self . 0 . wrapping_shr( other as u32 ) )
169- }
170- }
171- forward_ref_binop! { impl Shr , shr for Saturating <$t>, $f,
172- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ] }
173-
174- #[ unstable( feature = "saturating_int_impl" , issue = "87920" ) ]
175- impl ShrAssign <$f> for Saturating <$t> {
176- #[ inline]
177- fn shr_assign( & mut self , other: $f) {
178- * self = * self >> other;
179- }
180- }
181- forward_ref_op_assign! { impl ShrAssign , shr_assign for Saturating <$t>, $f }
182- } ;
183- }
184-
185- // FIXME (#23545): uncomment the remaining impls
186- macro_rules! sh_impl_all {
187- ( $( $t: ident) * ) => ( $(
188- //sh_impl_unsigned! { $t, u8 }
189- //sh_impl_unsigned! { $t, u16 }
190- //sh_impl_unsigned! { $t, u32 }
191- //sh_impl_unsigned! { $t, u64 }
192- //sh_impl_unsigned! { $t, u128 }
193- sh_impl_unsigned! { $t, usize }
194-
195- //sh_impl_signed! { $t, i8 }
196- //sh_impl_signed! { $t, i16 }
197- //sh_impl_signed! { $t, i32 }
198- //sh_impl_signed! { $t, i64 }
199- //sh_impl_signed! { $t, i128 }
200- //sh_impl_signed! { $t, isize }
201- ) * )
202- }
203-
204- sh_impl_all ! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
83+ // FIXME the correct implementation is not clear. Waiting for a real world use case at https://github.com/rust-lang/libs-team/issues/230
84+ //
85+ // #[allow(unused_macros)]
86+ // macro_rules! sh_impl_signed {
87+ // ($t:ident, $f:ident) => {
88+ // // FIXME what is the correct implementation here? see discussion https://github.com/rust-lang/rust/pull/87921#discussion_r695870065
89+ // //
90+ // // #[unstable(feature = "saturating_int_impl", issue = "87920")]
91+ // // impl Shl<$f> for Saturating<$t> {
92+ // // type Output = Saturating<$t>;
93+ // //
94+ // // #[inline]
95+ // // fn shl(self, other: $f) -> Saturating<$t> {
96+ // // if other < 0 {
97+ // // Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32))
98+ // // } else {
99+ // // Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32))
100+ // // }
101+ // // }
102+ // // }
103+ // // forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f,
104+ // // #[unstable(feature = "saturating_int_impl", issue = "87920")] }
105+ // //
106+ // // #[unstable(feature = "saturating_int_impl", issue = "87920")]
107+ // // impl ShlAssign<$f> for Saturating<$t> {
108+ // // #[inline]
109+ // // fn shl_assign(&mut self, other: $f) {
110+ // // *self = *self << other;
111+ // // }
112+ // // }
113+ // // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f }
114+ //
115+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
116+ // impl Shr<$f> for Saturating<$t> {
117+ // type Output = Saturating<$t>;
118+ //
119+ // #[inline]
120+ // fn shr(self, other: $f) -> Saturating<$t> {
121+ // if other < 0 {
122+ // Saturating(self.0.shl((-other & self::shift_max::$t as $f) as u32))
123+ // } else {
124+ // Saturating(self.0.shr((other & self::shift_max::$t as $f) as u32))
125+ // }
126+ // }
127+ // }
128+ // forward_ref_binop! { impl Shr, shr for Saturating<$t>, $f,
129+ // #[unstable(feature = "saturating_int_impl", issue = "87920")] }
130+ //
131+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
132+ // impl ShrAssign<$f> for Saturating<$t> {
133+ // #[inline]
134+ // fn shr_assign(&mut self, other: $f) {
135+ // *self = *self >> other;
136+ // }
137+ // }
138+ // forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f }
139+ // };
140+ // }
141+ //
142+ // macro_rules! sh_impl_unsigned {
143+ // ($t:ident, $f:ident) => {
144+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
145+ // impl Shl<$f> for Saturating<$t> {
146+ // type Output = Saturating<$t>;
147+ //
148+ // #[inline]
149+ // fn shl(self, other: $f) -> Saturating<$t> {
150+ // Saturating(self.0.wrapping_shl(other as u32))
151+ // }
152+ // }
153+ // forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f,
154+ // #[unstable(feature = "saturating_int_impl", issue = "87920")] }
155+ //
156+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
157+ // impl ShlAssign<$f> for Saturating<$t> {
158+ // #[inline]
159+ // fn shl_assign(&mut self, other: $f) {
160+ // *self = *self << other;
161+ // }
162+ // }
163+ // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f }
164+ //
165+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
166+ // impl Shr<$f> for Saturating<$t> {
167+ // type Output = Saturating<$t>;
168+ //
169+ // #[inline]
170+ // fn shr(self, other: $f) -> Saturating<$t> {
171+ // Saturating(self.0.wrapping_shr(other as u32))
172+ // }
173+ // }
174+ // forward_ref_binop! { impl Shr, shr for Saturating<$t>, $f,
175+ // #[unstable(feature = "saturating_int_impl", issue = "87920")] }
176+ //
177+ // #[unstable(feature = "saturating_int_impl", issue = "87920")]
178+ // impl ShrAssign<$f> for Saturating<$t> {
179+ // #[inline]
180+ // fn shr_assign(&mut self, other: $f) {
181+ // *self = *self >> other;
182+ // }
183+ // }
184+ // forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f }
185+ // };
186+ // }
187+ //
188+ // // FIXME (#23545): uncomment the remaining impls
189+ // macro_rules! sh_impl_all {
190+ // ($($t:ident)*) => ($(
191+ // //sh_impl_unsigned! { $t, u8 }
192+ // //sh_impl_unsigned! { $t, u16 }
193+ // //sh_impl_unsigned! { $t, u32 }
194+ // //sh_impl_unsigned! { $t, u64 }
195+ // //sh_impl_unsigned! { $t, u128 }
196+ // sh_impl_unsigned! { $t, usize }
197+ //
198+ // //sh_impl_signed! { $t, i8 }
199+ // //sh_impl_signed! { $t, i16 }
200+ // //sh_impl_signed! { $t, i32 }
201+ // //sh_impl_signed! { $t, i64 }
202+ // //sh_impl_signed! { $t, i128 }
203+ // //sh_impl_signed! { $t, isize }
204+ // )*)
205+ // }
206+ //
207+ // sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
205208
206209// FIXME(30524): impl Op<T> for Saturating<T>, impl OpAssign<T> for Saturating<T>
207210macro_rules! saturating_impl {
0 commit comments