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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#[cfg(test)]
mod tests;

use crate::common::Writer;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct VecWriter {
    pub data: Vec<u8>,
}

impl VecWriter {
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }
}

impl Writer for VecWriter {
    #[inline]
    fn len(&self) -> usize {
        self.data.len()
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    #[inline]
    unsafe fn write_bytes_unchecked(&mut self, bytes: &[u8]) {
        self.data.extend_from_slice(bytes);
    }

    #[inline]
    unsafe fn write_bytes_unchecked_at(&mut self, bytes: &[u8], offset: usize) {
        assert!(offset + bytes.len() <= self.data.len());
        std::ptr::copy_nonoverlapping(
            bytes.as_ptr(),
            self.data[offset..].as_mut_ptr(),
            bytes.len(),
        );
    }

    #[inline]
    unsafe fn write_u8_unchecked(&mut self, value: u8) {
        self.data.push(value);
    }

    #[inline]
    unsafe fn write_u16_be_unchecked(&mut self, value: u16) {
        let bytes = value.to_be_bytes();
        self.data.extend_from_slice(&bytes);
    }

    #[inline]
    unsafe fn write_u32_be_unchecked(&mut self, value: u32) {
        let bytes = value.to_be_bytes();
        self.data.extend_from_slice(&bytes);
    }

    #[inline]
    unsafe fn write_u64_be_unchecked(&mut self, value: u64) {
        let bytes = value.to_be_bytes();
        self.data.extend_from_slice(&bytes);
    }
}