serbuffer/
buffer.rs

1//! https://github.com/capnproto/capnproto-rust/blob/master/capnp/src/lib.rs
2
3use bytes::{BufMut, BytesMut};
4
5use crate::reader::{BufferMutReader, BufferReader};
6use crate::writer::BufferWriter;
7
8pub mod types {
9    /// types: 0b[type]_[length_mod]
10    /// length = if length_mod == 0 then 0 else 2 << (length_mod -1) .
11    ///     eg: BOOL,I8,U8 = 0
12    ///         I16,U16 = 2 << (1-1) = 2
13    ///         I32,U32,F32 = 2 << (2-1) = 4
14    ///         I64,U64,F64 = 2 << (3-1) = 8
15    pub const BOOL: u8 = 0b0000_0000;
16    pub const I8: u8 = 0b0001_0000;
17    pub const U8: u8 = 0b0010_0000;
18    pub const I16: u8 = 0b0011_0001;
19    pub const U16: u8 = 0b0100_0001;
20    pub const I32: u8 = 0b0101_0010;
21    pub const U32: u8 = 0b0111_0010;
22    pub const I64: u8 = 0b1000_0011;
23    pub const U64: u8 = 0b1001_0011;
24    pub const F32: u8 = 0b1010_0010;
25    pub const F64: u8 = 0b1011_0011;
26
27    pub const BINARY: u8 = 0b1100_0000;
28    pub const STRING: u8 = 0b1100_0001;
29
30    #[inline]
31    pub fn len(data_type_id: u8) -> u8 {
32        let length_mod = data_type_id & 0b0000_1111;
33        if length_mod == 0 {
34            1
35        } else {
36            2 << (length_mod - 1)
37        }
38    }
39}
40
41#[derive(Clone, Debug)]
42pub struct Buffer {
43    pub(crate) buf: BytesMut,
44    pub(crate) buf_len: usize,
45}
46
47impl Buffer {
48    pub fn new() -> Self {
49        Buffer {
50            buf: BytesMut::with_capacity(256),
51            buf_len: 0,
52        }
53    }
54
55    pub fn with_capacity(capacity: usize) -> Self {
56        Buffer {
57            buf: BytesMut::with_capacity(capacity),
58            buf_len: 0,
59        }
60    }
61
62    pub fn from(bytes: BytesMut) -> Self {
63        let buffer_len = bytes.len();
64        Buffer {
65            buf: bytes,
66            buf_len: buffer_len,
67        }
68    }
69
70    pub fn len(&self) -> usize {
71        self.buf_len
72    }
73
74    pub fn as_slice(&self) -> &[u8] {
75        self.buf.as_ref()
76    }
77
78    pub fn extend(&mut self, other: &Buffer) -> Result<(), std::io::Error> {
79        self.buf_len += other.buf_len;
80        self.buf.put_slice(other.as_slice());
81
82        Ok(())
83    }
84
85    pub fn as_reader<'a, 'b>(&'a mut self, data_types: &'b [u8]) -> BufferReader<'a, 'b> {
86        BufferReader::new(self, data_types)
87    }
88
89    pub fn as_reader_mut<'a, 'b>(&'a mut self, data_types: &'b [u8]) -> BufferMutReader<'a, 'b> {
90        BufferMutReader::new(self, data_types)
91    }
92
93    pub fn as_writer<'a, 'b>(&'a mut self, data_types: &'b [u8]) -> BufferWriter<'a, 'b> {
94        BufferWriter::new(self, data_types)
95    }
96}
97
98impl std::cmp::PartialEq for Buffer {
99    fn eq(&self, other: &Self) -> bool {
100        if self.buf_len != self.buf_len {
101            return false;
102        }
103
104        self.as_slice().eq(other.as_slice())
105    }
106}
107
108impl std::cmp::Eq for Buffer {}
109
110impl std::hash::Hash for Buffer {
111    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
112        self.as_slice().hash(state)
113    }
114}