| 1 |
use std::{ |
| 2 |
fmt::{ |
| 3 |
self, |
| 4 |
Debug, |
| 5 |
Display, |
| 6 |
LowerHex, |
| 7 |
UpperHex, |
| 8 |
Octal, |
| 9 |
Binary, |
| 10 |
}, |
| 11 |
ops::{ |
| 12 |
BitAnd, |
| 13 |
BitOr, |
| 14 |
BitXor, |
| 15 |
BitAndAssign, |
| 16 |
BitOrAssign, |
| 17 |
BitXorAssign, |
| 18 |
Not, |
| 19 |
}, |
| 20 |
}; |
| 21 |
|
| 22 |
use bitflags::bitflags; |
| 23 |
|
| 24 |
// Ideally we'd actually want this to work, but currently need something like `num`'s `Zero` |
| 25 |
// With some design work it could be made possible |
| 26 |
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 27 |
struct MyInt(u8); |
| 28 |
|
| 29 |
impl BitAnd for MyInt { |
| 30 |
type Output = Self; |
| 31 |
|
| 32 |
fn bitand(self, other: Self) -> Self { |
| 33 |
MyInt(self.0 & other.0) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl BitOr for MyInt { |
| 38 |
type Output = Self; |
| 39 |
|
| 40 |
fn bitor(self, other: Self) -> Self { |
| 41 |
MyInt(self.0 | other.0) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl BitXor for MyInt { |
| 46 |
type Output = Self; |
| 47 |
|
| 48 |
fn bitxor(self, other: Self) -> Self { |
| 49 |
MyInt(self.0 ^ other.0) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl BitAndAssign for MyInt { |
| 54 |
fn bitand_assign(&mut self, other: Self) { |
| 55 |
self.0 &= other.0 |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl BitOrAssign for MyInt { |
| 60 |
fn bitor_assign(&mut self, other: Self) { |
| 61 |
self.0 |= other.0 |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl BitXorAssign for MyInt { |
| 66 |
fn bitxor_assign(&mut self, other: Self) { |
| 67 |
self.0 ^= other.0 |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl Debug for MyInt { |
| 72 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 73 |
Debug::fmt(&self.0, f) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl Display for MyInt { |
| 78 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 79 |
Display::fmt(&self.0, f) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl LowerHex for MyInt { |
| 84 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 85 |
LowerHex::fmt(&self.0, f) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl UpperHex for MyInt { |
| 90 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 91 |
UpperHex::fmt(&self.0, f) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl Octal for MyInt { |
| 96 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 97 |
Octal::fmt(&self.0, f) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl Binary for MyInt { |
| 102 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 103 |
Binary::fmt(&self.0, f) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl Not for MyInt { |
| 108 |
type Output = MyInt; |
| 109 |
|
| 110 |
fn not(self) -> Self { |
| 111 |
MyInt(!self.0) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
bitflags! { |
| 116 |
struct Flags128: MyInt { |
| 117 |
const A = MyInt(0b0000_0001u8); |
| 118 |
const B = MyInt(0b0000_0010u8); |
| 119 |
const C = MyInt(0b0000_0100u8); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
fn main() {} |