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
62
63
64
65
/// Object that can be written to, e.g. [`crate::Uart`].
pub trait Writer {
    fn write_byte(&mut self, value: u8);
}

pub trait WriterHelper {
    fn write<T>(&mut self, value: T)
    where
        T: Writable;
}

impl<'a> WriterHelper for dyn Writer + 'a {
    fn write<T>(&mut self, value: T)
    where
        T: Writable,
    {
        value.write(self);
    }
}

/// Value that can be transmitted through a [`Writer`].
pub trait Writable {
    fn write(&self, tx: &mut dyn Writer);
}

impl<T> Writable for &T
where
    T: Writable + ?Sized,
{
    fn write(&self, tx: &mut dyn Writer) {
        T::write(self, tx)
    }
}

impl Writable for u8 {
    fn write(&self, tx: &mut dyn Writer) {
        tx.write_byte(*self);
    }
}

impl Writable for [u8] {
    fn write(&self, tx: &mut dyn Writer) {
        for value in self {
            tx.write(value);
        }
    }
}

impl<const N: usize> Writable for [u8; N] {
    fn write(&self, tx: &mut dyn Writer) {
        tx.write(self.as_slice());
    }
}

impl Writable for str {
    fn write(&self, tx: &mut dyn Writer) {
        tx.write(self.as_bytes());
    }
}

impl Writable for String {
    fn write(&self, tx: &mut dyn Writer) {
        tx.write(self.as_str());
    }
}