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
51
52
53
54
55
56
57
58
59
60
61
use std::{
    io::{Result as IoResult, Write},
    rc::Rc,
    sync::Arc,
};

/// A trait to allow writing messags to any `std::io::Write` implementation

pub trait Encodable {
    /// Encode this message to the provided `std::io::Write` implementation

    fn encode<W>(&self, buf: &mut W) -> IoResult<()>
    where
        W: Write + ?Sized;
}

impl<T> Encodable for &T
where
    T: Encodable + ?Sized,
{
    fn encode<W>(&self, buf: &mut W) -> IoResult<()>
    where
        W: Write + ?Sized,
    {
        <_ as Encodable>::encode(*self, buf)
    }
}

impl Encodable for str {
    fn encode<W>(&self, buf: &mut W) -> IoResult<()>
    where
        W: Write + ?Sized,
    {
        buf.write_all(self.as_bytes())
    }
}

impl Encodable for String {
    fn encode<W>(&self, buf: &mut W) -> IoResult<()>
    where
        W: Write + ?Sized,
    {
        buf.write_all(self.as_bytes())
    }
}

macro_rules! encodable_byte_slice {
    ($($ty:ty)*) => {
        $(impl Encodable for $ty {
            fn encode<W: Write + ?Sized>(&self, buf: &mut W) -> IoResult<()> {
                buf.write_all(self)
            }
        })*
    };
}

encodable_byte_slice! {
    [u8]
    Box<[u8]>
    Rc<[u8]>
    Arc<[u8]>
    Vec<u8>
}