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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! The [`Write`] trait definition and implementations.

/// A type that writes byte slices.
pub trait Write {
    type Error;

    /// Write the whole byte slice.
    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
}

impl<W: Write + ?Sized> Write for &mut W {
    type Error = W::Error;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        (**self).write_all(buf)
    }
}

impl Write for &mut [u8] {
    type Error = EndOfSlice;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        if self.len() < buf.len() {
            return Err(EndOfSlice(()))
        }
        let this = core::mem::take(self);
        let (prefix, suffix) = this.split_at_mut(buf.len());
        prefix.copy_from_slice(buf);
        *self = suffix;
        Ok(())
    }
}

#[cfg(feature = "alloc")]
impl Write for alloc::vec::Vec<u8> {
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.extend_from_slice(buf);
        Ok(())
    }
}

/// Wrapper around a `Write` impl that keeps track of the write position.
#[derive(Debug)]
pub struct Cursor<W>(W, usize);

impl<W> Cursor<W> {
    pub fn new(w: W) -> Self {
        Cursor(w, 0)
    }

    /// Get the current position.
    pub fn position(&self) -> usize {
        self.1
    }

    /// Access the inner writer.
    pub fn get_ref(&self) -> &W {
        &self.0
    }

    /// Unique access to the inner writer.
    pub fn get_mut(&mut self) -> &mut W {
        &mut self.0
    }

    /// Deconstruct into the inner writer.
    pub fn into_inner(self) -> W {
        self.0
    }
}

impl Write for Cursor<&mut [u8]> {
    type Error = EndOfSlice;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        let mut slice = &mut self.0[self.1 ..];
        slice.write_all(buf)?;
        self.1 += buf.len();
        Ok(())
    }
}

impl<const N: usize> Write for Cursor<[u8; N]> {
    type Error = EndOfArray;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        let mut slice = &mut self.0[self.1 ..];
        slice.write_all(buf).map_err(|_| EndOfArray(()))?;
        self.1 += buf.len();
        Ok(())
    }
}

#[cfg(feature = "alloc")]
impl Write for Cursor<alloc::boxed::Box<[u8]>> {
    type Error = EndOfSlice;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        let mut slice = &mut self.0[self.1 ..];
        slice.write_all(buf)?;
        self.1 += buf.len();
        Ok(())
    }
}

/// An adapter for `std::io::Write` types that implements [`Write`].
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct Writer<W>(W);

#[cfg(feature = "std")]
impl<W> Writer<W> {
    pub fn new(w: W) -> Self {
        Writer(w)
    }

    /// Access the inner writer.
    pub fn get_ref(&self) -> &W {
        &self.0
    }

    /// Unique access to the inner writer.
    pub fn get_mut(&mut self) -> &mut W {
        &mut self.0
    }

    /// Deconstruct into the inner writer.
    pub fn into_inner(self) -> W {
        self.0
    }
}

#[cfg(feature = "std")]
impl<W: std::io::Write> Write for Writer<W> {
    type Error = std::io::Error;

    fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
        std::io::Write::write_all(&mut self.0, buf)
    }
}

/// An error indicating the end of a slice.
#[derive(Debug)]
pub struct EndOfSlice(());

impl core::fmt::Display for EndOfSlice {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.write_str("end of slice")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for EndOfSlice {}

/// An error indicating the end of an array.
#[derive(Debug)]
pub struct EndOfArray(());

impl core::fmt::Display for EndOfArray {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.write_str("end of array")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for EndOfArray {}