tycho/collections/
bytes.rs

1use std::ops::{Deref, DerefMut};
2
3/// Maps to `Vec<u8>`, an unsized array of bytes
4pub struct Bytes(pub Vec<u8>);
5
6
7impl Default for Bytes {
8    fn default() -> Self {
9        Self(Vec::new())
10    }
11}
12
13impl Deref for Bytes {
14    type Target = Vec<u8>;
15
16    fn deref(&self) -> &Self::Target {
17        &self.0
18    }
19}
20
21impl DerefMut for Bytes {
22    fn deref_mut(&mut self) -> &mut Self::Target {
23        &mut self.0
24    }
25}
26
27impl Bytes {
28    /// Create a new array of bytes
29    pub fn new() -> Self {
30        Self(Vec::new())
31    }
32}
33
34#[cfg(feature="serde")]
35use serde::{Serialize, Serializer};
36
37#[cfg(feature="serde")]
38impl Serialize for Bytes {
39    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
40        S: Serializer {
41        serializer.serialize_bytes(&self.0)
42    }
43}