ndless/file_io/io/
util.rs

1#![allow(missing_copy_implementations)]
2
3use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write};
4use core::fmt;
5use core::mem::MaybeUninit;
6
7/// Copies the entire contents of a reader into a writer.
8///
9/// This function will continuously read data from `reader` and then
10/// write it into `writer` in a streaming fashion until `reader`
11/// returns EOF.
12///
13/// On success, the total number of bytes that were copied from
14/// `reader` to `writer` is returned.
15///
16/// If you’re wanting to copy the contents of one file to another and you’re
17/// working with filesystem paths, see the [`fs::copy`] function.
18///
19/// [`fs::copy`]: ../fs/fn.copy.html
20///
21/// # Errors
22///
23/// This function will return an error immediately if any call to `read` or
24/// `write` returns an error. All instances of `ErrorKind::Interrupted` are
25/// handled by this function and the underlying operation is retried.
26///
27/// # Examples
28///
29/// ```
30/// use std::io;
31///
32/// fn main() -> io::Result<()> {
33///     let mut reader: &[u8] = b"hello";
34///     let mut writer: Vec<u8> = vec![];
35///
36///     io::copy(&mut reader, &mut writer)?;
37///
38///     assert_eq!(&b"hello"[..], &writer[..]);
39///     Ok(())
40/// }
41/// ```
42pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
43where
44	R: Read,
45	W: Write,
46{
47	let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
48	// FIXME: #42788
49	//
50	//   - This creates a (mut) reference to a slice of
51	//     _uninitialized_ integers, which is **undefined behavior**
52	//
53	//   - Only the standard library gets to soundly "ignore" this,
54	//     based on its privileged knowledge of unstable rustc
55	//     internals;
56	unsafe {
57		reader.initializer().initialize(buf.assume_init_mut());
58	}
59
60	let mut written = 0;
61	loop {
62		let len = match reader.read(unsafe { buf.assume_init_mut() }) {
63			Ok(0) => return Ok(written),
64			Ok(len) => len,
65			Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
66			Err(e) => return Err(e),
67		};
68		writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
69		written += len as u64;
70	}
71}
72
73/// A reader which is always at EOF.
74///
75/// This struct is generally created by calling [`empty`]. Please see
76/// the documentation of [`empty()`][`empty`] for more details.
77///
78/// [`empty`]: fn.empty.html
79pub struct Empty {
80	_priv: (),
81}
82
83/// Constructs a new handle to an empty reader.
84///
85/// All reads from the returned reader will return [`Ok`]`(0)`.
86///
87/// [`Ok`]: ../result/enum.Result.html#variant.Ok
88///
89/// # Examples
90///
91/// A slightly sad example of not reading anything into a buffer:
92///
93/// ```
94/// use std::io::{self, Read};
95///
96/// let mut buffer = String::new();
97/// io::empty().read_to_string(&mut buffer).unwrap();
98/// assert!(buffer.is_empty());
99/// ```
100pub fn empty() -> Empty {
101	Empty { _priv: () }
102}
103
104impl Read for Empty {
105	#[inline]
106	fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
107		Ok(0)
108	}
109
110	#[inline]
111	unsafe fn initializer(&self) -> Initializer {
112		Initializer::nop()
113	}
114}
115impl BufRead for Empty {
116	#[inline]
117	fn fill_buf(&mut self) -> io::Result<&[u8]> {
118		Ok(&[])
119	}
120	#[inline]
121	fn consume(&mut self, _n: usize) {}
122}
123
124impl fmt::Debug for Empty {
125	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126		f.pad("Empty { .. }")
127	}
128}
129
130/// A reader which yields one byte over and over and over and over and over and...
131///
132/// This struct is generally created by calling [`repeat`][repeat]. Please
133/// see the documentation of `repeat()` for more details.
134///
135/// [repeat]: fn.repeat.html
136pub struct Repeat {
137	byte: u8,
138}
139
140/// Creates an instance of a reader that infinitely repeats one byte.
141///
142/// All reads from this reader will succeed by filling the specified buffer with
143/// the given byte.
144///
145/// # Examples
146///
147/// ```
148/// use std::io::{self, Read};
149///
150/// let mut buffer = [0; 3];
151/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
152/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
153/// ```
154pub fn repeat(byte: u8) -> Repeat {
155	Repeat { byte }
156}
157
158impl Read for Repeat {
159	#[inline]
160	fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
161		for slot in &mut *buf {
162			*slot = self.byte;
163		}
164		Ok(buf.len())
165	}
166
167	#[inline]
168	fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
169		let mut nwritten = 0;
170		for buf in bufs {
171			nwritten += self.read(buf)?;
172		}
173		Ok(nwritten)
174	}
175
176	#[inline]
177	unsafe fn initializer(&self) -> Initializer {
178		Initializer::nop()
179	}
180}
181
182impl fmt::Debug for Repeat {
183	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184		f.pad("Repeat { .. }")
185	}
186}
187
188/// A writer which will move data into the void.
189///
190/// This struct is generally created by calling [`sink`][sink]. Please
191/// see the documentation of `sink()` for more details.
192///
193/// [sink]: fn.sink.html
194pub struct Sink {
195	_priv: (),
196}
197
198/// Creates an instance of a writer which will successfully consume all data.
199///
200/// All calls to `write` on the returned instance will return `Ok(buf.len())`
201/// and the contents of the buffer will not be inspected.
202///
203/// # Examples
204///
205/// ```rust
206/// use std::io::{self, Write};
207///
208/// let buffer = vec![1, 2, 3, 5, 8];
209/// let num_bytes = io::sink().write(&buffer).unwrap();
210/// assert_eq!(num_bytes, 5);
211/// ```
212pub fn sink() -> Sink {
213	Sink { _priv: () }
214}
215
216impl Write for Sink {
217	#[inline]
218	fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
219		Ok(buf.len())
220	}
221
222	#[inline]
223	fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
224		let total_len = bufs.iter().map(|b| b.len()).sum();
225		Ok(total_len)
226	}
227
228	#[inline]
229	fn flush(&mut self) -> io::Result<()> {
230		Ok(())
231	}
232}
233
234impl fmt::Debug for Sink {
235	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236		f.pad("Sink { .. }")
237	}
238}
239
240#[cfg(test)]
241mod tests {
242	use crate::io::prelude::*;
243	use crate::io::{copy, empty, repeat, sink};
244
245	#[test]
246	fn copy_copies() {
247		let mut r = repeat(0).take(4);
248		let mut w = sink();
249		assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
250
251		let mut r = repeat(0).take(1 << 17);
252		assert_eq!(
253			copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(),
254			1 << 17
255		);
256	}
257
258	#[test]
259	fn sink_sinks() {
260		let mut s = sink();
261		assert_eq!(s.write(&[]).unwrap(), 0);
262		assert_eq!(s.write(&[0]).unwrap(), 1);
263		assert_eq!(s.write(&[0; 1024]).unwrap(), 1024);
264		assert_eq!(s.by_ref().write(&[0; 1024]).unwrap(), 1024);
265	}
266
267	#[test]
268	fn empty_reads() {
269		let mut e = empty();
270		assert_eq!(e.read(&mut []).unwrap(), 0);
271		assert_eq!(e.read(&mut [0]).unwrap(), 0);
272		assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
273		assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
274	}
275
276	#[test]
277	fn repeat_repeats() {
278		let mut r = repeat(4);
279		let mut b = [0; 1024];
280		assert_eq!(r.read(&mut b).unwrap(), 1024);
281		assert!(b.iter().all(|b| *b == 4));
282	}
283
284	#[test]
285	fn take_some_bytes() {
286		assert_eq!(repeat(4).take(100).bytes().count(), 100);
287		assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
288		assert_eq!(
289			repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(),
290			20
291		);
292	}
293}