rust/src/libstd/io
Jack O'Connor edb5214b29 avoid unnecessary reservations in std::io::Take::read_to_end
Prevously the `read_to_end` implementation for `std::io::Take` used its
own `limit` as a cap on the `reservation_size`. However, that could
still result in an over-allocation like this:

1. Call `reader.take(5).read_to_end(&mut vec)`.
2. `read_to_end_with_reservation` reserves 5 bytes and calls `read`.
3. `read` writes 5 bytes.
4. `read_to_end_with_reservation` reserves 5 bytes and calls `read`.
5. `read` writes 0 bytes.
6. The read loop ends with `vec` having length 5 and capacity 10.

The reservation of 5 bytes was correct for the read at step 2 but
unnecessary for the read at step 4. By that second read, `Take::limit`
is 0, but the `read_to_end_with_reservation` loop is still using the
same `reservation_size` it started with.

Solve this by having `read_to_end_with_reservation` take a closure,
which lets it get a fresh `reservation_size` for each read. This is an
implementation detail which doesn't affect any public API.
2019-08-06 10:15:11 -04:00
..
buffered.rs Rollup merge of #61235 - lzutao:stabilize-bufreader_buffer, r=Centril 2019-05-29 08:15:58 +02:00
cursor.rs Rollup merge of #60234 - tesaguri:cursor-default, r=Amanieu 2019-05-09 23:56:11 +02:00
error.rs libstd: deny(elided_lifetimes_in_paths) 2019-03-31 12:56:51 +02:00
impls.rs tidy 2019-04-27 09:19:34 -07:00
lazy.rs libstd => 2018 2019-02-28 04:06:15 +09:00
mod.rs avoid unnecessary reservations in std::io::Take::read_to_end 2019-08-06 10:15:11 -04:00
prelude.rs Remove licenses 2018-12-25 21:08:33 -07:00
stdio.rs Stabilized vectored IO 2019-04-27 08:34:08 -07:00
util.rs ONCE_INIT is deprecated-in-future only for bootstrap 2019-07-19 09:48:06 +02:00