      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

    //! Type-level bits.
    //!
    //! These are rather simple and are used as the building blocks of the
    //! other number types in this crate.
    //!
    //!
    //! **Type operators** implemented:
    //!
    //! - From `core::ops`: `BitAnd`, `BitOr`, `BitXor`, and `Not`.
    //! - From `typenum`: `Same` and `Cmp`.
    //!

    use core::ops::{BitAnd, BitOr, BitXor, Not};
    use {Cmp, Equal, Greater, Less, NonZero, PowerOfTwo};

    pub use marker_traits::Bit;

    /// The type-level bit 0.
    #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
    pub struct B0;

    impl B0 {
        /// Instantiates a singleton representing this bit.
        #[inline]
        pub fn new() -> B0 {
            B0
        }
    }

    /// The type-level bit 1.
    #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
    pub struct B1;

    impl B1 {
        /// Instantiates a singleton representing this bit.
        #[inline]
        pub fn new() -> B1 {
            B1
        }
    }

    impl Bit for B0 {
        const U8: u8 = 0;
        const BOOL: bool = false;

        #[inline]
        fn to_u8() -> u8 {
            0
        }
        #[inline]
        fn to_bool() -> bool {
            false
        }
    }

    impl Bit for B1 {
        const U8: u8 = 1;
        const BOOL: bool = true;

        #[inline]
        fn to_u8() -> u8 {
            1
        }
        #[inline]
        fn to_bool() -> bool {
            true
        }
    }

    impl NonZero for B1 {}
    impl PowerOfTwo for B1 {}

    /// Not of 0 (!0 = 1)
    impl Not for B0 {
        type Output = B1;
        fn not(self) -> Self::Output {
            B1
        }
    }
    /// Not of 1 (!1 = 0)
    impl Not for B1 {
        type Output = B0;
        fn not(self) -> Self::Output {
            B0
        }
    }

    /// And with 0 ( 0 & B = 0)
    impl<Rhs: Bit> BitAnd<Rhs> for B0 {
        type Output = B0;
        fn bitand(self, _: Rhs) -> Self::Output {
            B0
        }
    }

    /// And with 1 ( 1 & 0 = 0)
    impl BitAnd<B0> for B1 {
        type Output = B0;
        fn bitand(self, _: B0) -> Self::Output {
            B0
        }
    }

    /// And with 1 ( 1 & 1 = 1)
    impl BitAnd<B1> for B1 {
        type Output = B1;
        fn bitand(self, _: B1) -> Self::Output {
            B1
        }
    }

    /// Or with 0 ( 0 | 0 = 0)
    impl BitOr<B0> for B0 {
        type Output = B0;
        fn bitor(self, _: B0) -> Self::Output {
            B0
        }
    }

    /// Or with 0 ( 0 | 1 = 1)
    impl BitOr<B1> for B0 {
        type Output = B1;
        fn bitor(self, _: B1) -> Self::Output {
            B1
        }
    }

    /// Or with 1 ( 1 | B = 1)
    impl<Rhs: Bit> BitOr<Rhs> for B1 {
        type Output = B1;
        fn bitor(self, _: Rhs) -> Self::Output {
            B1
        }
    }

    /// Xor between 0 and 0 ( 0 ^ 0 = 0)
    impl BitXor<B0> for B0 {
        type Output = B0;
        fn bitxor(self, _: B0) -> Self::Output {
            B0
        }
    }
    /// Xor between 1 and 0 ( 1 ^ 0 = 1)
    impl BitXor<B0> for B1 {
        type Output = B1;
        fn bitxor(self, _: B0) -> Self::Output {
            B1
        }
    }
    /// Xor between 0 and 1 ( 0 ^ 1 = 1)
    impl BitXor<B1> for B0 {
        type Output = B1;
        fn bitxor(self, _: B1) -> Self::Output {
            B1
        }
    }
    /// Xor between 1 and 1 ( 1 ^ 1 = 0)
    impl BitXor<B1> for B1 {
        type Output = B0;
        fn bitxor(self, _: B1) -> Self::Output {
            B0
        }
    }

    #[cfg(tests)]
    mod tests {
        // macro for testing operation results. Uses `Same` to ensure the types are equal and
        // not just the values they evaluate to.
        macro_rules! test_bit_op {
            ($op:ident $Lhs:ident = $Answer:ident) => (
                {
                    type Test = <<$Lhs as $op>::Output as ::Same<$Answer>>::Output;
                    assert_eq!(<$Answer as Bit>::to_u8(), <Test as Bit>::to_u8());
                }
            );
            ($Lhs:ident $op:ident $Rhs:ident = $Answer:ident) => (
                {
                    type Test = <<$Lhs as $op<$Rhs>>::Output as ::Same<$Answer>>::Output;
                    assert_eq!(<$Answer as Bit>::to_u8(), <Test as Bit>::to_u8());
                }
            );
        }

        #[test]
        fn bit_operations() {
            test_bit_op!(Not B0 = B1);
            test_bit_op!(Not B1 = B0);

            test_bit_op!(B0 BitAnd B0 = B0);
            test_bit_op!(B0 BitAnd B1 = B0);
            test_bit_op!(B1 BitAnd B0 = B0);
            test_bit_op!(B1 BitAnd B1 = B1);

            test_bit_op!(B0 BitOr B0 = B0);
            test_bit_op!(B0 BitOr B1 = B1);
            test_bit_op!(B1 BitOr B0 = B1);
            test_bit_op!(B1 BitOr B1 = B1);

            test_bit_op!(B0 BitXor B0 = B0);
            test_bit_op!(B0 BitXor B1 = B1);
            test_bit_op!(B1 BitXor B0 = B1);
            test_bit_op!(B1 BitXor B1 = B0);
        }
    }

    impl Cmp<B0> for B0 {
        type Output = Equal;
    }

    impl Cmp<B1> for B0 {
        type Output = Less;
    }

    impl Cmp<B0> for B1 {
        type Output = Greater;
    }

    impl Cmp<B1> for B1 {
        type Output = Equal;
    }

    use Min;
    impl Min<B0> for B0 {
        type Output = B0;
        fn min(self, _: B0) -> B0 {
            self
        }
    }
    impl Min<B1> for B0 {
        type Output = B0;
        fn min(self, _: B1) -> B0 {
            self
        }
    }
    impl Min<B0> for B1 {
        type Output = B0;
        fn min(self, rhs: B0) -> B0 {
            rhs
        }
    }
    impl Min<B1> for B1 {
        type Output = B1;
        fn min(self, _: B1) -> B1 {
            self
        }
    }

    use Max;
    impl Max<B0> for B0 {
        type Output = B0;
        fn max(self, _: B0) -> B0 {
            self
        }
    }
    impl Max<B1> for B0 {
        type Output = B1;
        fn max(self, rhs: B1) -> B1 {
            rhs
        }
    }
    impl Max<B0> for B1 {
        type Output = B1;
        fn max(self, _: B0) -> B1 {
            self
        }
    }
    impl Max<B1> for B1 {
        type Output = B1;
        fn max(self, _: B1) -> B1 {
            self
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse 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`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)