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))
135 #[stable(feature = "op_assign_traits", since = "1.8.0")]
136 impl AddAssign for Wrapping<$t> {
138 fn add_assign(&mut self, other: Wrapping<$t>) {
139 *self = *self + other;
143 #[stable(feature = "rust1", since = "1.0.0")]
144 impl Sub for Wrapping<$t> {
145 type Output = Wrapping<$t>;
148 fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
149 Wrapping(self.0.wrapping_sub(other.0))
153 #[stable(feature = "op_assign_traits", since = "1.8.0")]
154 impl SubAssign for Wrapping<$t> {
156 fn sub_assign(&mut self, other: Wrapping<$t>) {
157 *self = *self - other;
161 #[stable(feature = "rust1", since = "1.0.0")]
162 impl Mul for Wrapping<$t> {
163 type Output = Wrapping<$t>;
166 fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
167 Wrapping(self.0.wrapping_mul(other.0))
171 #[stable(feature = "op_assign_traits", since = "1.8.0")]
172 impl MulAssign for Wrapping<$t> {
174 fn mul_assign(&mut self, other: Wrapping<$t>) {
175 *self = *self * other;
179 #[stable(feature = "wrapping_div", since = "1.3.0")]
180 impl Div for Wrapping<$t> {
181 type Output = Wrapping<$t>;
184 fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
185 Wrapping(self.0.wrapping_div(other.0))
189 #[stable(feature = "op_assign_traits", since = "1.8.0")]
190 impl DivAssign for Wrapping<$t> {
192 fn div_assign(&mut self, other: Wrapping<$t>) {
193 *self = *self / other;
197 #[stable(feature = "wrapping_impls", since = "1.7.0")]
198 impl Rem for Wrapping<$t> {
199 type Output = Wrapping<$t>;
202 fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
203 Wrapping(self.0.wrapping_rem(other.0))
207 #[stable(feature = "op_assign_traits", since = "1.8.0")]
208 impl RemAssign for Wrapping<$t> {
210 fn rem_assign(&mut self, other: Wrapping<$t>) {
211 *self = *self % other;
215 #[stable(feature = "rust1", since = "1.0.0")]
216 impl Not for Wrapping<$t> {
217 type Output = Wrapping<$t>;
220 fn not(self) -> Wrapping<$t> {
225 #[stable(feature = "rust1", since = "1.0.0")]
226 impl BitXor for Wrapping<$t> {
227 type Output = Wrapping<$t>;
230 fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
231 Wrapping(self.0 ^ other.0)
235 #[stable(feature = "op_assign_traits", since = "1.8.0")]
236 impl BitXorAssign for Wrapping<$t> {
238 fn bitxor_assign(&mut self, other: Wrapping<$t>) {
239 *self = *self ^ other;
243 #[stable(feature = "rust1", since = "1.0.0")]
244 impl BitOr for Wrapping<$t> {
245 type Output = Wrapping<$t>;
248 fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
249 Wrapping(self.0 | other.0)
253 #[stable(feature = "op_assign_traits", since = "1.8.0")]
254 impl BitOrAssign for Wrapping<$t> {
256 fn bitor_assign(&mut self, other: Wrapping<$t>) {
257 *self = *self | other;
261 #[stable(feature = "rust1", since = "1.0.0")]
262 impl BitAnd for Wrapping<$t> {
263 type Output = Wrapping<$t>;
266 fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
267 Wrapping(self.0 & other.0)
271 #[stable(feature = "op_assign_traits", since = "1.8.0")]
272 impl BitAndAssign for Wrapping<$t> {
274 fn bitand_assign(&mut self, other: Wrapping<$t>) {
275 *self = *self & other;
279 #[stable(feature = "wrapping_neg", since = "1.10.0")]
280 impl Neg for Wrapping<$t> {
283 fn neg(self) -> Self {
290 wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
293 #![allow(non_upper_case_globals)]
295 #[cfg(target_pointer_width = "16")]
297 pub const usize: u32 = super::u16;
298 pub const isize: u32 = super::i16;
301 #[cfg(target_pointer_width = "32")]
303 pub const usize: u32 = super::u32;
304 pub const isize: u32 = super::i32;
307 #[cfg(target_pointer_width = "64")]
309 pub const usize: u32 = super::u64;
310 pub const isize: u32 = super::i64;
313 pub const i8: u32 = (1 << 3) - 1;
314 pub const i16: u32 = (1 << 4) - 1;
315 pub const i32: u32 = (1 << 5) - 1;
316 pub const i64: u32 = (1 << 6) - 1;
317 pub use self::platform::isize;
319 pub const u8: u32 = i8;
320 pub const u16: u32 = i16;
321 pub const u32: u32 = i32;
322 pub const u64: u32 = i64;
323 pub use self::platform::usize;