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    SizeHint, 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<'_>) -> 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<'_>) -> 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
105impl SizeHint for Empty {
106    #[inline]
107    fn upper_bound(&self) -> Option<usize> {
108        Some(0)
109    }
110}
111
112#[stable(feature = "empty_write", since = "1.73.0")]
113impl Write for Empty {
114    #[inline]
115    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
116        Ok(buf.len())
117    }
118
119    #[inline]
120    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
121        let total_len = bufs.iter().map(|b| b.len()).sum();
122        Ok(total_len)
123    }
124
125    #[inline]
126    fn is_write_vectored(&self) -> bool {
127        true
128    }
129
130    #[inline]
131    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
132        Ok(())
133    }
134
135    #[inline]
136    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
137        Ok(())
138    }
139
140    #[inline]
141    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
142        Ok(())
143    }
144
145    #[inline]
146    fn flush(&mut self) -> io::Result<()> {
147        Ok(())
148    }
149}
150
151#[stable(feature = "empty_write", since = "1.73.0")]
152impl Write for &Empty {
153    #[inline]
154    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
155        Ok(buf.len())
156    }
157
158    #[inline]
159    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
160        let total_len = bufs.iter().map(|b| b.len()).sum();
161        Ok(total_len)
162    }
163
164    #[inline]
165    fn is_write_vectored(&self) -> bool {
166        true
167    }
168
169    #[inline]
170    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
171        Ok(())
172    }
173
174    #[inline]
175    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
176        Ok(())
177    }
178
179    #[inline]
180    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
181        Ok(())
182    }
183
184    #[inline]
185    fn flush(&mut self) -> io::Result<()> {
186        Ok(())
187    }
188}
189
190#[stable(feature = "rust1", since = "1.0.0")]
191impl Read for Repeat {
192    #[inline]
193    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
194        buf.fill(self.byte);
195        Ok(buf.len())
196    }
197
198    #[inline]
199    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
200        buf.fill(self.byte);
201        Ok(())
202    }
203
204    #[inline]
205    fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
206        // SAFETY: No uninit bytes are being written.
207        unsafe { buf.as_mut() }.write_filled(self.byte);
208        // SAFETY: the entire unfilled portion of buf has been initialized.
209        unsafe { buf.advance(buf.capacity()) };
210        Ok(())
211    }
212
213    #[inline]
214    fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
215        self.read_buf(buf)
216    }
217
218    /// This function is not supported by `io::Repeat`, because there's no end of its data
219    fn read_to_end(&mut self, _: &mut Vec<u8>) -> io::Result<usize> {
220        Err(io::Error::from(io::ErrorKind::OutOfMemory))
221    }
222
223    /// This function is not supported by `io::Repeat`, because there's no end of its data
224    fn read_to_string(&mut self, _: &mut String) -> io::Result<usize> {
225        Err(io::Error::from(io::ErrorKind::OutOfMemory))
226    }
227
228    #[inline]
229    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
230        let mut nwritten = 0;
231        for buf in bufs {
232            nwritten += self.read(buf)?;
233        }
234        Ok(nwritten)
235    }
236
237    #[inline]
238    fn is_read_vectored(&self) -> bool {
239        true
240    }
241}
242
243impl SizeHint for Repeat {
244    #[inline]
245    fn lower_bound(&self) -> usize {
246        usize::MAX
247    }
248
249    #[inline]
250    fn upper_bound(&self) -> Option<usize> {
251        None
252    }
253}
254
255#[stable(feature = "rust1", since = "1.0.0")]
256impl Write for Sink {
257    #[inline]
258    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
259        Ok(buf.len())
260    }
261
262    #[inline]
263    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
264        let total_len = bufs.iter().map(|b| b.len()).sum();
265        Ok(total_len)
266    }
267
268    #[inline]
269    fn is_write_vectored(&self) -> bool {
270        true
271    }
272
273    #[inline]
274    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
275        Ok(())
276    }
277
278    #[inline]
279    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
280        Ok(())
281    }
282
283    #[inline]
284    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
285        Ok(())
286    }
287
288    #[inline]
289    fn flush(&mut self) -> io::Result<()> {
290        Ok(())
291    }
292}
293
294#[stable(feature = "write_mt", since = "1.48.0")]
295impl Write for &Sink {
296    #[inline]
297    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
298        Ok(buf.len())
299    }
300
301    #[inline]
302    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
303        let total_len = bufs.iter().map(|b| b.len()).sum();
304        Ok(total_len)
305    }
306
307    #[inline]
308    fn is_write_vectored(&self) -> bool {
309        true
310    }
311
312    #[inline]
313    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
314        Ok(())
315    }
316
317    #[inline]
318    fn write_all_vectored(&mut self, _bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
319        Ok(())
320    }
321
322    #[inline]
323    fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> io::Result<()> {
324        Ok(())
325    }
326
327    #[inline]
328    fn flush(&mut self) -> io::Result<()> {
329        Ok(())
330    }
331}