about summary refs log tree commit diff
path: root/library/std/src/io/util.rs
AgeCommit message (Collapse)AuthorLines
2025-03-19Rollup merge of #137051 - thaliaarchi:io-optional-impls/empty, r=m-ou-seMatthias Krüger-0/+116
Implement default methods for `io::Empty` and `io::Sink` Implements default methods of `io::Read`, `io::BufRead`, and `io::Write` for `io::Empty` and `io::Sink`. These implementations are equivalent to the defaults, except in doing less unnecessary work. `Read::read_to_string` and `BufRead::read_line` both have a redundant call to `str::from_utf8` which can't be inlined from `core` and `Write::write_all_vectored` has slicing logic which can't be simplified (See on [Compiler Explorer](https://rust.godbolt.org/z/KK6xcrWr4)). The rest are optimized to the minimal with `-C opt-level=3`, but this PR gives that benefit to unoptimized builds. This includes an implementation of `Write::write_fmt` which just ignores the `fmt::Arguments<'_>`. This could be problematic whenever a user formatting impl is impure, but the docs do not guarantee that the args will be expanded. Tracked in https://github.com/rust-lang/rust/issues/136756. r? `@m-ou-se`
2025-03-10Implement default methods for io::Empty and io::SinkThalia Archibald-0/+116
Eliminate any redundant, unobservable logic from the their default method implementations. The observable changes are that `Write::write_fmt` for both types now ignores the formatting arguments, so a user fmt impl which has side effects is not invoked, and `Write::write_all_vectored` for both types does not advance the borrowed buffers. Neither behavior is guaranteed by the docs and the latter is documented as unspecified. `Empty` is not marked as vectored, so that `Chain<Empty, _>` and `Chain<_, Empty>` are not forced to be vectored.
2025-03-08Add inherent versions of MaybeUninit::fill methods for slicesltdk-2/+1
2025-02-13Use `slice::fill` in `io::Repeat` implementationDaniPopes-18/+10
Use the existing `fill` methods on slices instead of manually writing the fill loop.
2025-02-10Implement `read*_exact` for `std:io::repeat`Benoît du Garreau-0/+11
cc #136756
2024-05-01Replace version placeholders for 1.79Mark Rousskov-3/+3
2024-04-17Stabilize `const_io_structs`Slanterns-3/+3
2024-02-07Make `io::BorrowedCursor::advance` safeBenoît du Garreau-1/+1
This also keeps the old `advance` method under `advance_unchecked` name. This makes pattern like `std::io::default_read_buf` safe to write.
2024-01-27Reject infinitely-sized reads from io::RepeatKornel-0/+10
Related to #117925
2023-08-22Replace version placeholders with 1.73.0Mark Rousskov-2/+2
2023-07-30Fix empty_write since rust version attributeAlex Saveau-2/+2
2023-05-05feat: merge functionality of `io::Sink` into `io::Empty`Vidhan Bhatt-24/+76
2022-08-18Address reviewer commentsNick Cameron-2/+2
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-05non-linux platformsNick Cameron-3/+3
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-04std::io: migrate ReadBuf to BorrowBuf/BorrowCursorNick Cameron-8/+6
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2021-11-02add safety commentsDrMeepster-0/+2
2021-11-02read_bufDrMeepster-8/+19
2021-10-30Add #[must_use] to remaining std functions (A-N)John Kugelman-0/+3
2021-09-25Apply 16 commits (squashed)Frank Steffahn-1/+1
---------- 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-30impl Default, Copy, Clone for std::io::Sink and EmptyIan Jackson-0/+2
The omission of Sink: Default is causing me a slight inconvenience in a test harness. There seems little reason for this and Empty not to be Clone and Copy too. I have made all three of these insta-stable, because: AIUI Copycan only be derived, and I was not able to find any examples of how to unstably derive it. I think it is probably not possible. I hunted through the git history for precedent and found 79b8ad84c84481a43704213cd0948d2ba0ea63b4 Implement `Copy` for `IoSlice` https://github.com/rust-lang/rust/pull/69403 which was also insta-stable. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-06-24Use `#[non_exhaustive]` where appropriateJacob Pratt-8/+6
Due to the std/alloc split, it is not possible to make `alloc::collections::TryReserveError::AllocError` non-exhaustive without having an unstable, doc-hidden method to construct (which negates the benefits from `#[non_exhaustive]`.
2021-06-10Specialize `io::Bytes::size_hint` for more typesBenoît du Garreau-0/+13
2021-04-21Replace all `fmt.pad` with `debug_struct`Christiaan Dirkx-3/+3
2021-01-31Fix line length formatXavientois-1/+3
2021-01-31Add space for proper indentationXavientois-1/+1
2021-01-31Implement SizeHint trait for BufReader, Emtpy, and ChainXavientois-1/+7
2021-01-30impl Seek for Emptyoberien-1/+16
Fix #78029
2020-11-14Auto merge of #75272 - the8472:spec-copy, r=KodrAusbors-72/+1
specialize io::copy to use copy_file_range, splice or sendfile Fixes #74426. Also covers #60689 but only as an optimization instead of an official API. The specialization only covers std-owned structs so it should avoid the problems with #71091 Currently linux-only but it should be generalizable to other unix systems that have sendfile/sosplice and similar. There is a bit of optimization potential around the syscall count. Right now it may end up doing more syscalls than the naive copy loop when doing short (<8KiB) copies between file descriptors. The test case executes the following: ``` [pid 103776] statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_ALL|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=17, ...}) = 0 [pid 103776] write(4, "wxyz", 4) = 4 [pid 103776] write(4, "iklmn", 5) = 5 [pid 103776] copy_file_range(3, NULL, 4, NULL, 5, 0) = 5 ``` 0-1 `stat` calls to identify the source file type. 0 if the type can be inferred from the struct from which the FD was extracted 𝖬 `write` to drain the `BufReader`/`BufWriter` wrappers. only happen when buffers are present. 𝖬 ≾ number of wrappers present. If there is a write buffer it may absorb the read buffer contents first so only result in a single write. Vectored writes would also be an option but that would require more invasive changes to `BufWriter`. 𝖭 `copy_file_range`/`splice`/`sendfile` until file size, EOF or the byte limit from `Take` is reached. This should generally be *much* more efficient than the read-write loop and also have other benefits such as DMA offload or extent sharing. ## Benchmarks ``` OLD test io::tests::bench_file_to_file_copy ... bench: 21,002 ns/iter (+/- 750) = 6240 MB/s [ext4] test io::tests::bench_file_to_file_copy ... bench: 35,704 ns/iter (+/- 1,108) = 3671 MB/s [btrfs] test io::tests::bench_file_to_socket_copy ... bench: 57,002 ns/iter (+/- 4,205) = 2299 MB/s test io::tests::bench_socket_pipe_socket_copy ... bench: 142,640 ns/iter (+/- 77,851) = 918 MB/s NEW test io::tests::bench_file_to_file_copy ... bench: 14,745 ns/iter (+/- 519) = 8889 MB/s [ext4] test io::tests::bench_file_to_file_copy ... bench: 6,128 ns/iter (+/- 227) = 21389 MB/s [btrfs] test io::tests::bench_file_to_socket_copy ... bench: 13,767 ns/iter (+/- 3,767) = 9520 MB/s test io::tests::bench_socket_pipe_socket_copy ... bench: 26,471 ns/iter (+/- 6,412) = 4951 MB/s ```
2020-11-13specialize io::copy to use copy_file_range, splice or sendfileThe8472-72/+1
Currently it only applies to linux systems. It can be extended to make use of similar syscalls on other unix systems.
2020-11-06Add tracking issueBenoît du Garreau-3/+3
2020-11-06Make some std::io functions `const`Benoît du Garreau-3/+6
Includes: - io::Cursor::new - io::Cursor::get_ref - io::Cursor::position - io::empty - io::repeat - io::sink
2020-09-21Rollup merge of #76275 - FedericoPonzi:immutable-write-impl-73836, r=dtolnayecstatic-morse-0/+24
Implementation of Write for some immutable ref structs Fixes #73836
2020-09-21Updates stability attributes to the current nightly versionFederico Ponzi-1/+1
2020-09-03More implementations of Write for immutable refsFederico Ponzi-0/+24
Fixes #73836
2020-09-02Read: adjust a FIXME referenceRalf Jung-5/+8
2020-09-01Auto merge of #76047 - Dylan-DPC:rename/maybe, r=RalfJungbors-4/+4
rename get_{ref, mut} to assume_init_{ref,mut} in Maybeuninit References #63568 Rework with comments addressed from #66174 Have replaced most of the occurrences I've found, hopefully didn't miss out anything r? @RalfJung (thanks @danielhenrymantilla for the initial work on this)
2020-08-31std: move "mod tests/benches" to separate filesLzu Tao-49/+3
Also doing fmt inplace as requested.
2020-08-30update fixmesDPC-1/+1
2020-08-29rename get_{ref, mut} to assume_init_{ref,mut} in MaybeuninitDPC-3/+3
2020-08-18Move to intra doc links for std::ioAlexis Bourget-18/+15
2020-07-27mv std libs to library/mark-0/+308