wrapping.rs.html -- source
Description: Source to the Rust file `src/libcore/num/wrapping.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357

    // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    use super::Wrapping;

    use ops::*;

    #[allow(unused_macros)]
    macro_rules! sh_impl_signed {
        ($t:ident, $f:ident) => (
            #[stable(feature = "rust1", since = "1.0.0")]
            impl Shl<$f> for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn shl(self, other: $f) -> Wrapping<$t> {
                    if other < 0 {
                        Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
                    } else {
                        Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
                    }
                }
            }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl ShlAssign<$f> for Wrapping<$t> {
                #[inline]
                fn shl_assign(&mut self, other: $f) {
                    *self = *self << other;
                }
            }
            forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl Shr<$f> for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn shr(self, other: $f) -> Wrapping<$t> {
                    if other < 0 {
                        Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
                    } else {
                        Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
                    }
                }
            }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl ShrAssign<$f> for Wrapping<$t> {
                #[inline]
                fn shr_assign(&mut self, other: $f) {
                    *self = *self >> other;
                }
            }
            forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
        )
    }

    macro_rules! sh_impl_unsigned {
        ($t:ident, $f:ident) => (
            #[stable(feature = "rust1", since = "1.0.0")]
            impl Shl<$f> for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn shl(self, other: $f) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
                }
            }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl ShlAssign<$f> for Wrapping<$t> {
                #[inline]
                fn shl_assign(&mut self, other: $f) {
                    *self = *self << other;
                }
            }
            forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl Shr<$f> for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn shr(self, other: $f) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
                }
            }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl ShrAssign<$f> for Wrapping<$t> {
                #[inline]
                fn shr_assign(&mut self, other: $f) {
                    *self = *self >> other;
                }
            }
            forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
        )
    }

    // FIXME (#23545): uncomment the remaining impls
    macro_rules! sh_impl_all {
        ($($t:ident)*) => ($(
            //sh_impl_unsigned! { $t, u8 }
            //sh_impl_unsigned! { $t, u16 }
            //sh_impl_unsigned! { $t, u32 }
            //sh_impl_unsigned! { $t, u64 }
            sh_impl_unsigned! { $t, usize }

            //sh_impl_signed! { $t, i8 }
            //sh_impl_signed! { $t, i16 }
            //sh_impl_signed! { $t, i32 }
            //sh_impl_signed! { $t, i64 }
            //sh_impl_signed! { $t, isize }
        )*)
    }

    sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }

    // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
    macro_rules! wrapping_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            impl Add for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_add(other.0))
                }
            }
            forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl AddAssign for Wrapping<$t> {
                #[inline]
                fn add_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self + other;
                }
            }
            forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl Sub for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_sub(other.0))
                }
            }
            forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl SubAssign for Wrapping<$t> {
                #[inline]
                fn sub_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self - other;
                }
            }
            forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl Mul for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_mul(other.0))
                }
            }
            forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl MulAssign for Wrapping<$t> {
                #[inline]
                fn mul_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self * other;
                }
            }
            forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "wrapping_div", since = "1.3.0")]
            impl Div for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_div(other.0))
                }
            }
            forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl DivAssign for Wrapping<$t> {
                #[inline]
                fn div_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self / other;
                }
            }
            forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "wrapping_impls", since = "1.7.0")]
            impl Rem for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0.wrapping_rem(other.0))
                }
            }
            forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl RemAssign for Wrapping<$t> {
                #[inline]
                fn rem_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self % other;
                }
            }
            forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl Not for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn not(self) -> Wrapping<$t> {
                    Wrapping(!self.0)
                }
            }
            forward_ref_unop! { impl Not, not for Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl BitXor for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0 ^ other.0)
                }
            }
            forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl BitXorAssign for Wrapping<$t> {
                #[inline]
                fn bitxor_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self ^ other;
                }
            }
            forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl BitOr for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0 | other.0)
                }
            }
            forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl BitOrAssign for Wrapping<$t> {
                #[inline]
                fn bitor_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self | other;
                }
            }
            forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "rust1", since = "1.0.0")]
            impl BitAnd for Wrapping<$t> {
                type Output = Wrapping<$t>;

                #[inline]
                fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
                    Wrapping(self.0 & other.0)
                }
            }
            forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }

            #[stable(feature = "op_assign_traits", since = "1.8.0")]
            impl BitAndAssign for Wrapping<$t> {
                #[inline]
                fn bitand_assign(&mut self, other: Wrapping<$t>) {
                    *self = *self & other;
                }
            }
            forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }

            #[stable(feature = "wrapping_neg", since = "1.10.0")]
            impl Neg for Wrapping<$t> {
                type Output = Self;
                #[inline]
                fn neg(self) -> Self {
                    Wrapping(0) - self
                }
            }
            forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
                    #[stable(feature = "wrapping_ref", since = "1.14.0")] }
        )*)
    }

    wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

    mod shift_max {
        #![allow(non_upper_case_globals)]

        #[cfg(target_pointer_width = "16")]
        mod platform {
            pub const usize: u32 = super::u16;
            pub const isize: u32 = super::i16;
        }

        #[cfg(target_pointer_width = "32")]
        mod platform {
            pub const usize: u32 = super::u32;
            pub const isize: u32 = super::i32;
        }

        #[cfg(target_pointer_width = "64")]
        mod platform {
            pub const usize: u32 = super::u64;
            pub const isize: u32 = super::i64;
        }

        pub const i8: u32 = (1 << 3) - 1;
        pub const i16: u32 = (1 << 4) - 1;
        pub const i32: u32 = (1 << 5) - 1;
        pub const i64: u32 = (1 << 6) - 1;
        pub use self::platform::isize;

        pub const u8: u32 = i8;
        pub const u16: u32 = i16;
        pub const u32: u32 = i32;
        pub const u64: u32 = i64;
        pub use self::platform::usize;
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)