Skip to main content

relibc/io/
impls.rs

1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use alloc::string::String;
12use core::{cmp, fmt, mem};
13
14use crate::io::{self, Error, ErrorKind, Initializer, Seek, SeekFrom, Write, prelude::*};
15
16impl<R: Read + ?Sized> Read for &mut R {
17    #[inline]
18    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
19        (**self).read(buf)
20    }
21
22    #[inline]
23    unsafe fn initializer(&self) -> Initializer {
24        unsafe { (**self).initializer() }
25    }
26
27    #[inline]
28    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
29        (**self).read_to_end(buf)
30    }
31
32    #[inline]
33    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
34        (**self).read_to_string(buf)
35    }
36
37    #[inline]
38    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
39        (**self).read_exact(buf)
40    }
41}
42
43impl<W: Write + ?Sized> Write for &mut W {
44    #[inline]
45    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
46        (**self).write(buf)
47    }
48
49    #[inline]
50    fn flush(&mut self) -> io::Result<()> {
51        (**self).flush()
52    }
53
54    #[inline]
55    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
56        (**self).write_all(buf)
57    }
58
59    #[inline]
60    fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
61        (**self).write_fmt(fmt)
62    }
63}
64impl<S: Seek + ?Sized> Seek for &mut S {
65    #[inline]
66    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
67        (**self).seek(pos)
68    }
69}
70
71impl<B: BufRead + ?Sized> BufRead for &mut B {
72    #[inline]
73    fn fill_buf(&mut self) -> io::Result<&[u8]> {
74        (**self).fill_buf()
75    }
76
77    #[inline]
78    fn consume(&mut self, amt: usize) {
79        (**self).consume(amt)
80    }
81
82    #[inline]
83    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
84        (**self).read_until(byte, buf)
85    }
86
87    #[inline]
88    fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
89        (**self).read_line(buf)
90    }
91}
92
93impl<R: Read + ?Sized> Read for Box<R> {
94    #[inline]
95    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
96        (**self).read(buf)
97    }
98
99    #[inline]
100    unsafe fn initializer(&self) -> Initializer {
101        unsafe { (**self).initializer() }
102    }
103
104    #[inline]
105    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
106        (**self).read_to_end(buf)
107    }
108
109    #[inline]
110    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
111        (**self).read_to_string(buf)
112    }
113
114    #[inline]
115    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
116        (**self).read_exact(buf)
117    }
118}
119
120impl<W: Write + ?Sized> Write for Box<W> {
121    #[inline]
122    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
123        (**self).write(buf)
124    }
125
126    #[inline]
127    fn flush(&mut self) -> io::Result<()> {
128        (**self).flush()
129    }
130
131    #[inline]
132    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
133        (**self).write_all(buf)
134    }
135
136    #[inline]
137    fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
138        (**self).write_fmt(fmt)
139    }
140}
141
142impl<S: Seek + ?Sized> Seek for Box<S> {
143    #[inline]
144    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
145        (**self).seek(pos)
146    }
147}
148
149impl<B: BufRead + ?Sized> BufRead for Box<B> {
150    #[inline]
151    fn fill_buf(&mut self) -> io::Result<&[u8]> {
152        (**self).fill_buf()
153    }
154
155    #[inline]
156    fn consume(&mut self, amt: usize) {
157        (**self).consume(amt)
158    }
159
160    #[inline]
161    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
162        (**self).read_until(byte, buf)
163    }
164
165    #[inline]
166    fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
167        (**self).read_line(buf)
168    }
169}
170
171// =============================================================================
172// In-memory buffer implementations
173
174/// Read is implemented for `&[u8]` by copying from the slice.
175///
176/// Note that reading updates the slice to point to the yet unread part.
177/// The slice will be empty when EOF is reached.
178impl Read for &[u8] {
179    #[inline]
180    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
181        let amt = cmp::min(buf.len(), self.len());
182        let (a, b) = self.split_at(amt);
183
184        // First check if the amount of bytes we want to read is small:
185        // `copy_from_slice` will generally expand to a call to `memcpy`, and
186        // for a single byte the overhead is significant.
187        if amt == 1 {
188            buf[0] = a[0];
189        } else {
190            buf[..amt].copy_from_slice(a);
191        }
192
193        *self = b;
194        Ok(amt)
195    }
196
197    #[inline]
198    unsafe fn initializer(&self) -> Initializer {
199        unsafe { Initializer::nop() }
200    }
201
202    #[inline]
203    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
204        if buf.len() > self.len() {
205            return Err(Error::new(
206                ErrorKind::UnexpectedEof,
207                "failed to fill whole buffer",
208            ));
209        }
210        let (a, b) = self.split_at(buf.len());
211
212        // First check if the amount of bytes we want to read is small:
213        // `copy_from_slice` will generally expand to a call to `memcpy`, and
214        // for a single byte the overhead is significant.
215        if buf.len() == 1 {
216            buf[0] = a[0];
217        } else {
218            buf.copy_from_slice(a);
219        }
220
221        *self = b;
222        Ok(())
223    }
224
225    #[inline]
226    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
227        buf.extend_from_slice(self);
228        let len = self.len();
229        *self = &self[len..];
230        Ok(len)
231    }
232}
233
234impl BufRead for &[u8] {
235    #[inline]
236    fn fill_buf(&mut self) -> io::Result<&[u8]> {
237        Ok(*self)
238    }
239
240    #[inline]
241    fn consume(&mut self, amt: usize) {
242        *self = &self[amt..];
243    }
244}
245
246/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
247/// its data.
248///
249/// Note that writing updates the slice to point to the yet unwritten part.
250/// The slice will be empty when it has been completely overwritten.
251impl Write for &mut [u8] {
252    #[inline]
253    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
254        let amt = cmp::min(data.len(), self.len());
255        let (a, b) = mem::take(self).split_at_mut(amt);
256        a.copy_from_slice(&data[..amt]);
257        *self = b;
258        Ok(amt)
259    }
260
261    #[inline]
262    fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
263        if self.write(data)? == data.len() {
264            Ok(())
265        } else {
266            Err(Error::new(
267                ErrorKind::WriteZero,
268                "failed to write whole buffer",
269            ))
270        }
271    }
272
273    #[inline]
274    fn flush(&mut self) -> io::Result<()> {
275        Ok(())
276    }
277}
278
279/// Write is implemented for `Vec<u8>` by appending to the vector.
280/// The vector will grow as needed.
281impl Write for Vec<u8> {
282    #[inline]
283    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
284        self.extend_from_slice(buf);
285        Ok(buf.len())
286    }
287
288    #[inline]
289    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
290        self.extend_from_slice(buf);
291        Ok(())
292    }
293
294    #[inline]
295    fn flush(&mut self) -> io::Result<()> {
296        Ok(())
297    }
298}