rl2tp/common/
vec_writer.rs

1#[cfg(test)]
2mod tests;
3
4use crate::common::Writer;
5
6#[derive(Clone, Debug, Default, Eq, PartialEq)]
7pub struct VecWriter {
8    pub data: Vec<u8>,
9}
10
11impl VecWriter {
12    #[inline]
13    pub fn new() -> Self {
14        Default::default()
15    }
16}
17
18impl Writer for VecWriter {
19    #[inline]
20    fn len(&self) -> usize {
21        self.data.len()
22    }
23
24    #[inline]
25    fn is_empty(&self) -> bool {
26        self.data.is_empty()
27    }
28
29    #[inline]
30    fn write_bytes(&mut self, bytes: &[u8]) {
31        self.data.extend_from_slice(bytes);
32    }
33
34    #[inline]
35    fn write_bytes_at(&mut self, bytes: &[u8], offset: usize) {
36        assert!(offset + bytes.len() <= self.data.len());
37
38        unsafe {
39            std::ptr::copy_nonoverlapping(
40                bytes.as_ptr(),
41                self.data[offset..].as_mut_ptr(),
42                bytes.len(),
43            );
44        }
45    }
46
47    #[inline]
48    fn write_u8(&mut self, value: u8) {
49        self.data.push(value);
50    }
51
52    #[inline]
53    fn write_u16_be(&mut self, value: u16) {
54        let bytes = value.to_be_bytes();
55        self.data.extend_from_slice(&bytes);
56    }
57
58    #[inline]
59    fn write_u32_be(&mut self, value: u32) {
60        let bytes = value.to_be_bytes();
61        self.data.extend_from_slice(&bytes);
62    }
63
64    #[inline]
65    fn write_u64_be(&mut self, value: u64) {
66        let bytes = value.to_be_bytes();
67        self.data.extend_from_slice(&bytes);
68    }
69}