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
use crate::BytesMut;

#[derive(Default)]
pub struct Writer {
    writer: BytesMut,
}

impl Writer {
    #[inline]
    pub fn write<B: ToBytes>(&mut self, bytes: B) {
        self.writer.extend_from_slice(bytes.to_bytes());
    }

    /// Returns the current buffer, zeroing out self
    pub fn take(&mut self) -> BytesMut {
        self.writer.split_to(self.writer.len())
    }
}

pub trait ToBytes {
    fn to_bytes(&self) -> &[u8];
}
impl ToBytes for &str {
    fn to_bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}
impl ToBytes for &String {
    fn to_bytes(&self) -> &[u8] {
        self.as_bytes()
    }
}
impl ToBytes for &[u8] {
    fn to_bytes(&self) -> &[u8] {
        self
    }
}
impl ToBytes for BytesMut {
    fn to_bytes(&self) -> &[u8] {
        self.as_ref()
    }
}

impl<const N: usize> ToBytes for &[u8; N] {
    fn to_bytes(&self) -> &[u8] {
        *self
    }
}