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
use core::ops::{BitAnd, BitOr, BitXor, Not};
use {NonZero, Cmp, Greater, Less, Equal};
pub use marker_traits::Bit;
pub enum B0 {}
impl_derivable!{B0}
pub enum B1 {}
impl_derivable!{B1}
impl Bit for B0 {
#[inline]
fn to_u8() -> u8 {
0
}
#[inline]
fn to_bool() -> bool {
false
}
}
impl Bit for B1 {
#[inline]
fn to_u8() -> u8 {
1
}
#[inline]
fn to_bool() -> bool {
true
}
}
impl NonZero for B1 {}
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());
}
);
}
impl Not for B0 {
type Output = B1;
fn not(self) -> Self::Output {
unreachable!()
}
}
impl Not for B1 {
type Output = B0;
fn not(self) -> Self::Output {
unreachable!()
}
}
impl<Rhs: Bit> BitAnd<Rhs> for B0 {
type Output = B0;
fn bitand(self, _: Rhs) -> Self::Output {
unreachable!()
}
}
impl<Rhs: Bit> BitAnd<Rhs> for B1 {
type Output = Rhs;
fn bitand(self, _: Rhs) -> Self::Output {
unreachable!()
}
}
impl<Rhs: Bit> BitOr<Rhs> for B0 {
type Output = Rhs;
fn bitor(self, _: Rhs) -> Self::Output {
unreachable!()
}
}
impl<Rhs: Bit> BitOr<Rhs> for B1 {
type Output = B1;
fn bitor(self, _: Rhs) -> Self::Output {
unreachable!()
}
}
impl BitXor<B0> for B0 {
type Output = B0;
fn bitxor(self, _: B0) -> Self::Output {
unreachable!()
}
}
impl BitXor<B0> for B1 {
type Output = B1;
fn bitxor(self, _: B0) -> Self::Output {
unreachable!()
}
}
impl BitXor<B1> for B0 {
type Output = B1;
fn bitxor(self, _: B1) -> Self::Output {
unreachable!()
}
}
impl BitXor<B1> for B1 {
type Output = B0;
fn bitxor(self, _: B1) -> Self::Output {
unreachable!()
}
}
#[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;
}