rust/library/std/src/io
Matthias Krüger 458095aa17
Rollup merge of #137107 - thaliaarchi:io-optional-methods/cursors, r=joboet
Override default `Write` methods for cursor-like types

Override the default `io::Write` methods for cursor-like types to provide more efficient versions.

Writes to resizable containers already write everything, so implement `write_all` and `write_all_vectored` in terms of those. For fixed-sized containers, cut out unnecessary error checking and looping for those same methods.

| `impl Write for T`              | `vectored` | `all` | `all_vectored` | `fmt`   |
| ------------------------------- | ---------- | ----- | -------------- | ------- |
| `&mut [u8]`                     | Y          | Y     | new            |         |
| `Vec<u8>`                       | Y          | Y     | new            | #137762 |
| `VecDeque<u8>`                  | Y          | Y     | new            | #137762 |
| `std::io::Cursor<&mut [u8]>`    | Y          | new   | new            |         |
| `std::io::Cursor<&mut Vec<u8>>` | Y          | new   | new            | #137762 |
| `std::io::Cursor<Vec<u8>>`      | Y          | new   | new            | #137762 |
| `std::io::Cursor<Box<[u8]>>`    | Y          | new   | new            |         |
| `std::io::Cursor<[u8; N]>`      | Y          | new   | new            |         |
| `core::io::BorrowedCursor<'_>`  | new        | new   | new            |         |

Tracked in https://github.com/rust-lang/rust/issues/136756.

# Open questions

Is it guaranteed by `Write::write_all` that the maximal write is performed when not everything can be written? Its documentation describes the behavior of the default implementation, which writes until a 0-length write is encountered, thus implying that a maximal write is expected. In contrast, `Read::read_exact` declares that the contents of the buffer are unspecified for short reads. If it were allowed, these cursor-like types could bail on the write altogether if it has insufficient capacity.
2025-03-07 10:02:20 +01:00
..
buffered Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
copy Fix testing of the standard library with Emscripten 2025-01-24 09:25:34 +00:00
cursor attempt to optimise vectored write 2022-06-26 17:15:31 +01:00
error Use NonNull::without_provenance within the standard library 2025-01-10 23:23:10 +01:00
impls std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
pipe Test pipes also when not running on Windows and Linux simultaneously 2025-01-26 12:48:33 +01:00
stdio Rustfmt 2025-02-08 22:12:13 +00:00
util Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
copy.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
cursor.rs Override default Write methods for cursor-like types 2025-02-27 17:09:09 -08:00
error.rs Use correct error message casing for io::const_errors 2025-02-28 17:50:38 +01:00
impls.rs Override default Write methods for cursor-like types 2025-02-27 17:09:09 -08:00
mod.rs Clarify/update comments in BufRead::read_line's default body 2025-02-21 03:56:49 +01:00
pipe.rs Update std::io::{pipe, PipeReader, PipeWriter} docs the new location 2025-01-26 12:42:52 +01:00
prelude.rs Use heading for std::prelude and not io::prelude 2021-01-05 17:52:24 -08:00
stdio.rs Rollup merge of #136798 - pcorwin:master, r=tgross35 2025-03-05 21:46:34 +08:00
tests.rs Move std::io::pipe code into its own file 2025-01-26 12:40:36 +01:00
util.rs Use slice::fill in io::Repeat implementation 2025-02-13 12:23:52 +01:00