Skip to main content

sark_json/
token.rs

1use o3::buffer::{storage::inline, write::Sink as _};
2
3use crate::Error;
4
5#[repr(transparent)]
6#[derive(Clone, Copy, Debug)]
7pub struct Token<const N: usize>(inline::Bytes<N>);
8
9impl<const N: usize> Token<N> {
10    pub const fn new() -> Self {
11        Self(inline::Bytes::new())
12    }
13
14    pub(crate) fn from_slice(bytes: &[u8]) -> crate::Result<Self> {
15        inline::Bytes::from_slice(bytes)
16            .map(Self)
17            .map_err(Error::Token)
18    }
19
20    pub(crate) fn push(&mut self, byte: u8) -> crate::Result<()> {
21        self.0.write_byte(byte).map_err(Error::Token)
22    }
23
24    pub fn as_bytes(&self) -> &[u8] {
25        self.0.as_ref()
26    }
27
28    pub fn len(&self) -> usize {
29        self.0.len()
30    }
31
32    pub fn is_empty(&self) -> bool {
33        self.0.is_empty()
34    }
35}
36
37impl<const N: usize> Default for Token<N> {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43impl<const N: usize> AsRef<[u8]> for Token<N> {
44    fn as_ref(&self) -> &[u8] {
45        self.as_bytes()
46    }
47}