object_rainbow/impls/
bool.rs

1use std::ops::Add;
2
3use generic_array::GenericArray;
4use typenum::{B0, B1, IsGreater, IsLess, ToInt, U1, U2, U255, U256};
5
6use crate::*;
7
8impl ToOutput for bool {
9    fn to_output(&self, output: &mut dyn Output) {
10        output.write(&[*self as _])
11    }
12}
13
14impl Size for bool {
15    type Size = U1;
16}
17
18pub struct BoolNiche<K>(K);
19
20pub trait NextNiche {
21    type NextNiche;
22}
23
24pub trait WrapNext {
25    type Wrap<J>;
26}
27
28impl WrapNext for B1 {
29    type Wrap<J> = SomeNiche<BoolNiche<J>>;
30}
31
32impl WrapNext for B0 {
33    type Wrap<J> = NoNiche<ZeroNoNiche<U1>>;
34}
35
36impl<
37    K: IsGreater<U1, Output = B1>
38        + IsLess<U256, Output = B1>
39        + Add<B1, Output = J>
40        + IsLess<U255, Output = B>,
41    J,
42    B: WrapNext,
43> NextNiche for K
44{
45    type NextNiche = B::Wrap<J>;
46}
47
48impl<K: ToInt<u8> + NextNiche> Niche for BoolNiche<K> {
49    type NeedsTag = typenum::B0;
50    type N = U1;
51    fn niche() -> GenericArray<u8, Self::N> {
52        GenericArray::from_array([K::INT])
53    }
54    type Next = K::NextNiche;
55}
56
57impl MaybeHasNiche for bool {
58    type MnArray = SomeNiche<BoolNiche<U2>>;
59}
60
61impl<I: ParseInput> Parse<I> for bool {
62    fn parse(input: I) -> crate::Result<Self> {
63        ParseInline::parse_as_inline(input)
64    }
65}
66
67impl<I: ParseInput> ParseInline<I> for bool {
68    fn parse_inline(input: &mut I) -> crate::Result<Self> {
69        match input.parse_chunk::<1>()? {
70            [0] => Ok(false),
71            [1] => Ok(true),
72            [_] => Err(Error::OutOfBounds),
73        }
74    }
75}
76
77impl Tagged for bool {}
78impl Topological for bool {}
79impl<E: 'static> Object<E> for bool {}
80impl<E: 'static> Inline<E> for bool {}
81impl ReflessObject for bool {}
82impl ReflessInline for bool {}
83
84#[test]
85fn none_is_2() {
86    assert_eq!(None::<bool>.vec(), [2]);
87}
88
89#[test]
90fn none_none_is_2() {
91    assert_eq!(None::<Option<bool>>.vec(), [3]);
92}