1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 macro_rules! sh_impl_signed {
16 ($t:ident, $f:ident) => (
17 #[stable(feature = "rust1", since = "1.0.0")]
18 impl Shl<$f> for Wrapping<$t> {
19 type Output = Wrapping<$t>;
22 fn shl(self, other: $f) -> Wrapping<$t> {
24 Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
26 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
31 #[stable(feature = "op_assign_traits", since = "1.8.0")]
32 impl ShlAssign<$f> for Wrapping<$t> {
34 fn shl_assign(&mut self, other: $f) {
35 *self = *self << other;
39 #[stable(feature = "rust1", since = "1.0.0")]
40 impl Shr<$f> for Wrapping<$t> {
41 type Output = Wrapping<$t>;
44 fn shr(self, other: $f) -> Wrapping<$t> {
46 Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
48 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
53 #[stable(feature = "op_assign_traits", since = "1.8.0")]
54 impl ShrAssign<$f> for Wrapping<$t> {
56 fn shr_assign(&mut self, other: $f) {
57 *self = *self >> other;
63 macro_rules! sh_impl_unsigned {
64 ($t:ident, $f:ident) => (
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl Shl<$f> for Wrapping<$t> {
67 type Output = Wrapping<$t>;
70 fn shl(self, other: $f) -> Wrapping<$t> {
71 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
75 #[stable(feature = "op_assign_traits", since = "1.8.0")]
76 impl ShlAssign<$f> for Wrapping<$t> {
78 fn shl_assign(&mut self, other: $f) {
79 *self = *self << other;
83 #[stable(feature = "rust1", since = "1.0.0")]
84 impl Shr<$f> for Wrapping<$t> {
85 type Output = Wrapping<$t>;
88 fn shr(self, other: $f) -> Wrapping<$t> {
89 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
93 #[stable(feature = "op_assign_traits", since = "1.8.0")]
94 impl ShrAssign<$f> for Wrapping<$t> {
96 fn shr_assign(&mut self, other: $f) {
97 *self = *self >> other;
103 // FIXME (#23545): uncomment the remaining impls
104 macro_rules! sh_impl_all {
105 ($($t:ident)*) => ($(
106 //sh_impl_unsigned! { $t, u8 }
107 //sh_impl_unsigned! { $t, u16 }
108 //sh_impl_unsigned! { $t, u32 }
109 //sh_impl_unsigned! { $t, u64 }
110 sh_impl_unsigned! { $t, usize }
112 //sh_impl_signed! { $t, i8 }
113 //sh_impl_signed! { $t, i16 }
114 //sh_impl_signed! { $t, i32 }
115 //sh_impl_signed! { $t, i64 }
116 //sh_impl_signed! { $t, isize }
120 sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
122 // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
123 macro_rules! wrapping_impl {
125 #[stable(feature = "rust1", since = "1.0.0")]
126 impl Add for Wrapping<$t> {
127 type Output = Wrapping<$t>;
130 fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
131 Wrapping(self.0.wrapping_add(other.0))
134 forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
135 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
137 #[stable(feature = "op_assign_traits", since = "1.8.0")]
138 impl AddAssign for Wrapping<$t> {
140 fn add_assign(&mut self, other: Wrapping<$t>) {
141 *self = *self + other;
145 #[stable(feature = "rust1", since = "1.0.0")]
146 impl Sub for Wrapping<$t> {
147 type Output = Wrapping<$t>;
150 fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
151 Wrapping(self.0.wrapping_sub(other.0))
154 forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
155 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
157 #[stable(feature = "op_assign_traits", since = "1.8.0")]
158 impl SubAssign for Wrapping<$t> {
160 fn sub_assign(&mut self, other: Wrapping<$t>) {
161 *self = *self - other;
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl Mul for Wrapping<$t> {
167 type Output = Wrapping<$t>;
170 fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
171 Wrapping(self.0.wrapping_mul(other.0))
174 forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>,
175 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
177 #[stable(feature = "op_assign_traits", since = "1.8.0")]
178 impl MulAssign for Wrapping<$t> {
180 fn mul_assign(&mut self, other: Wrapping<$t>) {
181 *self = *self * other;
185 #[stable(feature = "wrapping_div", since = "1.3.0")]
186 impl Div for Wrapping<$t> {
187 type Output = Wrapping<$t>;
190 fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
191 Wrapping(self.0.wrapping_div(other.0))
194 forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
195 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
197 #[stable(feature = "op_assign_traits", since = "1.8.0")]
198 impl DivAssign for Wrapping<$t> {
200 fn div_assign(&mut self, other: Wrapping<$t>) {
201 *self = *self / other;
205 #[stable(feature = "wrapping_impls", since = "1.7.0")]
206 impl Rem for Wrapping<$t> {
207 type Output = Wrapping<$t>;
210 fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
211 Wrapping(self.0.wrapping_rem(other.0))
214 forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
215 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
217 #[stable(feature = "op_assign_traits", since = "1.8.0")]
218 impl RemAssign for Wrapping<$t> {
220 fn rem_assign(&mut self, other: Wrapping<$t>) {
221 *self = *self % other;
225 #[stable(feature = "rust1", since = "1.0.0")]
226 impl Not for Wrapping<$t> {
227 type Output = Wrapping<$t>;
230 fn not(self) -> Wrapping<$t> {
234 forward_ref_unop! { impl Not, not for Wrapping<$t>,
235 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
237 #[stable(feature = "rust1", since = "1.0.0")]
238 impl BitXor for Wrapping<$t> {
239 type Output = Wrapping<$t>;
242 fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
243 Wrapping(self.0 ^ other.0)
246 forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
247 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
249 #[stable(feature = "op_assign_traits", since = "1.8.0")]
250 impl BitXorAssign for Wrapping<$t> {
252 fn bitxor_assign(&mut self, other: Wrapping<$t>) {
253 *self = *self ^ other;
257 #[stable(feature = "rust1", since = "1.0.0")]
258 impl BitOr for Wrapping<$t> {
259 type Output = Wrapping<$t>;
262 fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
263 Wrapping(self.0 | other.0)
266 forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
267 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
269 #[stable(feature = "op_assign_traits", since = "1.8.0")]
270 impl BitOrAssign for Wrapping<$t> {
272 fn bitor_assign(&mut self, other: Wrapping<$t>) {
273 *self = *self | other;
277 #[stable(feature = "rust1", since = "1.0.0")]
278 impl BitAnd for Wrapping<$t> {
279 type Output = Wrapping<$t>;
282 fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
283 Wrapping(self.0 & other.0)
286 forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
287 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
289 #[stable(feature = "op_assign_traits", since = "1.8.0")]
290 impl BitAndAssign for Wrapping<$t> {
292 fn bitand_assign(&mut self, other: Wrapping<$t>) {
293 *self = *self & other;
297 #[stable(feature = "wrapping_neg", since = "1.10.0")]
298 impl Neg for Wrapping<$t> {
301 fn neg(self) -> Self {
305 forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
306 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
310 wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
313 #![allow(non_upper_case_globals)]
315 #[cfg(target_pointer_width = "16")]
317 pub const usize: u32 = super::u16;
318 pub const isize: u32 = super::i16;
321 #[cfg(target_pointer_width = "32")]
323 pub const usize: u32 = super::u32;
324 pub const isize: u32 = super::i32;
327 #[cfg(target_pointer_width = "64")]
329 pub const usize: u32 = super::u64;
330 pub const isize: u32 = super::i64;
333 pub const i8: u32 = (1 << 3) - 1;
334 pub const i16: u32 = (1 << 4) - 1;
335 pub const i32: u32 = (1 << 5) - 1;
336 pub const i64: u32 = (1 << 6) - 1;
337 pub use self::platform::isize;
339 pub const u8: u32 = i8;
340 pub const u16: u32 = i16;
341 pub const u32: u32 = i32;
342 pub const u64: u32 = i64;
343 pub use self::platform::usize;