about summary refs log tree commit diff
path: root/library/std/src/io/buffered/bufreader.rs
AgeCommit message (Collapse)AuthorLines
2025-03-16Rollup merge of #137890 - lolbinarycat:docs-bufreader-peek-consume, ↵许杰友 Jieyou Xu (Joe)-1/+5
r=Mark-Simulacrum doc: clarify that consume can be called after BufReader::peek tracking issue #128405
2025-03-02doc: clarify that consume can be called after BufReader::peekbinarycat-1/+5
2025-02-28Tweak BufReader::peek() doctest to expose bug in Buffer::read_more()Will Woods-3/+3
This patch makes BufReader::peek()'s doctest call read_more() to refill the buffer before the inner reader hits EOF. This exposes a bug in read_more() that causes an out-of-bounds slice access and segfault.
2024-09-28Rollup merge of #125404 - a1phyr:fix-read_buf-uses, r=workingjubileeMatthias Krüger-1/+1
Fix `read_buf` uses in `std` Following lib-team decision here: https://github.com/rust-lang/rust/issues/78485#issuecomment-2122992314 Guard against the pathological behavior of both returning an error and performing a read.
2024-09-24Pre-allocate buffers in `File::open_buffered` and `create_buffered`Josh Stone-0/+8
2024-09-23Fix `io::BufReader` uses of `read_buf`Benoît du Garreau-1/+1
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-15Rollup merge of #130042 - lolbinarycat:bufreaker_peek_eof, r=AmanieuStuart Cook-2/+10
properly handle EOF in BufReader::peek previously this would cause an infinite loop due to it being unable to read `n` bytes.
2024-09-09Add missing `#[allow(missing_docs)]` on hack functions in allocUrgau-0/+1
2024-09-06properly handle EOF in BufReader::peekbinarycat-2/+10
previously this would cause an infinite loop due to it being unable to read `n` bytes.
2024-08-28allow BufReader::peek to be called on unsized typesbinarycat-0/+2
2024-08-05implement BufReader::peekbinarycat-0/+34
2024-07-29Reformat `use` declarations.Nicholas Nethercote-1/+2
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-04-11Factor some common `io::Error` constantsBenoît du Garreau-6/+1
2024-04-10Auto merge of #122393 - a1phyr:specialize_read_buf_exact, r=joboetbors-0/+8
Specialize many implementations of `Read::read_buf_exact` This makes all implementations of `Read` that have a specialized `read_exact` implementation also have one for `read_buf_exact`.
2024-03-20resolve clippy errorsonur-ozkan-4/+3
Signed-off-by: onur-ozkan <work@onurozkan.dev>
2024-03-12Specialize many implementations of `Read::read_buf_exact`Benoît du Garreau-0/+8
2024-02-21Remove unnecessary map_errKornel-1/+1
2024-02-19Remove an old hack for rustdocPavel Grigorenko-2/+1
2024-01-29Handle out of memory errors in io:Read::read_to_end()Kornel-0/+1
2023-11-19Rollup merge of #116750 - fintelia:seek_seek_relative, r=Mark-SimulacrumTakayuki Maeda-0/+10
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-10-20Specialize `Bytes<R>::next` when `R` is a `BufReader`.Nicholas Nethercote-5/+20
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-14Add Seek::seek_relativeJonathan Behrens-0/+10
2023-07-06Rollup merge of #112008 - intruder-kat:master, r=NilstriebMichael Goulet-1/+1
Fix incorrect documented default bufsize in bufreader/writer
2023-06-17Extend io::copy buffer reuse to BufReader tooThe 8472-1/+1
previously it was only able to use BufWriter. This was due to a limitation in the BufReader generics that prevented specialization. This change works around the issue by using `where Self: Read` instead of `where I: Read`. This limits our options, e.g. we can't access BufRead methods, but it happens to work out if we rely on some implementation details.
2023-05-26Fix incorrect documented default bufsize in bufreader/writerKathryn R-1/+1
2023-05-01Relax implicit `R: Sized` bound on `BufReader<R>`Maybe Waffle-12/+15
2022-10-06Avoid defensive re-initialization of the BufReader bufferBen Kimock-0/+8
2022-08-18Address reviewer commentsNick Cameron-2/+2
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-05non-linux platformsNick Cameron-3/+2
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-04std::io: migrate ReadBuf to BorrowBuf/BorrowCursorNick Cameron-7/+8
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-07-26Add Buffer::consume_with to enable direct buffer access with one checkBen Kimock-3/+1
2022-07-24Rename and document the new BufReader internalsBen Kimock-7/+7
2022-07-24Remove some redundant checks from BufReaderBen Kimock-53/+30
The implementation of BufReader contains a lot of redundant checks. While any one of these checks is not particularly expensive to execute, especially when taken together they dramatically inhibit LLVM's ability to make subsequent optimizations.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-2/+2
2021-11-02implement review suggestionsDrMeepster-1/+1
2021-11-02consolidate 2 unsafe blocks into 1DrMeepster-2/+2
2021-11-02read_bufDrMeepster-16/+43
2021-10-09Apply clippy suggestionsClemens Wasser-6/+5
2021-10-07Optimize File::read_to_end and read_to_stringJohn Kugelman-0/+45
Reading a file into an empty vector or string buffer can incur unnecessary `read` syscalls and memory re-allocations as the buffer "warms up" and grows to its final size. This is perhaps a necessary evil with generic readers, but files can be read in smarter by checking the file size and reserving that much capacity. `std::fs::read` and `read_to_string` already perform this optimization: they open the file, reads its metadata, and call `with_capacity` with the file size. This ensures that the buffer does not need to be resized and an initial string of small `read` syscalls. However, if a user opens the `File` themselves and calls `file.read_to_end` or `file.read_to_string` they do not get this optimization. ```rust let mut buf = Vec::new(); file.read_to_end(&mut buf)?; ``` I searched through this project's codebase and even here are a *lot* of examples of this. They're found all over in unit tests, which isn't a big deal, but there are also several real instances in the compiler and in Cargo. I've documented the ones I found in a comment here: https://github.com/rust-lang/rust/issues/89516#issuecomment-934423999 Most telling, the `Read` trait and the `read_to_end` method both show this exact pattern as examples of how to use readers. What this says to me is that this shouldn't be solved by simply fixing the instances of it in this codebase. If it's here it's certain to be prevalent in the wider Rust ecosystem. To that end, this commit adds specializations of `read_to_end` and `read_to_string` directly on `File`. This way it's no longer a minor footgun to start with an empty buffer when reading a file in. A nice side effect of this change is that code that accesses a `File` as a bare `Read` constraint or via a `dyn Read` trait object will benefit. For example, this code from `compiler/rustc_serialize/src/json.rs`: ```rust pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> { let mut contents = Vec::new(); match rdr.read_to_end(&mut contents) { ``` Related changes: - I also added specializations to `BufReader` to delegate to `self.inner`'s methods. That way it can call `File`'s optimized implementations if the inner reader is a file. - The private `std::io::append_to_string` function is now marked `unsafe`. - `File::read_to_string` being more efficient means that the performance note for `io::read_to_string` can be softened. I've added @camelid's suggested wording from: https://github.com/rust-lang/rust/issues/80218#issuecomment-936806502
2021-09-25Apply 16 commits (squashed)Frank Steffahn-4/+4
---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::fmt ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::{rc, sync} ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::string ---------- Fix spacing for links inside code blocks in alloc::vec ---------- Fix spacing for links inside code blocks in core::option ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in core::result ---------- Fix spacing for links inside code blocks in core::{iter::{self, iterator}, stream::stream, poll} ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{fs, path} ---------- Fix spacing for links inside code blocks in std::{collections, time} ---------- Fix spacing for links inside code blocks in and make formatting of `&str`-like types consistent in std::ffi::{c_str, os_str} ---------- Fix spacing for links inside code blocks, and improve link tooltips in std::ffi ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{io::{self, buffered::{bufreader, bufwriter}, cursor, util}, net::{self, addr}} ---------- Fix typo in link to `into` for `OsString` docs ---------- Remove tooltips that will probably become redundant in the future ---------- Apply suggestions from code review Replacing `…std/primitive.reference.html` paths with just `reference` Co-authored-by: Joshua Nelson <github@jyn.dev> ---------- Also replace `…std/primitive.reference.html` paths with just `reference` in `core::pin`
2021-06-10Specialize `io::Bytes::size_hint` for more typesBenoît du Garreau-1/+7
2021-03-21Bump stable version of bufreader_seek_relative.Mara Bos-1/+1
2021-03-10Stabilize `bufreader_seek_relative`philippeitis-1/+1
2021-03-05Rollup merge of #82728 - calebsander:refactor/bufreader-buf, r=m-ou-seMara-4/+3
Avoid unnecessary Vec construction in BufReader As mentioned in #80460, creating a `Vec` and calling `Vec::into_boxed_slice()` emits unnecessary calls to `realloc()` and `free()`. Updated the code to use `Box::new_uninit_slice()` to create a boxed slice directly. I think this also makes it more explicit that the initial contents of the buffer are uninitialized. r? ``@m-ou-se``
2021-03-03Avoid unnecessary Vec construction in BufReaderCaleb Sander-4/+3
2021-01-31Remove trailing newlineXavientois-2/+3
2021-01-31Implement SizeHint trait for BufReader, Emtpy, and ChainXavientois-1/+8
2021-01-24Stabilize `Seek::stream_position` & change feature of `Seek::stream_len`Lukas Kalbertodt-1/+0