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 fn write_u16(&mut self, num: u16) {
22 self.write_all(&[num as u8, (num >> 8) as u8])
23 }
24
25 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 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 #[inline]
52 fn write_var<T: VarInt>(&mut self, num: T) {
53 num.write(self)
54 }
55
56 #[inline]
63 fn write_var_signed<T: SignedVarInt>(&mut self, num: &Signed<T>) {
64 T::write_signed(num, self)
65 }
66
67 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 #[inline]
76 fn write_string(&mut self, str: &str) {
77 self.write_buf(str)
78 }
79
80 #[inline]
82 fn write_f32(&mut self, num: f32) {
83 self.write_all(&num.to_be_bytes())
84 }
85
86 #[inline]
88 fn write_f64(&mut self, num: f64) {
89 self.write_all(&num.to_be_bytes())
90 }
91
92 #[inline]
94 fn write_i64(&mut self, num: i64) {
95 self.write_all(&num.to_be_bytes())
96 }
97
98 #[inline]
100 fn write_u64(&mut self, num: u64) {
101 self.write_all(&num.to_be_bytes())
102 }
103}