-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbool.rs
59 lines (53 loc) · 1.14 KB
/
bool.rs
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
#[derive(Clone, Copy)]
pub struct True;
#[derive(Clone, Copy)]
pub struct False;
impl True {
pub const fn not(&self) -> &'static False {
&False
}
pub const fn and<'a, T>(&self, other: &'a T) -> &'a T {
other
}
pub const fn or<T>(&self, _: &T) -> &'static True {
&True
}
pub const fn value(&self) -> bool {
true
}
}
impl False {
pub const fn not(&self) -> &'static True {
&True
}
pub const fn and<T>(&self, _: &T) -> &'static False {
&False
}
pub const fn or<'a, T>(&self, other: &'a T) -> &'a T {
other
}
pub const fn value(&self) -> bool {
false
}
}
pub trait ToBool: Sized {
type Bool: Sized;
const TO_BOOL: Self::Bool;
}
impl ToBool for [(); 0] {
type Bool = False;
const TO_BOOL: Self::Bool = False;
}
impl ToBool for [(); 1] {
type Bool = True;
const TO_BOOL: Self::Bool = True;
}
/// Converts a `const bool` to a type-level boolean.
#[doc(hidden)]
#[macro_export]
macro_rules! _to_bool {
($x:expr) => {{
const X: bool = $x;
<[(); X as usize] as $crate::_bool::ToBool>::TO_BOOL
}};
}