]> git.proxmox.com Git - rustc.git/blob - library/std/src/io/util.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / library / std / src / io / util.rs
1 #![allow(missing_copy_implementations)]
2
3 #[cfg(test)]
4 mod tests;
5
6 use crate::fmt;
7 use crate::io::{
8 self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
9 };
10
11 /// A reader which is always at EOF.
12 ///
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")]
16 #[non_exhaustive]
17 #[derive(Copy, Clone, Default)]
18 pub struct Empty;
19
20 /// Constructs a new handle to an empty reader.
21 ///
22 /// All reads from the returned reader will return <code>[Ok]\(0)</code>.
23 ///
24 /// # Examples
25 ///
26 /// A slightly sad example of not reading anything into a buffer:
27 ///
28 /// ```
29 /// use std::io::{self, Read};
30 ///
31 /// let mut buffer = String::new();
32 /// io::empty().read_to_string(&mut buffer).unwrap();
33 /// assert!(buffer.is_empty());
34 /// ```
35 #[must_use]
36 #[stable(feature = "rust1", since = "1.0.0")]
37 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
38 pub const fn empty() -> Empty {
39 Empty
40 }
41
42 #[stable(feature = "rust1", since = "1.0.0")]
43 impl Read for Empty {
44 #[inline]
45 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
46 Ok(0)
47 }
48
49 #[inline]
50 unsafe fn initializer(&self) -> Initializer {
51 Initializer::nop()
52 }
53 }
54 #[stable(feature = "rust1", since = "1.0.0")]
55 impl BufRead for Empty {
56 #[inline]
57 fn fill_buf(&mut self) -> io::Result<&[u8]> {
58 Ok(&[])
59 }
60 #[inline]
61 fn consume(&mut self, _n: usize) {}
62 }
63
64 #[stable(feature = "empty_seek", since = "1.51.0")]
65 impl Seek for Empty {
66 fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
67 Ok(0)
68 }
69
70 fn stream_len(&mut self) -> io::Result<u64> {
71 Ok(0)
72 }
73
74 fn stream_position(&mut self) -> io::Result<u64> {
75 Ok(0)
76 }
77 }
78
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()
83 }
84 }
85
86 impl SizeHint for Empty {
87 #[inline]
88 fn upper_bound(&self) -> Option<usize> {
89 Some(0)
90 }
91 }
92
93 /// A reader which yields one byte over and over and over and over and over and...
94 ///
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")]
98 pub struct Repeat {
99 byte: u8,
100 }
101
102 /// Creates an instance of a reader that infinitely repeats one byte.
103 ///
104 /// All reads from this reader will succeed by filling the specified buffer with
105 /// the given byte.
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// use std::io::{self, Read};
111 ///
112 /// let mut buffer = [0; 3];
113 /// io::repeat(0b101).read_exact(&mut buffer).unwrap();
114 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
115 /// ```
116 #[must_use]
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 {
120 Repeat { byte }
121 }
122
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl Read for Repeat {
125 #[inline]
126 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
127 for slot in &mut *buf {
128 *slot = self.byte;
129 }
130 Ok(buf.len())
131 }
132
133 #[inline]
134 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
135 let mut nwritten = 0;
136 for buf in bufs {
137 nwritten += self.read(buf)?;
138 }
139 Ok(nwritten)
140 }
141
142 #[inline]
143 fn is_read_vectored(&self) -> bool {
144 true
145 }
146
147 #[inline]
148 unsafe fn initializer(&self) -> Initializer {
149 Initializer::nop()
150 }
151 }
152
153 impl SizeHint for Repeat {
154 #[inline]
155 fn lower_bound(&self) -> usize {
156 usize::MAX
157 }
158
159 #[inline]
160 fn upper_bound(&self) -> Option<usize> {
161 None
162 }
163 }
164
165 #[stable(feature = "std_debug", since = "1.16.0")]
166 impl fmt::Debug for Repeat {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 f.debug_struct("Repeat").finish_non_exhaustive()
169 }
170 }
171
172 /// A writer which will move data into the void.
173 ///
174 /// This struct is generally created by calling [`sink`]. Please
175 /// see the documentation of [`sink()`] for more details.
176 #[stable(feature = "rust1", since = "1.0.0")]
177 #[non_exhaustive]
178 #[derive(Copy, Clone, Default)]
179 pub struct Sink;
180
181 /// Creates an instance of a writer which will successfully consume all data.
182 ///
183 /// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
184 /// and the contents of the buffer will not be inspected.
185 ///
186 /// [`write`]: Write::write
187 ///
188 /// # Examples
189 ///
190 /// ```rust
191 /// use std::io::{self, Write};
192 ///
193 /// let buffer = vec![1, 2, 3, 5, 8];
194 /// let num_bytes = io::sink().write(&buffer).unwrap();
195 /// assert_eq!(num_bytes, 5);
196 /// ```
197 #[must_use]
198 #[stable(feature = "rust1", since = "1.0.0")]
199 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
200 pub const fn sink() -> Sink {
201 Sink
202 }
203
204 #[stable(feature = "rust1", since = "1.0.0")]
205 impl Write for Sink {
206 #[inline]
207 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
208 Ok(buf.len())
209 }
210
211 #[inline]
212 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
213 let total_len = bufs.iter().map(|b| b.len()).sum();
214 Ok(total_len)
215 }
216
217 #[inline]
218 fn is_write_vectored(&self) -> bool {
219 true
220 }
221
222 #[inline]
223 fn flush(&mut self) -> io::Result<()> {
224 Ok(())
225 }
226 }
227
228 #[stable(feature = "write_mt", since = "1.48.0")]
229 impl Write for &Sink {
230 #[inline]
231 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
232 Ok(buf.len())
233 }
234
235 #[inline]
236 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
237 let total_len = bufs.iter().map(|b| b.len()).sum();
238 Ok(total_len)
239 }
240
241 #[inline]
242 fn is_write_vectored(&self) -> bool {
243 true
244 }
245
246 #[inline]
247 fn flush(&mut self) -> io::Result<()> {
248 Ok(())
249 }
250 }
251
252 #[stable(feature = "std_debug", since = "1.16.0")]
253 impl fmt::Debug for Sink {
254 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255 f.debug_struct("Sink").finish_non_exhaustive()
256 }
257 }