1 use super::{repeat, Cursor, SeekFrom};
2 use crate::cmp::{self, min};
3 use crate::io::{self, IoSlice, IoSliceMut};
4 use crate::io::{BufRead, BufReader, BufWriter, Read, Result, Seek, Write};
8 #[cfg_attr(target_os = "emscripten", ignore)]
10 let mut buf = Cursor::new(&b"12"[..]);
11 let mut v = Vec::new();
12 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
15 let mut buf = Cursor::new(&b"1233"[..]);
16 let mut v = Vec::new();
17 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
18 assert_eq!(v, b"123");
20 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
23 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
29 let buf = Cursor::new(&b"12"[..]);
30 let mut s = buf.split(b'3');
31 assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
32 assert!(s.next().is_none());
34 let buf = Cursor::new(&b"1233"[..]);
35 let mut s = buf.split(b'3');
36 assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
37 assert_eq!(s.next().unwrap().unwrap(), vec![]);
38 assert!(s.next().is_none());
43 let mut buf = Cursor::new(&b"12"[..]);
44 let mut v = String::new();
45 assert_eq!(buf.read_line(&mut v).unwrap(), 2);
48 let mut buf = Cursor::new(&b"12\n\n"[..]);
49 let mut v = String::new();
50 assert_eq!(buf.read_line(&mut v).unwrap(), 3);
51 assert_eq!(v, "12\n");
53 assert_eq!(buf.read_line(&mut v).unwrap(), 1);
56 assert_eq!(buf.read_line(&mut v).unwrap(), 0);
62 let buf = Cursor::new(&b"12\r"[..]);
63 let mut s = buf.lines();
64 assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
65 assert!(s.next().is_none());
67 let buf = Cursor::new(&b"12\r\n\n"[..]);
68 let mut s = buf.lines();
69 assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
70 assert_eq!(s.next().unwrap().unwrap(), "".to_string());
71 assert!(s.next().is_none());
76 let mut c = Cursor::new(&b""[..]);
77 let mut v = Vec::new();
78 assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
81 let mut c = Cursor::new(&b"1"[..]);
82 let mut v = Vec::new();
83 assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
86 let cap = 1024 * 1024;
87 let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
88 let mut v = Vec::new();
89 let (a, b) = data.split_at(data.len() / 2);
90 assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
91 assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
97 let mut c = Cursor::new(&b""[..]);
98 let mut v = String::new();
99 assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
102 let mut c = Cursor::new(&b"1"[..]);
103 let mut v = String::new();
104 assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
107 let mut c = Cursor::new(&b"\xff"[..]);
108 let mut v = String::new();
109 assert!(c.read_to_string(&mut v).is_err());
114 let mut buf = [0; 4];
116 let mut c = Cursor::new(&b""[..]);
117 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
119 let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
120 c.read_exact(&mut buf).unwrap();
121 assert_eq!(&buf, b"1234");
122 c.read_exact(&mut buf).unwrap();
123 assert_eq!(&buf, b"5678");
124 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
128 fn read_exact_slice() {
129 let mut buf = [0; 4];
131 let mut c = &b""[..];
132 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
134 let mut c = &b"123"[..];
135 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
136 // make sure the optimized (early returning) method is being used
137 assert_eq!(&buf, &[0; 4]);
139 let mut c = &b"1234"[..];
140 c.read_exact(&mut buf).unwrap();
141 assert_eq!(&buf, b"1234");
143 let mut c = &b"56789"[..];
144 c.read_exact(&mut buf).unwrap();
145 assert_eq!(&buf, b"5678");
154 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
155 Err(io::Error::new(io::ErrorKind::Other, ""))
159 fn fill_buf(&mut self) -> io::Result<&[u8]> {
160 Err(io::Error::new(io::ErrorKind::Other, ""))
162 fn consume(&mut self, _amt: usize) {}
165 let mut buf = [0; 1];
166 assert_eq!(0, R.take(0).read(&mut buf).unwrap());
167 assert_eq!(b"", R.take(0).fill_buf().unwrap());
170 fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
171 let mut cat = Vec::new();
174 let buf1 = br1.fill_buf().unwrap();
175 let buf2 = br2.fill_buf().unwrap();
176 let minlen = if buf1.len() < buf2.len() { buf1.len() } else { buf2.len() };
177 assert_eq!(buf1[..minlen], buf2[..minlen]);
178 cat.extend_from_slice(&buf1[..minlen]);
184 br1.consume(consume);
185 br2.consume(consume);
187 assert_eq!(br1.fill_buf().unwrap().len(), 0);
188 assert_eq!(br2.fill_buf().unwrap().len(), 0);
189 assert_eq!(&cat[..], &exp[..])
194 let testdata = b"ABCDEFGHIJKL";
196 (&testdata[..3]).chain(&testdata[3..6]).chain(&testdata[6..9]).chain(&testdata[9..]);
197 let chain2 = (&testdata[..4]).chain(&testdata[4..8]).chain(&testdata[8..]);
198 cmp_bufread(chain1, chain2, &testdata[..]);
202 fn chain_zero_length_read_is_not_eof() {
205 let mut s = String::new();
206 let mut chain = (&a[..]).chain(&b[..]);
207 chain.read(&mut []).unwrap();
208 chain.read_to_string(&mut s).unwrap();
213 #[cfg_attr(target_os = "emscripten", ignore)]
214 fn bench_read_to_end(b: &mut test::Bencher) {
216 let mut lr = repeat(1).take(10000000);
217 let mut vec = Vec::with_capacity(1024);
218 super::read_to_end(&mut lr, &mut vec)
223 fn seek_len() -> io::Result<()> {
224 let mut c = Cursor::new(vec![0; 15]);
225 assert_eq!(c.stream_len()?, 15);
227 c.seek(SeekFrom::End(0))?;
228 let old_pos = c.stream_position()?;
229 assert_eq!(c.stream_len()?, 15);
230 assert_eq!(c.stream_position()?, old_pos);
232 c.seek(SeekFrom::Start(7))?;
233 c.seek(SeekFrom::Current(2))?;
234 let old_pos = c.stream_position()?;
235 assert_eq!(c.stream_len()?, 15);
236 assert_eq!(c.stream_position()?, old_pos);
242 fn seek_position() -> io::Result<()> {
243 // All `asserts` are duplicated here to make sure the method does not
244 // change anything about the seek state.
245 let mut c = Cursor::new(vec![0; 15]);
246 assert_eq!(c.stream_position()?, 0);
247 assert_eq!(c.stream_position()?, 0);
249 c.seek(SeekFrom::End(0))?;
250 assert_eq!(c.stream_position()?, 15);
251 assert_eq!(c.stream_position()?, 15);
253 c.seek(SeekFrom::Start(7))?;
254 c.seek(SeekFrom::Current(2))?;
255 assert_eq!(c.stream_position()?, 9);
256 assert_eq!(c.stream_position()?, 9);
258 c.seek(SeekFrom::End(-3))?;
259 c.seek(SeekFrom::Current(1))?;
260 c.seek(SeekFrom::Current(-5))?;
261 assert_eq!(c.stream_position()?, 8);
262 assert_eq!(c.stream_position()?, 8);
267 // A simple example reader which uses the default implementation of
269 struct ExampleSliceReader<'a> {
273 impl<'a> Read for ExampleSliceReader<'a> {
274 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
275 let len = cmp::min(self.slice.len(), buf.len());
276 buf[..len].copy_from_slice(&self.slice[..len]);
277 self.slice = &self.slice[len..];
283 fn test_read_to_end_capacity() -> io::Result<()> {
284 let input = &b"foo"[..];
286 // read_to_end() generally needs to over-allocate, both for efficiency
287 // and so that it can distinguish EOF. Assert that this is the case
288 // with this simple ExampleSliceReader struct, which uses the default
289 // implementation of read_to_end. Even though vec1 is allocated with
290 // exactly enough capacity for the read, read_to_end will allocate more
292 let mut vec1 = Vec::with_capacity(input.len());
293 ExampleSliceReader { slice: input }.read_to_end(&mut vec1)?;
294 assert_eq!(vec1.len(), input.len());
295 assert!(vec1.capacity() > input.len(), "allocated more");
297 // However, std::io::Take includes an implementation of read_to_end
298 // that will not allocate when the limit has already been reached. In
299 // this case, vec2 never grows.
300 let mut vec2 = Vec::with_capacity(input.len());
301 ExampleSliceReader { slice: input }.take(input.len() as u64).read_to_end(&mut vec2)?;
302 assert_eq!(vec2.len(), input.len());
303 assert_eq!(vec2.capacity(), input.len(), "did not allocate more");
309 fn io_slice_mut_advance() {
310 let mut buf1 = [1; 8];
311 let mut buf2 = [2; 16];
312 let mut buf3 = [3; 8];
313 let mut bufs = &mut [
314 IoSliceMut::new(&mut buf1),
315 IoSliceMut::new(&mut buf2),
316 IoSliceMut::new(&mut buf3),
319 // Only in a single buffer..
320 bufs = IoSliceMut::advance(bufs, 1);
321 assert_eq!(bufs[0].deref(), [1; 7].as_ref());
322 assert_eq!(bufs[1].deref(), [2; 16].as_ref());
323 assert_eq!(bufs[2].deref(), [3; 8].as_ref());
325 // Removing a buffer, leaving others as is.
326 bufs = IoSliceMut::advance(bufs, 7);
327 assert_eq!(bufs[0].deref(), [2; 16].as_ref());
328 assert_eq!(bufs[1].deref(), [3; 8].as_ref());
330 // Removing a buffer and removing from the next buffer.
331 bufs = IoSliceMut::advance(bufs, 18);
332 assert_eq!(bufs[0].deref(), [3; 6].as_ref());
336 fn io_slice_mut_advance_empty_slice() {
337 let empty_bufs = &mut [][..];
339 IoSliceMut::advance(empty_bufs, 1);
343 fn io_slice_mut_advance_beyond_total_length() {
344 let mut buf1 = [1; 8];
345 let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
347 // Going beyond the total length should be ok.
348 bufs = IoSliceMut::advance(bufs, 9);
349 assert!(bufs.is_empty());
353 fn io_slice_advance() {
357 let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];
359 // Only in a single buffer..
360 bufs = IoSlice::advance(bufs, 1);
361 assert_eq!(bufs[0].deref(), [1; 7].as_ref());
362 assert_eq!(bufs[1].deref(), [2; 16].as_ref());
363 assert_eq!(bufs[2].deref(), [3; 8].as_ref());
365 // Removing a buffer, leaving others as is.
366 bufs = IoSlice::advance(bufs, 7);
367 assert_eq!(bufs[0].deref(), [2; 16].as_ref());
368 assert_eq!(bufs[1].deref(), [3; 8].as_ref());
370 // Removing a buffer and removing from the next buffer.
371 bufs = IoSlice::advance(bufs, 18);
372 assert_eq!(bufs[0].deref(), [3; 6].as_ref());
376 fn io_slice_advance_empty_slice() {
377 let empty_bufs = &mut [][..];
379 IoSlice::advance(empty_bufs, 1);
383 fn io_slice_advance_beyond_total_length() {
385 let mut bufs = &mut [IoSlice::new(&buf1)][..];
387 // Going beyond the total length should be ok.
388 bufs = IoSlice::advance(bufs, 9);
389 assert!(bufs.is_empty());
392 /// Create a new writer that reads from at most `n_bufs` and reads
393 /// `per_call` bytes (in total) per call to write.
394 fn test_writer(n_bufs: usize, per_call: usize) -> TestWriter {
395 TestWriter { n_bufs, per_call, written: Vec::new() }
404 impl Write for TestWriter {
405 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
406 self.write_vectored(&[IoSlice::new(buf)])
409 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
410 let mut left = self.per_call;
412 for buf in bufs.iter().take(self.n_bufs) {
413 let n = min(left, buf.len());
414 self.written.extend_from_slice(&buf[0..n]);
421 fn flush(&mut self) -> io::Result<()> {
427 fn test_writer_read_from_one_buf() {
428 let mut writer = test_writer(1, 2);
430 assert_eq!(writer.write(&[]).unwrap(), 0);
431 assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
433 // Read at most 2 bytes.
434 assert_eq!(writer.write(&[1, 1, 1]).unwrap(), 2);
435 let bufs = &[IoSlice::new(&[2, 2, 2])];
436 assert_eq!(writer.write_vectored(bufs).unwrap(), 2);
438 // Only read from first buf.
439 let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4, 4])];
440 assert_eq!(writer.write_vectored(bufs).unwrap(), 1);
442 assert_eq!(writer.written, &[1, 1, 2, 2, 3]);
446 fn test_writer_read_from_multiple_bufs() {
447 let mut writer = test_writer(3, 3);
449 // Read at most 3 bytes from two buffers.
450 let bufs = &[IoSlice::new(&[1]), IoSlice::new(&[2, 2, 2])];
451 assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
453 // Read at most 3 bytes from three buffers.
454 let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4]), IoSlice::new(&[5, 5])];
455 assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
457 assert_eq!(writer.written, &[1, 2, 2, 3, 4, 5]);
461 fn test_write_all_vectored() {
462 #[rustfmt::skip] // Becomes unreadable otherwise.
463 let tests: Vec<(_, &'static [u8])> = vec![
465 (vec![IoSlice::new(&[]), IoSlice::new(&[])], &[]),
466 (vec![IoSlice::new(&[1])], &[1]),
467 (vec![IoSlice::new(&[1, 2])], &[1, 2]),
468 (vec![IoSlice::new(&[1, 2, 3])], &[1, 2, 3]),
469 (vec![IoSlice::new(&[1, 2, 3, 4])], &[1, 2, 3, 4]),
470 (vec![IoSlice::new(&[1, 2, 3, 4, 5])], &[1, 2, 3, 4, 5]),
471 (vec![IoSlice::new(&[1]), IoSlice::new(&[2])], &[1, 2]),
472 (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2])], &[1, 2, 2]),
473 (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2])], &[1, 1, 2, 2]),
474 (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
475 (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
476 (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 1, 2, 2, 2]),
477 (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 2, 2, 2, 2]),
478 (vec![IoSlice::new(&[1, 1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 1, 2, 2, 2, 2]),
479 (vec![IoSlice::new(&[1]), IoSlice::new(&[2]), IoSlice::new(&[3])], &[1, 2, 3]),
480 (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3])], &[1, 1, 2, 2, 3, 3]),
481 (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 2, 2, 3, 3, 3]),
482 (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 1, 1, 2, 2, 2, 3, 3, 3]),
485 let writer_configs = &[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)];
487 for (n_bufs, per_call) in writer_configs.iter().copied() {
488 for (mut input, wanted) in tests.clone().into_iter() {
489 let mut writer = test_writer(n_bufs, per_call);
490 assert!(writer.write_all_vectored(&mut *input).is_ok());
491 assert_eq!(&*writer.written, &*wanted);
498 fn copy_specialization() -> Result<()> {
499 let path = crate::env::temp_dir();
500 let source_path = path.join("copy-spec.source");
501 let sink_path = path.join("copy-spec.sink");
503 let result: Result<()> = try {
504 let mut source = crate::fs::OpenOptions::new()
509 .open(&source_path)?;
510 source.write_all(b"abcdefghiklmnopqr")?;
511 source.seek(SeekFrom::Start(8))?;
512 let mut source = BufReader::with_capacity(8, source.take(5));
514 assert_eq!(source.buffer(), b"iklmn");
515 source.get_mut().set_limit(6);
516 source.get_mut().get_mut().seek(SeekFrom::Start(1))?; // "bcdefg"
517 let mut source = source.take(10); // "iklmnbcdef"
519 let mut sink = crate::fs::OpenOptions::new()
525 sink.write_all(b"000000")?;
526 let mut sink = BufWriter::with_capacity(5, sink);
527 sink.write_all(b"wxyz")?;
528 assert_eq!(sink.buffer(), b"wxyz");
530 let copied = crate::io::copy(&mut source, &mut sink)?;
531 assert_eq!(copied, 10);
532 assert_eq!(sink.buffer().len(), 0);
534 let mut sink = sink.into_inner()?;
535 sink.seek(SeekFrom::Start(0))?;
536 let mut copied = Vec::new();
537 sink.read_to_end(&mut copied)?;
538 assert_eq!(&copied, b"000000wxyziklmnbcdef");
541 let rm1 = crate::fs::remove_file(source_path);
542 let rm2 = crate::fs::remove_file(sink_path);
544 result.and(rm1).and(rm2)