bitcode/
u8_char.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::coder::{Buffer, Encoder};
use crate::derive::Encode;
use crate::fast::VecImpl;
use alloc::vec::Vec;
use core::num::NonZeroUsize;

/// Represents a single byte of a string, unlike u8 which represents an integer.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct U8Char(pub u8);

// Could derive with bytemuck/derive.
unsafe impl bytemuck::Zeroable for U8Char {}
unsafe impl bytemuck::Pod for U8Char {}

impl Encode for U8Char {
    type Encoder = U8CharEncoder;
}

#[derive(Default)]
pub struct U8CharEncoder(VecImpl<U8Char>);

impl Encoder<U8Char> for U8CharEncoder {
    #[inline(always)]
    fn as_primitive(&mut self) -> Option<&mut VecImpl<U8Char>> {
        Some(&mut self.0)
    }

    #[inline(always)]
    fn encode(&mut self, _: &U8Char) {
        unimplemented!(); // StrEncoder only uses Encoder::as_primitive.
    }
}

impl Buffer for U8CharEncoder {
    fn collect_into(&mut self, out: &mut Vec<u8>) {
        out.extend_from_slice(bytemuck::must_cast_slice(self.0.as_slice()));
        self.0.clear();
    }

    fn reserve(&mut self, _: NonZeroUsize) {
        unimplemented!(); // StrEncoder only uses Encoder::as_primitive.
    }
}