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