Skip to main content

object_rainbow/
partial_byte_tag.rs

1use std::marker::PhantomData;
2
3use typenum::{ToInt, U1};
4
5use crate::{enumkind::UsizeTag, incr_byte_niche::IncrByteNiche, *};
6
7#[derive(ToOutput, InlineOutput, Tagged, ListHashes, Topological, ParseAsInline)]
8pub struct PartialByteTag<K>(u8, PhantomData<K>);
9
10impl<K> Size for PartialByteTag<K> {
11    type Size = U1;
12    const SIZE: usize = 1;
13}
14
15impl<K: ToInt<u8>, E: ParseInput> ParseInline<E> for PartialByteTag<K> {
16    fn parse_inline(input: &mut E) -> crate::Result<Self> {
17        let n = input.parse_inline()?;
18        if n < K::INT {
19            Ok(Self(n, PhantomData))
20        } else {
21            Err(crate::Error::OutOfBounds)
22        }
23    }
24}
25
26impl<K: ToInt<u8>> UsizeTag for PartialByteTag<K> {
27    fn from_usize(n: usize) -> Self {
28        assert!(n < K::INT as usize);
29        Self(n as _, PhantomData)
30    }
31
32    fn to_usize(&self) -> usize {
33        self.0.to_usize()
34    }
35
36    fn try_to_usize(&self) -> Option<usize> {
37        self.0.try_to_usize()
38    }
39}
40
41impl<K> MaybeHasNiche for PartialByteTag<K> {
42    type MnArray = SomeNiche<IncrByteNiche<K>>;
43}