about summary refs log tree commit diff
path: root/library/std/src/io
AgeCommit message (Collapse)AuthorLines
2023-12-10remove redundant importssurechen-2/+0
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-11-28Rollup merge of #118222 - the8472:copy-use-vec-write, r=m-ou-seMatthias Krüger-107/+91
unify read_to_end and io::copy impls for reading into a Vec This ports over the initial probe (to avoid allocation) and the dynamic read sizing from the io::copy specialization to the `default_read_to_end` implementation which already had its own optimizations for different cases. I think it should be a best-of-both now. suggested by `@a1phyr` in https://github.com/rust-lang/rust/pull/117576#issuecomment-1803408492
2023-11-26unify read_to_end and io::copy impls for reading into a VecThe 8472-107/+91
2023-11-24Rollup merge of #116807 - seanlinsley:patch-2, r=thomccMatthias Krüger-0/+4
Improve rewind documentation The persistent use of an internal cursor for readers is expected for buffer data types that aren't read all at once, but for files it leads to the confusing situation where calling `read_to_end` on the same file handle multiple times only returns the contents of the file for the first call. This PR adds a note to the documentation clarifying that in that case, `rewind()` must first be called. I'm unsure if this is the right location for the docs update. Maybe it should also be duplicated on `File`?
2023-11-23Auto merge of #98943 - WilliamVenner:feat/bufread_skip_until, r=dtolnaybors-0/+114
Add `BufRead::skip_until` Alternative version of `BufRead::read_until` that simply discards data, rather than copying it into a buffer. Useful for situations like skipping irrelevant data in a binary file format that is NUL-terminated. <details> <summary>Benchmark</summary> ``` running 2 tests test bench_read_until ... bench: 123 ns/iter (+/- 6) test bench_skip_until ... bench: 66 ns/iter (+/- 3) ``` ```rs #![feature(test)] extern crate test; use test::Bencher; use std::io::{ErrorKind, BufRead}; fn skip_until<R: BufRead + ?Sized>(r: &mut R, delim: u8) -> Result<usize, std::io::Error> { let mut read = 0; loop { let (done, used) = { let available = match r.fill_buf() { Ok(n) => n, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), }; match memchr::memchr(delim, available) { Some(i) => (true, i + 1), None => (false, available.len()), } }; r.consume(used); read += used; if done || used == 0 { return Ok(read); } } } const STR: &[u8] = b"Ferris\0Hello, world!\0"; #[bench] fn bench_skip_until(b: &mut Bencher) { b.iter(|| { let mut io = std::io::Cursor::new(test::black_box(STR)); skip_until(&mut io, b'\0').unwrap(); let mut hello = Vec::with_capacity(b"Hello, world!\0".len()); let num_bytes = io.read_until(b'\0', &mut hello).unwrap(); assert_eq!(num_bytes, b"Hello, world!\0".len()); assert_eq!(hello, b"Hello, world!\0"); }); } #[bench] fn bench_read_until(b: &mut Bencher) { b.iter(|| { let mut io = std::io::Cursor::new(test::black_box(STR)); io.read_until(b'\0', &mut Vec::new()).unwrap(); let mut hello = Vec::with_capacity(b"Hello, world!\0".len()); let num_bytes = io.read_until(b'\0', &mut hello).unwrap(); assert_eq!(num_bytes, b"Hello, world!\0".len()); assert_eq!(hello, b"Hello, world!\0"); }); } ``` </details>
2023-11-19Rollup merge of #116750 - fintelia:seek_seek_relative, r=Mark-SimulacrumTakayuki Maeda-0/+40
Add Seek::seek_relative The `BufReader` struct has a `seek_relative` method because its `Seek::seek` implementation involved dumping the internal buffer (https://github.com/rust-lang/rust/issues/31100). Unfortunately, there isn't really a good way to take advantage of that method in generic code. This PR adds the same method to the main `Seek` trait with the straightforward default method, and an override for `BufReader` that calls its implementation. _Also discussed in [this](https://internals.rust-lang.org/t/add-seek-seek-relative/19546) internals.rust-lang.org thread._
2023-11-15Substitute version placeholdersMark Rousskov-1/+1
2023-11-09Move `BorrowedBuf` and `BorrowedCursor` from `std:io` to `core::io`John Millikin-495/+15
Assigned new feature name `core_io_borrowed_buf` to distinguish from the `Read::read_buf` functionality in `std::io`.
2023-11-08Auto merge of #115460 - zachs18:borrowedcursor_write_no_panic, r=dtolnaybors-2/+3
Don't panic in `<BorrowedCursor as io::Write>::write` Instead of panicking if the BorrowedCursor does not have enough capacity for the whole buffer, just return a short write, [like `<&mut [u8] as io::Write>::write` does](https://doc.rust-lang.org/src/std/io/impls.rs.html#349). (cc `@ChayimFriedman2` https://github.com/rust-lang/rust/issues/78485#issuecomment-1493129588) (I'm not sure if this needs an ACP? since it's not changing the "API", just what the function does)
2023-11-04Improve documentationJonathan Behrens-1/+5
2023-11-04detect EOF earlierThe 8472-5/+7
The initial probe-for-empty-source by stack_buffer_copy only detected EOF if the source was empty but not when it was merely small which lead to additional calls to read() after Ok(0) had already been returned in the stack copy routine
2023-11-04avoid excessive initialization when copying to a VecThe 8472-17/+47
It now keeps track of initialized bytes to avoid reinitialization. It also keeps track of read sizes to avoid initializing more bytes than the reader needs. This is important when passing a huge vector to a Read that only has a few bytes to offer and doesn't implement read_buf().
2023-10-29Add tracking issueJonathan Behrens-1/+1
2023-10-27Hide internal methods from documentationJacob Pratt-0/+1
2023-10-23Fix invalid stability attribute features in standard libraryDavid Tolnay-4/+4
2023-10-20Specialize `Bytes<R>::next` when `R` is a `BufReader`.Nicholas Nethercote-15/+60
This reduces the runtime for a simple program using `Bytes::next` to iterate through a file from 220ms to 70ms on my Linux box.
2023-10-16Improve rewind documentationSean Linsley-0/+4
2023-10-16Auto merge of #116775 - nnethercote:inline-Bytes-next, r=the8472bors-0/+2
Inline `Bytes::next` and `Bytes::size_hint`. This greatly increases its speed. On one small test program using `Bytes::next` to iterate over a large file, execution time dropped from ~330ms to ~220ms. r? `@the8472`
2023-10-16Inline `Bytes::next` and `Bytes::size_hint`.Nicholas Nethercote-0/+2
This greatly increases its speed.
2023-10-15Auto merge of #110604 - a1phyr:vecdeque_buf_read, r=dtolnaybors-0/+18
Implement `BufRead` for `VecDeque<u8>` Note: it would become insta-stable
2023-10-14Add Seek::seek_relativeJonathan Behrens-0/+36
2023-10-14Auto merge of #116407 - Mark-Simulacrum:bootstrap-bump, r=onur-ozkanbors-1/+1
Bump bootstrap compiler to just-released beta https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
2023-10-07Remove unnecessary tmp variable in default_read_exactWilfred Hughes-2/+1
This variable seems to serve no purpose, and it's a little confusing when reading std source code, so remove it.
2023-10-05Add more diagnostic items for clippyJason Newcomb-0/+4
2023-10-03Bump version placeholdersMark Rousskov-1/+1
2023-09-25Auto merge of #116070 - eduardosm:IoSlice-advance_slices-checked_add, ↵bors-16/+19
r=Mark-Simulacrum Avoid overflow in `IoSlice::advance_slices` Noticed in https://github.com/rust-lang/rust/issues/62726#issuecomment-1713997431.
2023-09-23Avoid overflow in `IoSlice::advance_slices`Eduardo Sánchez Muñoz-16/+19
2023-09-22Fixes from PRAyush Singh-4/+4
- Hide Docs - Use repr_unpacked error Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
2023-09-22Auto merge of #114780 - RalfJung:io-safety, r=Amanieubors-1/+47
add more explicit I/O safety documentation Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/434 Cc https://github.com/rust-lang/rust/issues/114167 Cc `@Manishearth` `@sunfishcode` `@joshtriplett`
2023-09-20PR feedbackBen Kimock-0/+1
2023-09-06Auto merge of #115453 - ibraheemdev:patch-16, r=joshtriplettbors-3/+1
Stabilize `io_error_other` feature Per the FCP for https://github.com/rust-lang/rust/issues/91946.
2023-09-04Auto merge of #115493 - Ayush1325:raw-os-error, r=workingjubileebors-4/+1
Move RawOsError defination to sys This was originally a part of https://github.com/rust-lang/rust/pull/105861, but I feel it should be its own PR since the raw os error is still unstable.
2023-09-03Use std::io::Error::is_interrupted everywhereBen Kimock-7/+8
2023-09-03Move RawOsError defination to sysAyush Singh-4/+1
Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
2023-09-01don't panic in BorrowedCursor::writeZachary S-2/+3
2023-09-01stabilize `io_error_other` featureIbraheem Ahmed-3/+1
2023-08-29further expand on Arc and Box analogyRalf Jung-3/+6
2023-08-25Add a new helper to avoid calling io::Error::kindBen Kimock-7/+17
2023-08-22Replace version placeholders with 1.73.0Mark Rousskov-2/+2
2023-08-22clarify what you cannot doRalf Jung-3/+3
2023-08-22typos and wordingRalf Jung-3/+3
Co-authored-by: Dan Gohman <dev@sunfishcode.online>
2023-08-16Partially revert #107200Josh Triplett-3/+3
`Ok(0)` is indeed something the caller may interpret as an error, but that's the *correct* thing to return if the writer can't accept any more bytes.
2023-08-14reference-counting analogyRalf Jung-20/+23
2023-08-14reword the paragraph on file description ownershipRalf Jung-9/+10
2023-08-14don't link to RFCs, they are not up-to-date docsRalf Jung-2/+1
2023-08-14wording; and explain some of the possible consequences of violating io-safetyRalf Jung-1/+7
2023-08-14typosRalf Jung-1/+1
2023-08-13add more explicit I/O safety documentationRalf Jung-1/+35
2023-08-10Rollup merge of #114194 - thomcc:flushinline, r=cuviperMichael Goulet-0/+1
Inline trivial (noop) flush calls At work I noticed that `writer.flush()?` didn't get optimized away in cases where the flush is obviously a no-op, which I had expected (well, desired). I went through and added `#[inline]` to a bunch of cases that were obviously noops, or delegated to ones that were obviously noops. I omitted platforms I don't have access to (some tier3). I didn't do this very scientifically, in cases where it was non-obvious I left `#[inline]` off.
2023-08-03Add assertion to test `skip_until` return valueWilliam Venner-2/+4
The extra `\0` in this commit is needed because the assertion on line 49 will fail otherwise (as `skip_until` stops reading on EOF and therefore does not read a trailing `\0`, returning 6 read bytes rather than the expected 7)