1 #![allow(missing_copy_implementations)]
8 self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
11 /// A reader which is always at EOF.
13 /// This struct is generally created by calling [`empty()`]. Please see
14 /// the documentation of [`empty()`] for more details.
15 #[stable(feature = "rust1", since = "1.0.0")]
17 #[derive(Copy, Clone, Default)]
20 /// Constructs a new handle to an empty reader.
22 /// All reads from the returned reader will return <code>[Ok]\(0)</code>.
26 /// A slightly sad example of not reading anything into a buffer:
29 /// use std::io::{self, Read};
31 /// let mut buffer = String::new();
32 /// io::empty().read_to_string(&mut buffer).unwrap();
33 /// assert!(buffer.is_empty());
36 #[stable(feature = "rust1", since = "1.0.0")]
37 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
38 pub const fn empty() -> Empty {
42 #[stable(feature = "rust1", since = "1.0.0")]
45 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
50 fn read_buf(&mut self, _cursor: BorrowedCursor<'_>) -> io::Result<()> {
54 #[stable(feature = "rust1", since = "1.0.0")]
55 impl BufRead for Empty {
57 fn fill_buf(&mut self) -> io::Result<&[u8]> {
61 fn consume(&mut self, _n: usize) {}
64 #[stable(feature = "empty_seek", since = "1.51.0")]
66 fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
70 fn stream_len(&mut self) -> io::Result<u64> {
74 fn stream_position(&mut self) -> io::Result<u64> {
79 #[stable(feature = "std_debug", since = "1.16.0")]
80 impl fmt::Debug for Empty {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 f.debug_struct("Empty").finish_non_exhaustive()
86 impl SizeHint for Empty {
88 fn upper_bound(&self) -> Option<usize> {
93 /// A reader which yields one byte over and over and over and over and over and...
95 /// This struct is generally created by calling [`repeat()`]. Please
96 /// see the documentation of [`repeat()`] for more details.
97 #[stable(feature = "rust1", since = "1.0.0")]
102 /// Creates an instance of a reader that infinitely repeats one byte.
104 /// All reads from this reader will succeed by filling the specified buffer with
110 /// use std::io::{self, Read};
112 /// let mut buffer = [0; 3];
113 /// io::repeat(0b101).read_exact(&mut buffer).unwrap();
114 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
117 #[stable(feature = "rust1", since = "1.0.0")]
118 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
119 pub const fn repeat(byte: u8) -> Repeat {
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl Read for Repeat {
126 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
127 for slot in &mut *buf {
133 fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
134 // SAFETY: No uninit bytes are being written
135 for slot in unsafe { buf.as_mut() } {
136 slot.write(self.byte);
139 let remaining = buf.capacity();
141 // SAFETY: the entire unfilled portion of buf has been initialized
143 buf.advance(remaining);
150 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
151 let mut nwritten = 0;
153 nwritten += self.read(buf)?;
159 fn is_read_vectored(&self) -> bool {
164 impl SizeHint for Repeat {
166 fn lower_bound(&self) -> usize {
171 fn upper_bound(&self) -> Option<usize> {
176 #[stable(feature = "std_debug", since = "1.16.0")]
177 impl fmt::Debug for Repeat {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 f.debug_struct("Repeat").finish_non_exhaustive()
183 /// A writer which will move data into the void.
185 /// This struct is generally created by calling [`sink`]. Please
186 /// see the documentation of [`sink()`] for more details.
187 #[stable(feature = "rust1", since = "1.0.0")]
189 #[derive(Copy, Clone, Default)]
192 /// Creates an instance of a writer which will successfully consume all data.
194 /// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
195 /// and the contents of the buffer will not be inspected.
197 /// [`write`]: Write::write
202 /// use std::io::{self, Write};
204 /// let buffer = vec![1, 2, 3, 5, 8];
205 /// let num_bytes = io::sink().write(&buffer).unwrap();
206 /// assert_eq!(num_bytes, 5);
209 #[stable(feature = "rust1", since = "1.0.0")]
210 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
211 pub const fn sink() -> Sink {
215 #[stable(feature = "rust1", since = "1.0.0")]
216 impl Write for Sink {
218 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
223 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
224 let total_len = bufs.iter().map(|b| b.len()).sum();
229 fn is_write_vectored(&self) -> bool {
234 fn flush(&mut self) -> io::Result<()> {
239 #[stable(feature = "write_mt", since = "1.48.0")]
240 impl Write for &Sink {
242 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
247 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
248 let total_len = bufs.iter().map(|b| b.len()).sum();
253 fn is_write_vectored(&self) -> bool {
258 fn flush(&mut self) -> io::Result<()> {
263 #[stable(feature = "std_debug", since = "1.16.0")]
264 impl fmt::Debug for Sink {
265 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266 f.debug_struct("Sink").finish_non_exhaustive()