const_arithmetic/binary.rs
1//! Implements all binary types
2
3use super::hex::{_0, _1};
4
5/// Indicates binary types.
6pub trait Binary: Copy {
7 const VALUE: bool;
8}
9impl Binary for _0 { const VALUE: bool = false; }
10impl Binary for _1 { const VALUE: bool = true; }
11
12/// Asserts that a binary equals to _1
13///
14/// Example
15/// ```
16/// use const_arithmetic::*;
17///
18/// fn bin_assert_true<B>(_b: B) where
19/// B: Binary,
20/// B: AssertTrue
21/// {}
22///
23/// bin_assert_true(_1);
24/// ```
25pub trait AssertTrue {}
26impl AssertTrue for _1 {}
27
28/// Asserts that a binary equals to _0
29///
30/// Example
31/// ```
32/// use const_arithmetic::*;
33/// let True = _1;
34/// let False = _0;
35///
36/// fn bin_assert_false<B>(_b: B) where
37/// B: Binary,
38/// B: AssertFalse
39/// {}
40///
41/// bin_assert_false(_0);
42/// ```
43pub trait AssertFalse {}
44impl AssertFalse for _0 {}
45
46/// The NOT logic gate
47///
48/// Example
49/// ```
50/// use const_arithmetic::*;
51/// let True = _1;
52/// let False = _0;
53///
54/// fn bin_not<B, R>(_b: B, _r: R) where
55/// B: Binary,
56/// R: Binary,
57/// B: BinNot<Output = R>
58/// {}
59///
60/// bin_not(_0, True);
61/// bin_not(_1, False);
62/// ```
63pub trait BinNot { type Output: Binary; }
64impl BinNot for _0 { type Output = _1; }
65impl BinNot for _1 { type Output = _0; }
66
67/// The AND logic gate
68///
69/// Example
70/// ```
71/// use const_arithmetic::*;
72/// let True = _1;
73/// let False = _0;
74///
75/// fn bin_and<B1, B2, R>(_h1: B1, _h2: B2, _r: R) where
76/// B1: Binary,
77/// B2: Binary,
78/// R: Binary,
79/// B1: BinAnd<B2, Output = R>
80/// {}
81///
82/// bin_and(_0, _0, False);
83/// bin_and(_1, _0, False);
84/// bin_and(_0, _1, False);
85/// bin_and(_1, _1, True);
86/// ```
87pub trait BinAnd<B: Binary> { type Output: Binary; }
88impl BinAnd<_0> for _0 {type Output = _0;}
89impl BinAnd<_0> for _1 {type Output = _0;}
90impl BinAnd<_1> for _0 {type Output = _0;}
91impl BinAnd<_1> for _1 {type Output = _1;}
92
93/// The OR logic gate
94///
95/// Example
96/// ```
97/// use const_arithmetic::*;
98/// let True = _1;
99/// let False = _0;
100///
101/// fn bin_or<B1, B2, R>(_h1: B1, _h2: B2, _r: R) where
102/// B1: Binary,
103/// B2: Binary,
104/// R: Binary,
105/// B1: BinOr<B2, Output = R>
106/// {}
107///
108/// bin_or(_0, _0, False);
109/// bin_or(_1, _0, True);
110/// bin_or(_0, _1, True);
111/// bin_or(_1, _1, True);
112/// ```
113pub trait BinOr<B: Binary> { type Output: Binary; }
114impl BinOr<_0> for _0 {type Output = _0;}
115impl BinOr<_0> for _1 {type Output = _1;}
116impl BinOr<_1> for _0 {type Output = _1;}
117impl BinOr<_1> for _1 {type Output = _1;}
118
119/// The NOR logic gate
120///
121/// Example
122/// ```
123/// use const_arithmetic::*;
124/// let True = _1;
125/// let False = _0;
126///
127/// fn bin_nor<B1, B2, R>(_h1: B1, _h2: B2, _r: R) where
128/// B1: Binary,
129/// B2: Binary,
130/// R: Binary,
131/// B1: BinNor<B2, Output = R>
132/// {}
133///
134/// bin_nor(_0, _0, True);
135/// bin_nor(_1, _0, False);
136/// bin_nor(_0, _1, False);
137/// bin_nor(_1, _1, False);
138/// ```
139pub trait BinNor<B: Binary> { type Output: Binary; }
140impl BinNor<_0> for _0 {type Output = _1;}
141impl BinNor<_0> for _1 {type Output = _0;}
142impl BinNor<_1> for _0 {type Output = _0;}
143impl BinNor<_1> for _1 {type Output = _0;}
144
145
146/// The NXOR logic gate, or EQUAL operator
147///
148/// Example
149/// ```
150/// use const_arithmetic::*;
151/// let True = _1;
152/// let False = _0;
153///
154/// fn bin_nxor<B1, B2, R>(_h1: B1, _h2: B2, _r: R) where
155/// B1: Binary,
156/// B2: Binary,
157/// R: Binary,
158/// B1: BinEq<B2, Output = R>
159/// {}
160///
161/// bin_nxor(_0, _0, True);
162/// bin_nxor(_1, _0, False);
163/// bin_nxor(_0, _1, False);
164/// bin_nxor(_1, _1, True);
165/// ```
166pub trait BinEq<B: Binary> { type Output: Binary; }
167impl BinEq<_0> for _0 {type Output = _1;}
168impl BinEq<_0> for _1 {type Output = _0;}
169impl BinEq<_1> for _0 {type Output = _0;}
170impl BinEq<_1> for _1 {type Output = _1;}
171
172/// A 8-input AND gate
173pub(crate) trait BinMultiAnd<B1: Binary, B2: Binary, B3: Binary, B4: Binary, B5: Binary, B6: Binary, B7: Binary> { type Output: Binary; }
174impl BinMultiAnd<_1, _1, _1, _1, _1, _1, _1> for _1 { type Output = _1; }
175impl BinMultiAnd<_0, _1, _1, _1, _1, _1, _1> for _1 { type Output = _0; }
176impl<B0: Binary> BinMultiAnd<B0, _0, _1, _1, _1, _1, _1> for _1 { type Output = _0; }
177impl<B0: Binary, B1: Binary> BinMultiAnd<B0, B1, _0, _1, _1, _1, _1> for _1 { type Output = _0; }
178impl<B0: Binary, B1: Binary, B2: Binary> BinMultiAnd<B0, B1, B2, _0, _1, _1, _1> for _1 { type Output = _0; }
179impl<B0: Binary, B1: Binary, B2: Binary, B3: Binary> BinMultiAnd<B0, B1, B2, B3, _0, _1, _1> for _1 { type Output = _0; }
180impl<B0: Binary, B1: Binary, B2: Binary, B3: Binary, B4: Binary> BinMultiAnd<B0, B1, B2, B3, B4, _0, _1> for _1 { type Output = _0; }
181impl<B0: Binary, B1: Binary, B2: Binary, B3: Binary, B4: Binary, B5: Binary> BinMultiAnd<B0, B1, B2, B3, B4, B5, _0> for _1 { type Output = _0; }
182impl<B0: Binary, B1: Binary, B2: Binary, B3: Binary, B4: Binary, B5: Binary, B6: Binary> BinMultiAnd<B0, B1, B2, B3, B4, B5, B6> for _0 { type Output = _0; }