about summary refs log tree commit diff
path: root/library/std/src/io/mod.rs
AgeCommit message (Collapse)AuthorLines
2021-03-27Derive Debug for io::Chain instead of manually implementing it.Mara Bos-7/+1
The manual implementation has the same bounds, so I don't think there's any reason for a manual implementation. The names used in the derive implementation are even nicer (`first`/`second`) than the manual implementation (`t`/`u`), and include the `done_first` field too.
2021-03-24Rollup merge of #83353 - m-ou-se:io-error-avoid-alloc, r=nagisaDylan DPC-5/+11
Add internal io::Error::new_const to avoid allocations. This makes it possible to have a io::Error containing a message with zero allocations, and uses that everywhere to avoid the *three* allocations involved in `io::Error::new(kind, "message")`. The function signature isn't perfect, because it needs a reference to the `&str`. So for now, this is just a `pub(crate)` function. Later, we'll be able to use `fn new_const<MSG: &'static str>(kind: ErrorKind)` to make that a bit better. (Then we'll also be able to use some ZST trickery if that would result in more efficient code.) See https://github.com/rust-lang/rust/issues/83352
2021-03-21Use io::Error::new_const everywhere to avoid allocations.Mara Bos-5/+11
2021-03-18Clarify docs for Read::read's return valueJannis Harder-2/+7
2021-03-05Rollup merge of #81136 - Xavientois:io_reader_size_hint, r=cramertjMara-0/+37
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-02-25Convert primitives to use intra-doc linksJoshua Nelson-2/+1
2021-01-31Fix formatting on modXavientois-2/+1
2021-01-31Implement SizeHint trait for BufReader, Emtpy, and ChainXavientois-6/+14
2021-01-31Use fully qualified syntax to avoid dynXavientois-1/+1
2021-01-31Remove unnecessary default keywordXavientois-4/+6
2021-01-31Remove stable annotationXavientois-3/+0
2021-01-31Fix formattingXavientois-10/+2
2021-01-31Add dyn for SizeHint castXavientois-1/+1
2021-01-31Remove exposing private traitXavientois-2/+2
2021-01-31Fix semicolonXavientois-1/+1
2021-01-31Add back lower_bound as memeberXavientois-0/+2
2021-01-31Add default keyword for specializationXavientois-5/+5
2021-01-31Fix incorrect tokenXavientois-1/+1
2021-01-31Move default to trait definitionXavientois-6/+4
2021-01-31Add missing genericXavientois-1/+1
2021-01-31Use helper trait to follow min_specialization rulesXavientois-4/+24
2021-01-31Fix implementation to specializeXavientois-1/+1
2021-01-31Implement size_hint for BufReaderXavientois-2/+21
2021-01-24Stabilize `Seek::stream_position` & change feature of `Seek::stream_len`Lukas Kalbertodt-4/+3
2021-01-17Add benchmark and fast path for BufReader::read_exactBen Kimock-17/+21
2021-01-14Rollup merge of #80895 - sfackler:read-to-end-ub, r=m-ou-seMara Bos-12/+10
Fix handling of malicious Readers in read_to_end A malicious `Read` impl could return overly large values from `read`, which would result in the guard's drop impl setting the buffer's length to greater than its capacity! ~~To fix this, the drop impl now uses the safe `truncate` function instead of `set_len` which ensures that this will not happen. The result of calling the function will be nonsensical, but that's fine given the contract violation of the `Read` impl.~~ ~~The `Guard` type is also used by `append_to_string` which does not pass untrusted values into the length field, so I've copied the guard type into each function and only modified the one used by `read_to_end`. We could just keep a single one and modify it, but it seems a bit cleaner to keep the guard code close to the functions and related specifically to them.~~ To fix this, we now assert that the returned length is not larger than the buffer passed to the method. For reference, this bug has been present for ~2.5 years since 1.20: https://github.com/rust-lang/rust/commit/ecbb896b9eb2acadefde57be493e4298c1aa04a3. Closes #80894.
2021-01-14Rollup merge of #80217 - camelid:io-read_to_string, r=m-ou-seMara Bos-0/+48
Add a `std::io::read_to_string` function I recognize that you're usually supposed to open an issue first, but the implementation is very small so it's okay if this is closed and it was 'wasted work' :) ----- The equivalent of `std::fs::read_to_string`, but generalized to all `Read` impls. As the documentation on `std::io::read_to_string` says, the advantage of this function is that it means you don't have to create a variable first and it provides more type safety since you can only get the buffer out if there were no errors. If you use `Read::read_to_string`, you have to remember to check whether the read succeeded because otherwise your buffer will be empty. It's friendlier to newcomers and better in most cases to use an explicit return value instead of an out parameter.
2021-01-11Add docs on performanceCamelid-0/+13
2021-01-11clarify docs a bitSteven Fackler-4/+3
2021-01-11make check a bit more clearSteven Fackler-2/+3
2021-01-11clean up control flowSteven Fackler-11/+2
2021-01-11Fix handling of malicious Readers in read_to_endSteven Fackler-1/+8
2020-12-31Remove many unnecessary manual link resolves from libraryCamelid-2/+0
Now that #76934 has merged, we can remove a lot of these! E.g, this is no longer necessary: [`Vec<T>`]: Vec
2020-12-30Add error docsCamelid-1/+8
2020-12-30Add description independent of `Read::read_to_string`Camelid-5/+6
2020-12-19Add a `std::io::read_to_string` functionCamelid-0/+27
The equivalent of `std::fs::read_to_string`, but generalized to all `Read` impls. As the documentation on `std::io::read_to_string` says, the advantage of this function is that it means you don't have to create a variable first and it provides more type safety since you can only get the buffer out if there were no errors. If you use `Read::read_to_string`, you have to remember to check whether the read succeeded because otherwise your buffer will be empty. It's friendlier to newcomers and better in most cases to use an explicit return value instead of an out parameter.
2020-11-17Fix typo in `std::io::Write` docsWilliam Chargin-2/+2
These referred to a “`Write`er”—extra *e*. Presumably a copy-paste holdover from “`Read`er”. Test Plan: Running ``git grep '`\?[Ww]rite`\?er'`` no longer finds any results. wchargin-branch: io-write-docs
2020-11-16Rollup merge of #78714 - m-ou-se:simplify-local-streams, r=KodrAusMara Bos-5/+3
Simplify output capturing This is a sequence of incremental improvements to the unstable/internal `set_panic` and `set_print` mechanism used by the `test` crate: 1. Remove the `LocalOutput` trait and use `Arc<Mutex<dyn Write>>` instead of `Box<dyn LocalOutput>`. In practice, all implementations of `LocalOutput` were just `Arc<Mutex<..>>`. This simplifies some logic and removes all custom `Sink` implementations such as `library/test/src/helpers/sink.rs`. Also removes a layer of indirection, as the outermost `Box` is now gone. It also means that locking now happens per `write_fmt`, not per individual `write` within. (So `"{} {}\n"` now results in one `lock()`, not four or more.) 2. Since in all cases the `dyn Write`s were just `Vec<u8>`s, replace the type with `Arc<Mutex<Vec<u8>>>`. This simplifies things more, as error handling and flushing can be removed now. This also removes the hack needed in the default panic handler to make this work with `::realstd`, as (unlike `Write`) `Vec<u8>` is from `alloc`, not `std`. 3. Replace the `RefCell`s by regular `Cell`s. The `RefCell`s were mostly used as `mem::replace(&mut *cell.borrow_mut(), something)`, which is just `Cell::replace`. This removes an unecessary bookkeeping and makes the code a bit easier to read. 4. Merge `set_panic` and `set_print` into a single `set_output_capture`. Neither the test crate nor rustc (the only users of this feature) have a use for using these separately. Merging them simplifies things even more. This uses a new function name and feature name, to make it clearer this is internal and not supposed to be used by other crates. Might be easier to review per commit.
2020-11-14Auto merge of #75272 - the8472:spec-copy, r=KodrAusbors-1/+4
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-13hide unused exports on other platformsThe8472-3/+1
2020-11-13specialize io::copy to use copy_file_range, splice or sendfileThe8472-1/+6
Currently it only applies to linux systems. It can be extended to make use of similar syscalls on other unix systems.
2020-11-10Merge set_panic and set_print into set_output_capture.Mara Bos-5/+3
There were no use cases for setting them separately. Merging them simplifies some things.
2020-11-10Remove io::LocalOutput and use Arc<Mutex<dyn>> for local streams.Mara Bos-1/+1
2020-10-22Capture output from threads spawned in testsTyler Mandry-1/+3
Fixes #42474.
2020-09-24Remove std::io::lazy::Lazy in favour of SyncOnceCellMara Bos-1/+0
The (internal) std::io::lazy::Lazy was used to lazily initialize the stdout and stdin buffers (and mutexes). It uses atexit() to register a destructor to flush the streams on exit, and mark the streams as 'closed'. Using the stream afterwards would result in a panic. Stdout uses a LineWriter which contains a BufWriter that will flush the buffer on drop. This one is important to be executed during shutdown, to make sure no buffered output is lost. It also forbids access to stdout afterwards, since the buffer is already flushed and gone. Stdin uses a BufReader, which does not implement Drop. It simply forgets any previously read data that was not read from the buffer yet. This means that in the case of stdin, the atexit() function's only effect is making stdin inaccessible to the program, such that later accesses result in a panic. This is uncessary, as it'd have been safe to access stdin during shutdown of the program. --- This change removes the entire io::lazy module in favour of SyncOnceCell. SyncOnceCell's fast path is much faster (a single atomic operation) than locking a sys_common::Mutex on every access like Lazy did. However, SyncOnceCell does not use atexit() to drop the contained object during shutdown. As noted above, this is not a problem for stdin. It simply means stdin is now usable during shutdown. The atexit() call for stdout is moved to the stdio module. Unlike the now-removed Lazy struct, SyncOnceCell does not have a 'gone and unusable' state that panics. Instead of adding this again, this simply replaces the buffer with one with zero capacity. This effectively flushes the old buffer *and* makes any writes afterwards pass through directly without touching a buffer, making print!() available during shutdown without panicking.
2020-08-31std: move "mod tests/benches" to separate filesLzu Tao-498/+3
Also doing fmt inplace as requested.
2020-08-23Convert str -> prim@str in `std`Joshua Nelson-1/+1
2020-08-19Fix nits in intra-doc links for std ioAlexis Bourget-22/+22
2020-08-18Move to intra doc links for std::ioAlexis Bourget-39/+42
2020-08-12Fix intra-doc linkCamelid-0/+1