about summary refs log tree commit diff
path: root/library/std/src/io/buffered
AgeCommit message (Collapse)AuthorLines
2021-03-27Use DebugStruct::finish_non_exhaustive() in std.Mara Bos-1/+1
2021-03-21Use io::Error::new_const everywhere to avoid allocations.Mara Bos-2/+2
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-05Rollup merge of #81136 - Xavientois:io_reader_size_hint, r=cramertjMara-1/+9
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
2021-03-03Avoid unnecessary Vec construction in BufReaderCaleb Sander-4/+3
2021-01-31specialize io::copy to use the memory of the writer if it is a BufWriterThe8472-1/+13
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
2021-01-19Auto merge of #79705 - ijackson:bufwriter-disassemble, r=m-ou-sebors-0/+99
BufWriter: Provide into_raw_parts If something goes wrong, one might want to unpeel the layers of nested Writers to perform recovery actions on the underlying writer, or reuse its resources. `into_inner` can be used for this when the inner writer is still working. But when the inner writer is broken, and returning errors, `into_inner` simply gives you the error from flush, and the same `Bufwriter` back again. Here I provide the necessary function, which I have chosen to call `into_raw_parts`. I had to do something with `panicked`. Returning it to the caller as a boolean seemed rather bare. Throwing the buffered data away in this situation also seems unfriendly: maybe the programmer knows something about the underlying writer and can recover somehow. So I went for a custom Error. This may be overkill, but it does have the nice property that a caller who actually wants to look at the buffered data, rather than simply extracting the inner writer, will be told by the type system if they forget to handle the panicked case. If a caller doesn't need the buffer, it can just be discarded. That WriterPanicked is a newtype around Vec<u8> means that hopefully the layouts of the Ok and Err variants can be very similar, with just a boolean discriminant. So this custom error type should compile down to nearly no code. *If this general idea is felt appropriate, I will open a tracking issue, etc.*
2021-01-17Add benchmark and fast path for BufReader::read_exactBen Kimock-0/+26
2021-01-04BufWriter::into_raw_parts: Add tracking issue numberIan Jackson-6/+6
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-12fixup! WriterPanicked: Use debug_structIan Jackson-1/+1
2020-12-12WriterPanicked: Use debug_structIan Jackson-1/+3
Co-authored-by: Ivan Tham <pickfire@riseup.net>
2020-12-12bufwriter::WriterPanicked: Provide panicking exampleIan Jackson-0/+24
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-09Auto merge of #78768 - mzabaluev:optimize-buf-writer, r=cramertjbors-14/+53
Use is_write_vectored to optimize the write_vectored implementation for BufWriter In case when the underlying writer does not have an efficient implementation `write_vectored`, the present implementation of `write_vectored` for `BufWriter` may still forward vectored writes directly to the writer depending on the total length of the data. This misses the advantage of buffering, as the actually written slice may be small. Provide an alternative code path for the non-vectored case, where the slices passed to `BufWriter` are coalesced in the buffer before being flushed to the underlying writer with plain `write` calls. The buffer is only bypassed if an individual slice's length is at least as large as the buffer. Remove a FIXME comment referring to #72919 as the issue has been closed with an explanation provided.
2020-12-04IntoInnerError: Provide into_errorIan Jackson-0/+21
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-04IntoInnerError: Provide into_partsIan Jackson-0/+24
In particular, IntoIneerError only currently provides .error() which returns a reference, not an owned value. This is not helpful and means that a caller of BufWriter::into_inner cannot acquire an owned io::Error which seems quite wrong. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-04BufWriter: Provide into_raw_partsIan Jackson-0/+73
If something goes wrong, one might want to unpeel the layers of nested Writers to perform recovery actions on the underlying writer, or reuse its resources. `into_inner` can be used for this when the inner writer is still working. But when the inner writer is broken, and returning errors, `into_inner` simply gives you the error from flush, and the same `Bufwriter` back again. Here I provide the necessary function, which I have chosen to call `into_raw_parts`. I had to do something with `panicked`. Returning it to the caller as a boolean seemed rather bare. Throwing the buffered data away in this situation also seems unfriendly: maybe the programmer knows something about the underlying writer and can recover somehow. So I went for a custom Error. This may be overkill, but it does have the nice property that a caller who actually wants to look at the buffered data, rather than simply extracting the inner writer, will be told by the type system if they forget to handle the panicked case. If a caller doesn't need the buffer, it can just be discarded. That WriterPanicked is a newtype around Vec<u8> means that hopefully the layouts of the Ok and Err variants can be very similar, with just a boolean discriminant. So this custom error type should compile down to nearly no code. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-11-22Reduce branching in write_vectored for BufWriterMikhail Zabaluev-14/+9
Do what write does and optimize for the most likely case: slices are much smaller than the buffer. If a slice does not fit completely in the remaining capacity of the buffer, it is left out rather than buffered partially. Special treatment is only left for oversized slices that are written directly to the underlying writer.
2020-11-22Fix is_write_vectored in LineWriterShimMikhail Zabaluev-1/+7
Now that BufWriter always claims to support vectored writes, look through it at the wrapped writer to decide whether to use vectored writes for LineWriter.
2020-11-22Make is_write_vectored return true for BufWriterMikhail Zabaluev-1/+1
BufWriter provides an efficient implementation of write_vectored also when the underlying writer does not support vectored writes.
2020-11-22Optimize write_vectored for BufWriterMikhail Zabaluev-12/+50
If the underlying writer does not support efficient vectored output, do it differently: always try to coalesce the slices in the buffer until one comes that does not fit entirely. Flush the buffer before the first slice if needed.
2020-11-05document HACKsPeter Jaszkowiak-0/+2
2020-11-05Intra-doc links for std::io::bufferedPeter Jaszkowiak-3/+3
2020-10-26fix(docs): typo in BufWriter documentationMichele Lacchia-1/+1
2020-09-10move buffered.rs to mod.rsNathan West-0/+151
2020-09-10Refactor io/buffered.rs into submodulesNathan West-0/+1312
2020-09-07Implement Seek::stream_position() for BufReaderTobias Rapp-0/+42
Optimization over BufReader::seek() for getting the current position without flushing the internal buffer. Related to #31100. Based on code in #70577.
2020-08-31std: move "mod tests/benches" to separate filesLzu Tao-0/+916
Also doing fmt inplace as requested.