1 #[derive(Clone, Copy)] 2 pub struct True; 3 #[derive(Clone, Copy)] 4 pub struct False; 5 6 impl True { not(&self) -> &'static False7 pub const fn not(&self) -> &'static False { 8 &False 9 } and<'a, T>(&self, other: &'a T) -> &'a T10 pub const fn and<'a, T>(&self, other: &'a T) -> &'a T { 11 other 12 } or<T>(&self, _: &T) -> &'static True13 pub const fn or<T>(&self, _: &T) -> &'static True { 14 &True 15 } value(&self) -> bool16 pub const fn value(&self) -> bool { 17 true 18 } 19 } 20 21 impl False { not(&self) -> &'static True22 pub const fn not(&self) -> &'static True { 23 &True 24 } and<T>(&self, _: &T) -> &'static False25 pub const fn and<T>(&self, _: &T) -> &'static False { 26 &False 27 } or<'a, T>(&self, other: &'a T) -> &'a T28 pub const fn or<'a, T>(&self, other: &'a T) -> &'a T { 29 other 30 } value(&self) -> bool31 pub const fn value(&self) -> bool { 32 false 33 } 34 } 35 36 pub trait ToBool: Sized { 37 type Bool: Sized; 38 const TO_BOOL: Self::Bool; 39 } 40 41 impl ToBool for [(); 0] { 42 type Bool = False; 43 const TO_BOOL: Self::Bool = False; 44 } 45 46 impl ToBool for [(); 1] { 47 type Bool = True; 48 const TO_BOOL: Self::Bool = True; 49 } 50 51 /// Converts a `const bool` to a type-level boolean. 52 #[doc(hidden)] 53 #[macro_export] 54 macro_rules! _to_bool { 55 ($x:expr) => {{ 56 const X: bool = $x; 57 <[(); X as usize] as $crate::_bool::ToBool>::TO_BOOL 58 }}; 59 } 60