Skip to main content

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