core2/io/
impls.rs

1use super::error::{Error, ErrorKind, Result};
2use super::traits::{BufRead, Read, Seek, SeekFrom, Write};
3use core::{cmp, fmt, mem};
4
5// =============================================================================
6// Forwarding implementations
7
8impl<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
61// =============================================================================
62// In-memory buffer implementations
63
64/// Read is implemented for `&[u8]` by copying from the slice.
65///
66/// Note that reading updates the slice to point to the yet unread part.
67/// The slice will be empty when EOF is reached.
68impl 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        // First check if the amount of bytes we want to read is small:
75        // `copy_from_slice` will generally expand to a call to `memcpy`, and
76        // for a single byte the overhead is significant.
77        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        // First check if the amount of bytes we want to read is small:
98        // `copy_from_slice` will generally expand to a call to `memcpy`, and
99        // for a single byte the overhead is significant.
100        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
123/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
124/// its data.
125///
126/// Note that writing updates the slice to point to the yet unwritten part.
127/// The slice will be empty when it has been completely overwritten.
128impl 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/// Write is implemented for `Vec<u8>` by appending to the vector.
157/// The vector will grow as needed.
158#[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}