Skip to main content

std/io/
util.rs

1#![allow(missing_copy_implementations)]
2
3#[cfg(test)]
4mod tests;
5
6use crate::fmt;
7use crate::io::{
8    self, BorrowedCursor, BufRead, Empty, IoSlice, IoSliceMut, Read, Repeat, Seek, SeekFrom, Sink,
9    Write,
10};
11
12#[stable(feature = "rust1", since = "1.0.0")]
13impl Read for Empty {
14    #[inline]
15    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
16        Ok(0)
17    }
18
19    #[inline]
20    fn read_buf(&mut self, _cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
21        Ok(())
22    }
23
24    #[inline]
25    fn read_vectored(&mut self, _bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
26        Ok(0)
27    }
28
29    #[inline]
30    fn is_read_vectored(&self) -> bool {
31        // Do not force `Chain<Empty, T>` or `Chain<T, Empty>` to use vectored
32        // reads, unless the other reader is vectored.
33        false
34    }
35
36    #[inline]
37    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
38        if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
39    }
40
41    #[inline]
42    fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
43        if cursor.capacity() != 0 { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
44    }
45
46    #[inline]
47    fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> io::Result<usize> {
48        Ok(0)
49    }
50
51    #[inline]
52    fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
53        Ok(0)
54    }
55}
56#[stable(feature = "rust1", since = "1.0.0")]
57impl BufRead for Empty {
58    #[inline]
59    fn fill_buf(&mut self) -> io::Result<&[u8]> {
60        Ok(&[])
61    }
62
63    #[inline]
64    fn consume(&mut self, _n: usize) {}
65
66    #[inline]
67    fn has_data_left(&mut self) -> io::Result<bool> {
68        Ok(false)
69    }
70
71    #[inline]
72    fn read_until(&mut self, _byte: u8, _buf: &mut Vec<u8>) -> io::Result<usize> {
73        Ok(0)
74    }
75
76    #[inline]
77    fn skip_until(&mut self, _byte: u8) -> io::Result<usize> {
78        Ok(0)
79    }
80
81    #[inline]
82    fn read_line(&mut self, _buf: &mut String) -> io::Result<usize> {
83        Ok(0)
84    }
85}
86
87#[stable(feature = "empty_seek", since = "1.51.0")]
88impl Seek for Empty {
89    #[inline]
90    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
91        Ok(0)
92    }
93
94    #[inline]
95    fn stream_len(&mut self) -> io::Result<u64> {
96        Ok(0)
97    }
98
99    #[inline]
100    fn stream_position(&mut self) -> io::Result<u64> {
101        Ok(0)
102    }
103}
104
105#[stable(feature = "empty_write", since = "1.73.0")]
106impl Write for Empty {
107    #[inline]
108    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
109        Ok(buf.len())
110    }
111
112    #[inline]
113    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
114        let total_len = bufs.iter().map(|b| b.len()).sum();
115        Ok(total_len)
116    }
117
118    #[inline]
119    fn is_write_vectored(&self) -> bool {
120        true
121    }
122
123    #[inline]
124    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
125        Ok(())
126    }
127
128    #[inline]
129    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
130        Ok(())
131    }
132
133    #[inline]
134    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
135        Ok(())
136    }
137
138    #[inline]
139    fn flush(&mut self) -> io::Result<()> {
140        Ok(())
141    }
142}
143
144#[stable(feature = "empty_write", since = "1.73.0")]
145impl Write for &Empty {
146    #[inline]
147    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
148        Ok(buf.len())
149    }
150
151    #[inline]
152    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
153        let total_len = bufs.iter().map(|b| b.len()).sum();
154        Ok(total_len)
155    }
156
157    #[inline]
158    fn is_write_vectored(&self) -> bool {
159        true
160    }
161
162    #[inline]
163    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
164        Ok(())
165    }
166
167    #[inline]
168    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
169        Ok(())
170    }
171
172    #[inline]
173    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
174        Ok(())
175    }
176
177    #[inline]
178    fn flush(&mut self) -> io::Result<()> {
179        Ok(())
180    }
181}
182
183#[stable(feature = "rust1", since = "1.0.0")]
184impl Read for Repeat {
185    #[inline]
186    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
187        buf.fill(self.byte);
188        Ok(buf.len())
189    }
190
191    #[inline]
192    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
193        buf.fill(self.byte);
194        Ok(())
195    }
196
197    #[inline]
198    fn read_buf(&mut self, mut buf: BorrowedCursor<'_, u8>) -> io::Result<()> {
199        // SAFETY: No uninit bytes are being written.
200        unsafe { buf.as_mut() }.write_filled(self.byte);
201        // SAFETY: the entire unfilled portion of buf has been initialized.
202        unsafe { buf.advance(buf.capacity()) };
203        Ok(())
204    }
205
206    #[inline]
207    fn read_buf_exact(&mut self, buf: BorrowedCursor<'_, u8>) -> io::Result<()> {
208        self.read_buf(buf)
209    }
210
211    /// This function is not supported by `io::Repeat`, because there's no end of its data
212    fn read_to_end(&mut self, _: &mut Vec<u8>) -> io::Result<usize> {
213        Err(io::Error::from(io::ErrorKind::OutOfMemory))
214    }
215
216    /// This function is not supported by `io::Repeat`, because there's no end of its data
217    fn read_to_string(&mut self, _: &mut String) -> io::Result<usize> {
218        Err(io::Error::from(io::ErrorKind::OutOfMemory))
219    }
220
221    #[inline]
222    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
223        let mut nwritten = 0;
224        for buf in bufs {
225            nwritten += self.read(buf)?;
226        }
227        Ok(nwritten)
228    }
229
230    #[inline]
231    fn is_read_vectored(&self) -> bool {
232        true
233    }
234}
235
236#[stable(feature = "rust1", since = "1.0.0")]
237impl Write for Sink {
238    #[inline]
239    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
240        Ok(buf.len())
241    }
242
243    #[inline]
244    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
245        let total_len = bufs.iter().map(|b| b.len()).sum();
246        Ok(total_len)
247    }
248
249    #[inline]
250    fn is_write_vectored(&self) -> bool {
251        true
252    }
253
254    #[inline]
255    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
256        Ok(())
257    }
258
259    #[inline]
260    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
261        Ok(())
262    }
263
264    #[inline]
265    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
266        Ok(())
267    }
268
269    #[inline]
270    fn flush(&mut self) -> io::Result<()> {
271        Ok(())
272    }
273}
274
275#[stable(feature = "write_mt", since = "1.48.0")]
276impl Write for &Sink {
277    #[inline]
278    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
279        Ok(buf.len())
280    }
281
282    #[inline]
283    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
284        let total_len = bufs.iter().map(|b| b.len()).sum();
285        Ok(total_len)
286    }
287
288    #[inline]
289    fn is_write_vectored(&self) -> bool {
290        true
291    }
292
293    #[inline]
294    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
295        Ok(())
296    }
297
298    #[inline]
299    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
300        Ok(())
301    }
302
303    #[inline]
304    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
305        Ok(())
306    }
307
308    #[inline]
309    fn flush(&mut self) -> io::Result<()> {
310        Ok(())
311    }
312}