lib0/
encoding.rs

1use crate::number::{Signed, SignedVarInt, VarInt};
2
3impl Write for Vec<u8> {
4    fn write_all(&mut self, buf: &[u8]) {
5        self.extend_from_slice(buf)
6    }
7
8    fn write_u8(&mut self, value: u8) {
9        self.push(value)
10    }
11}
12
13pub trait Write: Sized {
14    fn write_all(&mut self, buf: &[u8]);
15
16    fn write_u8(&mut self, value: u8) {
17        self.write_all(&[value])
18    }
19
20    /// Write an unsigned integer (16bit)
21    fn write_u16(&mut self, num: u16) {
22        self.write_all(&[num as u8, (num >> 8) as u8])
23    }
24
25    /// Write an unsigned integer (32bit)
26    fn write_u32(&mut self, num: u32) {
27        self.write_all(&[
28            num as u8,
29            (num >> 8) as u8,
30            (num >> 16) as u8,
31            (num >> 24) as u8,
32        ])
33    }
34
35    /// Write an unsigned integer (32bit) in big endian order (most significant byte first)
36    fn write_u32_be(&mut self, num: u32) {
37        self.write_all(&[
38            (num >> 24) as u8,
39            (num >> 16) as u8,
40            (num >> 8) as u8,
41            num as u8,
42        ])
43    }
44
45    /// Write a variable length integer or unsigned integer.
46    ///
47    /// We don't use zig-zag encoding because we want to keep the option open
48    /// to use the same function for BigInt and 53bit integers.
49    ///
50    /// We use the 7th bit instead for signaling that this is a negative number.
51    #[inline]
52    fn write_var<T: VarInt>(&mut self, num: T) {
53        num.write(self)
54    }
55
56    /// Write a variable length integer or unsigned integer.
57    ///
58    /// We don't use zig-zag encoding because we want to keep the option open
59    /// to use the same function for BigInt and 53bit integers.
60    ///
61    /// We use the 7th bit instead for signaling that this is a negative number.
62    #[inline]
63    fn write_var_signed<T: SignedVarInt>(&mut self, num: &Signed<T>) {
64        T::write_signed(num, self)
65    }
66
67    /// Write variable length buffer (binary content).
68    fn write_buf<B: AsRef<[u8]>>(&mut self, buf: B) {
69        let buf = buf.as_ref();
70        self.write_var(buf.len());
71        self.write_all(buf)
72    }
73
74    /// Write variable-length utf8 string
75    #[inline]
76    fn write_string(&mut self, str: &str) {
77        self.write_buf(str)
78    }
79
80    /// Write floating point number in 4 bytes
81    #[inline]
82    fn write_f32(&mut self, num: f32) {
83        self.write_all(&num.to_be_bytes())
84    }
85
86    /// Write floating point number in 8 bytes
87    #[inline]
88    fn write_f64(&mut self, num: f64) {
89        self.write_all(&num.to_be_bytes())
90    }
91
92    /// Write BigInt in 8 bytes in big endian order.
93    #[inline]
94    fn write_i64(&mut self, num: i64) {
95        self.write_all(&num.to_be_bytes())
96    }
97
98    /// Write BigUInt in 8 bytes in big endian order.
99    #[inline]
100    fn write_u64(&mut self, num: u64) {
101        self.write_all(&num.to_be_bytes())
102    }
103}