• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
bitand(self, other: Self) -> Self32     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 
bitor(self, other: Self) -> Self40     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 
bitxor(self, other: Self) -> Self48     fn bitxor(self, other: Self) -> Self {
49         MyInt(self.0 ^ other.0)
50     }
51 }
52 
53 impl BitAndAssign for MyInt {
bitand_assign(&mut self, other: Self)54     fn bitand_assign(&mut self, other: Self) {
55         self.0 &= other.0
56     }
57 }
58 
59 impl BitOrAssign for MyInt {
bitor_assign(&mut self, other: Self)60     fn bitor_assign(&mut self, other: Self) {
61         self.0 |= other.0
62     }
63 }
64 
65 impl BitXorAssign for MyInt {
bitxor_assign(&mut self, other: Self)66     fn bitxor_assign(&mut self, other: Self) {
67         self.0 ^= other.0
68     }
69 }
70 
71 impl Debug for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result72     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73         Debug::fmt(&self.0, f)
74     }
75 }
76 
77 impl Display for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result78     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79         Display::fmt(&self.0, f)
80     }
81 }
82 
83 impl LowerHex for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result84     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85         LowerHex::fmt(&self.0, f)
86     }
87 }
88 
89 impl UpperHex for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result90     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91         UpperHex::fmt(&self.0, f)
92     }
93 }
94 
95 impl Octal for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result96     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97         Octal::fmt(&self.0, f)
98     }
99 }
100 
101 impl Binary for MyInt {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result102     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 
not(self) -> Self110     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 
main()123 fn main() {}
124