Skip to main content

std/io/
util.rs

1#![allow(missing_copy_implementations)]
2
3#[cfg(test)]
4mod tests;
5
6use crate::io::{self, BorrowedCursor, BufRead, Empty, IoSliceMut, Read, Repeat};
7
8#[stable(feature = "rust1", since = "1.0.0")]
9impl Read for Empty {
10    #[inline]
11    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
12        Ok(0)
13    }
14
15    #[inline]
16    fn read_buf(&mut self, _cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
17        Ok(())
18    }
19
20    #[inline]
21    fn read_vectored(&mut self, _bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
22        Ok(0)
23    }
24
25    #[inline]
26    fn is_read_vectored(&self) -> bool {
27        // Do not force `Chain<Empty, T>` or `Chain<T, Empty>` to use vectored
28        // reads, unless the other reader is vectored.
29        false
30    }
31
32    #[inline]
33    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
34        if !buf.is_empty() { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
35    }
36
37    #[inline]
38    fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_, u8>) -> io::Result<()> {
39        if cursor.capacity() != 0 { Err(io::Error::READ_EXACT_EOF) } else { Ok(()) }
40    }
41
42    #[inline]
43    fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> io::Result<usize> {
44        Ok(0)
45    }
46
47    #[inline]
48    fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
49        Ok(0)
50    }
51}
52#[stable(feature = "rust1", since = "1.0.0")]
53impl BufRead for Empty {
54    #[inline]
55    fn fill_buf(&mut self) -> io::Result<&[u8]> {
56        Ok(&[])
57    }
58
59    #[inline]
60    fn consume(&mut self, _n: usize) {}
61
62    #[inline]
63    fn has_data_left(&mut self) -> io::Result<bool> {
64        Ok(false)
65    }
66
67    #[inline]
68    fn read_until(&mut self, _byte: u8, _buf: &mut Vec<u8>) -> io::Result<usize> {
69        Ok(0)
70    }
71
72    #[inline]
73    fn skip_until(&mut self, _byte: u8) -> io::Result<usize> {
74        Ok(0)
75    }
76
77    #[inline]
78    fn read_line(&mut self, _buf: &mut String) -> io::Result<usize> {
79        Ok(0)
80    }
81}
82
83#[stable(feature = "rust1", since = "1.0.0")]
84impl Read for Repeat {
85    #[inline]
86    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
87        buf.fill(self.byte);
88        Ok(buf.len())
89    }
90
91    #[inline]
92    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
93        buf.fill(self.byte);
94        Ok(())
95    }
96
97    #[inline]
98    fn read_buf(&mut self, mut buf: BorrowedCursor<'_, u8>) -> io::Result<()> {
99        // SAFETY: No uninit bytes are being written.
100        unsafe { buf.as_mut() }.write_filled(self.byte);
101        // SAFETY: the entire unfilled portion of buf has been initialized.
102        unsafe { buf.advance(buf.capacity()) };
103        Ok(())
104    }
105
106    #[inline]
107    fn read_buf_exact(&mut self, buf: BorrowedCursor<'_, u8>) -> io::Result<()> {
108        self.read_buf(buf)
109    }
110
111    /// This function is not supported by `io::Repeat`, because there's no end of its data
112    fn read_to_end(&mut self, _: &mut Vec<u8>) -> io::Result<usize> {
113        Err(io::Error::from(io::ErrorKind::OutOfMemory))
114    }
115
116    /// This function is not supported by `io::Repeat`, because there's no end of its data
117    fn read_to_string(&mut self, _: &mut String) -> io::Result<usize> {
118        Err(io::Error::from(io::ErrorKind::OutOfMemory))
119    }
120
121    #[inline]
122    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
123        let mut nwritten = 0;
124        for buf in bufs {
125            nwritten += self.read(buf)?;
126        }
127        Ok(nwritten)
128    }
129
130    #[inline]
131    fn is_read_vectored(&self) -> bool {
132        true
133    }
134}