summaryrefslogtreecommitdiffstats
path: root/src/writeable.rs
blob: 658b086cff89a5f9406a9e26952d6c34c23ad17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pub trait Writeable: Copy {
    fn to_byte_buf(&self) -> Box<[u8]>;
}

impl Writeable for &str {
    fn to_byte_buf(&self) -> Box<[u8]> {
        return Box::from(self.as_bytes());
    }
}

impl Writeable for char {
    fn to_byte_buf(&self) -> Box<[u8]> {
        let mut buf = [0; 4];
        return self
            .encode_utf8(&mut buf)
            .to_owned()
            .into_boxed_str()
            .into_boxed_bytes();
    }
}