diff options
| author | Mara <m-ou.se@m-ou.se> | 2021-03-05 10:57:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-05 10:57:17 +0100 |
| commit | 60138110d7b00c8c788526cad525942e732db6ca (patch) | |
| tree | bf0ee3d1f4fe6994b62a6298b5cbed5f7c04caad /library/std/src/io/buffered | |
| parent | ec2619ca6219a74f3c97ac7c32aeecd79368788f (diff) | |
| parent | 7674ae1a4e10ab6bafbda57f9fc2175fdf012f5e (diff) | |
Rollup merge of #81136 - Xavientois:io_reader_size_hint, r=cramertj
Improved IO Bytes Size Hint After trying to implement better `size_hint()` return values for `File` in [this PR](https://github.com/rust-lang/rust/pull/81044) and changing to implementing it for `BufReader` in [this PR](https://github.com/rust-lang/rust/pull/81052), I have arrived at this implementation that provides tighter bounds for the `Bytes` iterator of various readers including `BufReader`, `Empty`, and `Chain`. Unfortunately, for `BufReader`, the size_hint only improves after calling `fill_buffer` due to it using the contents of the buffer for the hint. Nevertheless, the the tighter bounds should result in better pre-allocation of space to handle the contents of the `Bytes` iterator. Closes #81052
Diffstat (limited to 'library/std/src/io/buffered')
| -rw-r--r-- | library/std/src/io/buffered/bufreader.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index 987371f50ec..839c64ee5c4 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -1,6 +1,8 @@ use crate::cmp; use crate::fmt; -use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE}; +use crate::io::{ + self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE, +}; /// The `BufReader<R>` struct adds buffering to any reader. /// @@ -435,3 +437,9 @@ impl<R: Seek> Seek for BufReader<R> { }) } } + +impl<T> SizeHint for BufReader<T> { + fn lower_bound(&self) -> usize { + self.buffer().len() + } +} |
