Files
agnostic_orderbook
ahash
aho_corasick
arrayref
arrayvec
atty
base64
bincode
blake3
block_buffer
block_padding
borsh
borsh_derive
borsh_derive_internal
borsh_schema_derive_internal
bs58
bv
bytemuck
byteorder
cfg_if
constant_time_eq
cpufeatures
crunchy
crypto_mac
curve25519_dalek
derivative
dex_v3
digest
either
enumflags2
enumflags2_derive
env_logger
generic_array
getrandom
hashbrown
hex
hmac
hmac_drbg
humantime
itertools
keccak
lazy_static
libc
libm
libsecp256k1
libsecp256k1_core
log
memchr
memmap2
num_derive
num_enum
num_enum_derive
num_traits
opaque_debug
ppv_lite86
proc_macro2
quote
rand
rand_chacha
rand_core
rand_pcg
regex
regex_syntax
rustversion
serde
serde_bytes
serde_derive
sha2
sha3
solana_frozen_abi
solana_frozen_abi_macro
solana_logger
solana_program
solana_sdk_macro
spin
spl_token
subtle
syn
synstructure
termcolor
thiserror
thiserror_impl
typenum
unicode_xid
zeroize
zeroize_derive
  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
//! 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 crate::{private::InternalMarker, Cmp, Equal, Greater, Less, NonZero, PowerOfTwo, Zero};
use core::ops::{BitAnd, BitOr, BitXor, Not};

pub use crate::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 new() -> Self {
        Self
    }
    #[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 new() -> Self {
        Self
    }
    #[inline]
    fn to_u8() -> u8 {
        1
    }
    #[inline]
    fn to_bool() -> bool {
        true
    }
}

impl Zero for B0 {}
impl NonZero for B1 {}
impl PowerOfTwo for B1 {}

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

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

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

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

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

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

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

/// Xor between 0 and 0 ( 0 ^ 0 = 0)
impl BitXor<B0> for B0 {
    type Output = B0;
    #[inline]
    fn bitxor(self, _: B0) -> Self::Output {
        B0
    }
}
/// Xor between 1 and 0 ( 1 ^ 0 = 1)
impl BitXor<B0> for B1 {
    type Output = B1;
    #[inline]
    fn bitxor(self, _: B0) -> Self::Output {
        B1
    }
}
/// Xor between 0 and 1 ( 0 ^ 1 = 1)
impl BitXor<B1> for B0 {
    type Output = B1;
    #[inline]
    fn bitxor(self, _: B1) -> Self::Output {
        B1
    }
}
/// Xor between 1 and 1 ( 1 ^ 1 = 0)
impl BitXor<B1> for B1 {
    type Output = B0;
    #[inline]
    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;

    #[inline]
    fn compare<P: InternalMarker>(&self, _: &B0) -> Self::Output {
        Equal
    }
}

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

    #[inline]
    fn compare<P: InternalMarker>(&self, _: &B1) -> Self::Output {
        Less
    }
}

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

    #[inline]
    fn compare<P: InternalMarker>(&self, _: &B0) -> Self::Output {
        Greater
    }
}

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

    #[inline]
    fn compare<P: InternalMarker>(&self, _: &B1) -> Self::Output {
        Equal
    }
}

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

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

#[cfg(test)]
mod tests {
    #[test]
    fn bit_creation() {
        {
            use crate::{B0, B1};
            let _: B0 = B0::new();
            let _: B1 = B1::new();
        }

        {
            use crate::{Bit, B0, B1};

            let _: B0 = <B0 as Bit>::new();
            let _: B1 = <B1 as Bit>::new();
        }
    }
}