1 #![allow(missing_copy_implementations)]
7 use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Write};
9 /// A reader which is always at EOF.
11 /// This struct is generally created by calling [`empty()`]. Please see
12 /// the documentation of [`empty()`] for more details.
13 #[stable(feature = "rust1", since = "1.0.0")]
18 /// Constructs a new handle to an empty reader.
20 /// All reads from the returned reader will return [`Ok`]`(0)`.
24 /// A slightly sad example of not reading anything into a buffer:
27 /// use std::io::{self, Read};
29 /// let mut buffer = String::new();
30 /// io::empty().read_to_string(&mut buffer).unwrap();
31 /// assert!(buffer.is_empty());
33 #[stable(feature = "rust1", since = "1.0.0")]
34 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
35 pub const fn empty() -> Empty {
39 #[stable(feature = "rust1", since = "1.0.0")]
42 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
47 unsafe fn initializer(&self) -> Initializer {
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl BufRead for Empty {
54 fn fill_buf(&mut self) -> io::Result<&[u8]> {
58 fn consume(&mut self, _n: usize) {}
61 #[stable(feature = "std_debug", since = "1.16.0")]
62 impl fmt::Debug for Empty {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 /// A reader which yields one byte over and over and over and over and over and...
70 /// This struct is generally created by calling [`repeat()`]. Please
71 /// see the documentation of [`repeat()`] for more details.
72 #[stable(feature = "rust1", since = "1.0.0")]
77 /// Creates an instance of a reader that infinitely repeats one byte.
79 /// All reads from this reader will succeed by filling the specified buffer with
85 /// use std::io::{self, Read};
87 /// let mut buffer = [0; 3];
88 /// io::repeat(0b101).read_exact(&mut buffer).unwrap();
89 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
91 #[stable(feature = "rust1", since = "1.0.0")]
92 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
93 pub const fn repeat(byte: u8) -> Repeat {
97 #[stable(feature = "rust1", since = "1.0.0")]
98 impl Read for Repeat {
100 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
101 for slot in &mut *buf {
108 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
109 let mut nwritten = 0;
111 nwritten += self.read(buf)?;
117 fn is_read_vectored(&self) -> bool {
122 unsafe fn initializer(&self) -> Initializer {
127 #[stable(feature = "std_debug", since = "1.16.0")]
128 impl fmt::Debug for Repeat {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 f.pad("Repeat { .. }")
134 /// A writer which will move data into the void.
136 /// This struct is generally created by calling [`sink`]. Please
137 /// see the documentation of [`sink()`] for more details.
138 #[stable(feature = "rust1", since = "1.0.0")]
143 /// Creates an instance of a writer which will successfully consume all data.
145 /// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
146 /// and the contents of the buffer will not be inspected.
148 /// [`write`]: Write::write
153 /// use std::io::{self, Write};
155 /// let buffer = vec![1, 2, 3, 5, 8];
156 /// let num_bytes = io::sink().write(&buffer).unwrap();
157 /// assert_eq!(num_bytes, 5);
159 #[stable(feature = "rust1", since = "1.0.0")]
160 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
161 pub const fn sink() -> Sink {
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl Write for Sink {
168 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
173 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
174 let total_len = bufs.iter().map(|b| b.len()).sum();
179 fn is_write_vectored(&self) -> bool {
184 fn flush(&mut self) -> io::Result<()> {
189 #[stable(feature = "write_mt", since = "1.48.0")]
190 impl Write for &Sink {
192 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
197 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
198 let total_len = bufs.iter().map(|b| b.len()).sum();
203 fn is_write_vectored(&self) -> bool {
208 fn flush(&mut self) -> io::Result<()> {
213 #[stable(feature = "std_debug", since = "1.16.0")]
214 impl fmt::Debug for Sink {
215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {