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
49
50
use bytes::Bytes;
use std::borrow::Cow;

/// Convert binary data into [`bytes::Bytes`].
pub trait IntoBytes {
    /// Convert binary data into [`bytes::Bytes`].
    fn into_bytes(self) -> Bytes;
}

impl IntoBytes for Bytes {
    fn into_bytes(self) -> Bytes {
        self
    }
}

impl IntoBytes for Vec<u8> {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self)
    }
}

impl IntoBytes for &Vec<u8> {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self.clone())
    }
}

impl IntoBytes for &[u8] {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self.to_vec())
    }
}

impl<const N: usize> IntoBytes for &[u8; N] {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self.to_vec())
    }
}

impl IntoBytes for &str {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self.as_bytes().to_vec())
    }
}

impl IntoBytes for Cow<'_, [u8]> {
    fn into_bytes(self) -> Bytes {
        Bytes::from(self.to_vec())
    }
}