Skip to main content

object_rainbow/impls/
bool.rs

1use typenum::{U1, U2};
2
3use crate::{enumkind::UsizeTag, incr_byte_niche::IncrByteNiche, *};
4
5impl ToOutput for bool {
6    fn to_output(&self, output: &mut impl Output) {
7        if output.is_real() {
8            output.write(&[*self as _])
9        }
10    }
11}
12
13impl InlineOutput for bool {}
14
15impl Size for bool {
16    type Size = U1;
17}
18
19impl MaybeHasNiche for bool {
20    type MnArray = SomeNiche<IncrByteNiche<U2>>;
21}
22
23impl<I: ParseInput> Parse<I> for bool {
24    fn parse(input: I) -> crate::Result<Self> {
25        ParseInline::parse_as_inline(input)
26    }
27}
28
29impl<I: ParseInput> ParseInline<I> for bool {
30    fn parse_inline(input: &mut I) -> crate::Result<Self> {
31        match input.parse_chunk::<1>()? {
32            [0] => Ok(false),
33            [1] => Ok(true),
34            [_] => Err(Error::OutOfBounds),
35        }
36    }
37}
38
39impl Tagged for bool {}
40impl ListHashes for bool {}
41impl Topological for bool {}
42
43impl ByteOrd for bool {
44    fn bytes_cmp(&self, other: &Self) -> Ordering {
45        self.cmp(other)
46    }
47}
48
49impl UsizeTag for bool {
50    fn from_usize(n: usize) -> Self {
51        match n {
52            0 => false,
53            1 => true,
54            _ => panic!("out of bounds"),
55        }
56    }
57
58    fn to_usize(&self) -> usize {
59        *self as _
60    }
61
62    fn try_to_usize(&self) -> Option<usize> {
63        Some(self.to_usize())
64    }
65}
66
67#[test]
68fn none_is_2() {
69    assert_eq!(None::<bool>.vec(), [2]);
70}
71
72#[test]
73fn none_none_is_2() {
74    assert_eq!(None::<Option<bool>>.vec(), [3]);
75}