1use super::error::{Error, ErrorKind, Result};
2use super::traits::{BufRead, Read, Seek, SeekFrom, Write};
3use core::{cmp, fmt, mem};
4
5impl<R: Read + ?Sized> Read for &mut R {
9 #[inline]
10 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
11 (**self).read(buf)
12 }
13
14 #[inline]
15 fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
16 (**self).read_exact(buf)
17 }
18}
19
20impl<W: Write + ?Sized> Write for &mut W {
21 #[inline]
22 fn write(&mut self, buf: &[u8]) -> Result<usize> {
23 (**self).write(buf)
24 }
25
26 #[inline]
27 fn flush(&mut self) -> Result<()> {
28 (**self).flush()
29 }
30
31 #[inline]
32 fn write_all(&mut self, buf: &[u8]) -> Result<()> {
33 (**self).write_all(buf)
34 }
35
36 #[inline]
37 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
38 (**self).write_fmt(fmt)
39 }
40}
41
42impl<S: Seek + ?Sized> Seek for &mut S {
43 #[inline]
44 fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
45 (**self).seek(pos)
46 }
47}
48
49impl<B: BufRead + ?Sized> BufRead for &mut B {
50 #[inline]
51 fn fill_buf(&mut self) -> Result<&[u8]> {
52 (**self).fill_buf()
53 }
54
55 #[inline]
56 fn consume(&mut self, amt: usize) {
57 (**self).consume(amt)
58 }
59}
60
61impl Read for &[u8] {
69 #[inline]
70 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
71 let amt = cmp::min(buf.len(), self.len());
72 let (a, b) = self.split_at(amt);
73
74 if amt == 1 {
78 buf[0] = a[0];
79 } else {
80 buf[..amt].copy_from_slice(a);
81 }
82
83 *self = b;
84 Ok(amt)
85 }
86
87 #[inline]
88 fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
89 if buf.len() > self.len() {
90 return Err(Error::new(
91 ErrorKind::UnexpectedEof,
92 "failed to fill whole buffer",
93 ));
94 }
95 let (a, b) = self.split_at(buf.len());
96
97 if buf.len() == 1 {
101 buf[0] = a[0];
102 } else {
103 buf.copy_from_slice(a);
104 }
105
106 *self = b;
107 Ok(())
108 }
109}
110
111impl BufRead for &[u8] {
112 #[inline]
113 fn fill_buf(&mut self) -> Result<&[u8]> {
114 Ok(*self)
115 }
116
117 #[inline]
118 fn consume(&mut self, amt: usize) {
119 *self = &self[amt..];
120 }
121}
122
123impl Write for &mut [u8] {
129 #[inline]
130 fn write(&mut self, data: &[u8]) -> Result<usize> {
131 let amt = cmp::min(data.len(), self.len());
132 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
133 a.copy_from_slice(&data[..amt]);
134 *self = b;
135 Ok(amt)
136 }
137
138 #[inline]
139 fn write_all(&mut self, data: &[u8]) -> Result<()> {
140 if self.write(data)? == data.len() {
141 Ok(())
142 } else {
143 Err(Error::new(
144 ErrorKind::WriteZero,
145 "failed to write whole buffer",
146 ))
147 }
148 }
149
150 #[inline]
151 fn flush(&mut self) -> Result<()> {
152 Ok(())
153 }
154}
155
156#[cfg(feature = "alloc")]
159impl Write for alloc::vec::Vec<u8> {
160 #[inline]
161 fn write(&mut self, buf: &[u8]) -> Result<usize> {
162 self.extend_from_slice(buf);
163 Ok(buf.len())
164 }
165
166 #[inline]
167 fn write_all(&mut self, buf: &[u8]) -> Result<()> {
168 self.extend_from_slice(buf);
169 Ok(())
170 }
171
172 #[inline]
173 fn flush(&mut self) -> Result<()> {
174 Ok(())
175 }
176}