about summary refs log tree commit diff
path: root/library/std/src/io/stdio.rs
AgeCommit message (Collapse)AuthorLines
2023-03-28Add "Platform-specific behavior" heading and link to changes disclaimerJosh Triplett-0/+5
2023-03-28Document the heuristics IsTerminal uses on WindowsJosh Triplett-0/+4
2023-03-06Implement read_buf for a few more typesTomasz Miąsko-1/+12
Implement read_buf for TcpStream, Stdin, StdinLock, ChildStdout, ChildStderr (and internally for AnonPipe, Handle, Socket), so that it skips buffer initialization. The other provided methods like read_to_string and read_to_end are implemented in terms of read_buf and so benefit from the optimization as well. This commit also implements read_vectored and is_read_vectored where applicable.
2022-11-14std: move `ReentrantMutex` to `sync`joboet-2/+1
2022-10-15Add `IsTerminal` trait to determine if a descriptor or handle is a terminalJosh Triplett-0/+29
The UNIX and WASI implementations use `isatty`. The Windows implementation uses the same logic the `atty` crate uses, including the hack needed to detect msys terminals. Implement this trait for `File` and for `Stdin`/`Stdout`/`Stderr` and their locked counterparts on all platforms. On UNIX and WASI, implement it for `BorrowedFd`/`OwnedFd`. On Windows, implement it for `BorrowedHandle`/`OwnedHandle`. Based on https://github.com/rust-lang/rust/pull/91121 Co-authored-by: Matt Wilkinson <mattwilki17@gmail.com>
2022-10-07Make tests capture the error printed by a Result returnDavid Tolnay-6/+22
2022-09-07stdio: Document no support for writing to non-blocking stdio/stderrUsama Arif-0/+3
Printing to stdio/stderr that have been opened with non-blocking (O_NONBLOCK in linux) can result in an error, which is not handled by std::io module causing a panic. Signed-off-by: Usama Arif <usama.arif@bytedance.com>
2022-09-05std: fix cleanup for uninitialized stdout (#101375)joboet-8/+17
2022-09-04Auto merge of #100576 - joboet:movable_const_remutex, r=Mark-Simulacrumbors-25/+16
Make `ReentrantMutex` movable and `const` As `MovableMutex` is now `const`, it can be used to simplify the implementation and interface of the internal reentrant mutex type. Consequently, the standard error stream does not need to be wrapped in `OnceLock` and `OnceLock::get_or_init_pin()` can be removed.
2022-09-03std: make `ReentrantMutex` movable and `const`; simplify `Stdout` initializationjoboet-25/+16
2022-09-01Update outdated comment about output capturing in print_to.Mara Bos-4/+4
2022-07-23Remove `mut`Phosra-1/+1
2022-06-16Move/rename `lazy::Sync{OnceCell,Lazy}` to `sync::{Once,Lazy}Lock`Maybe Waffle-5/+4
2022-04-06Bump stabilization of stdin_forwarders to 1.62.0.Mara Bos-1/+1
2022-03-21Stabilize Stdin::lines.Mara Bos-2/+1
2022-03-19Rollup merge of #93263 - sunfishcode:sunfishcode/detatched-console-handle, ↵Dylan DPC-9/+70
r=dtolnay Consistently present absent stdio handles on Windows as NULL handles. This addresses #90964 by making the std API consistent about presenting absent stdio handles on Windows as NULL handles. Stdio handles may be absent due to `#![windows_subsystem = "windows"]`, due to the console being detached, or due to a child process having been launched from a parent where stdio handles are absent. Specifically, this fixes the case of child processes of parents with absent stdio, which previously ended up with `stdin().as_raw_handle()` returning `INVALID_HANDLE_VALUE`, which was surprising, and which overlapped with an unrelated valid handle value. With this patch, `stdin().as_raw_handle()` now returns null in these situation, which is consistent with what it does in the parent process. And, document this in the "Windows Portability Considerations" sections of the relevant documentation.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-4/+4
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-04Consistently present absent stdio handles on Windows as NULL handles.Dan Gohman-9/+70
This addresses #90964 by making the std API consistent about presenting absent stdio handles on Windows as NULL handles. Stdio handles may be absent due to `#![windows_subsystem = "windows"]`, due to the console being detached, or due to a child process having been launched from a parent where stdio handles are absent. Specifically, this fixes the case of child processes of parents with absent stdio, which previously ended up with `stdin().as_raw_handle()` returning `INVALID_HANDLE_VALUE`, which was surprising, and which overlapped with an unrelated valid handle value. With this patch, `stdin().as_raw_handle()` now returns null in these situation, which is consistent with what it does in the parent process. And, document this in the "Windows Portability Considerations" sections of the relevant documentation.
2022-02-13Make default stdio lock() return 'static handlesMark Rousskov-223/+15
This also deletes the unstable API surface area previously added to expose this functionality on new methods rather than built into the current set.
2022-01-20delete `Stdin::split` forwarderTaylor Yu-24/+1
2021-12-14Fix a bunch of typosFrank Steffahn-1/+1
2021-11-02read_bufDrMeepster-15/+1
2021-10-31Rollup merge of #90430 - jkugelman:must-use-std-a-through-n, r=joshtriplettMatthias Krüger-0/+3
Add #[must_use] to remaining std functions (A-N) I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from A-N. I added these functions myself. Clippy predictably ignored the `mut` ones, but I don't know why the rest weren't flagged. Check them closely, please? Maybe I overlooked good reasons. ```rust std::backtrace::Backtrace const fn disabled() -> Backtrace; std::backtrace::Backtrace<'a> fn frames(&'a self) -> &'a [BacktraceFrame]; std::collections::hash_map::RawOccupiedEntryMut<'a, K, V> fn key_mut(&mut self) -> &mut K; std::collections::hash_map::RawOccupiedEntryMut<'a, K, V> fn get_mut(&mut self) -> &mut V; std::collections::hash_map::RawOccupiedEntryMut<'a, K, V> fn get_key_value(&mut self) -> (&K, &V); std::collections::hash_map::RawOccupiedEntryMut<'a, K, V> fn get_key_value_mut(&mut self) -> (&mut K, &mut V); std::env fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString>; std::env fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths<'_>; std::io::Error fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)>; ``` Parent issue: #89692 r? `@joshtriplett`
2021-10-30Add #[must_use] to remaining std functions (A-N)John Kugelman-0/+3
2021-10-12Add #[must_use] to expensive computationsJohn Kugelman-0/+1
The unifying theme for this commit is weak, admittedly. I put together a list of "expensive" functions when I originally proposed this whole effort, but nobody's cared about that criterion. Still, it's a decent way to bite off a not-too-big chunk of work. Given the grab bag nature of this commit, the messages I used vary quite a bit.
2021-10-10Add #[must_use] to conversions that move selfJohn Kugelman-0/+1
2021-10-08Add #[must_use] to stdin/stdout/stderr locksJohn Kugelman-0/+3
2021-08-06Replace read_to_string with read_line in Stdin exampleElichai Turkel-14/+14
2021-07-12add Stdin::lines, Stdin::split forwarder methodsTaylor Yu-1/+44
Add forwarder methods `Stdin::lines` and `Stdin::split`, which consume and lock a `Stdin` handle, and forward on to the corresponding `BufRead` methods. This should make it easier for beginners to use those iterator constructors without explicitly dealing with locks or lifetimes.
2021-07-03stdio_locked: add tracking issueTaylor Yu-6/+6
2021-07-02stdio_locked: updates based on feedbackTaylor Yu-54/+11
Rename methods to `into_locked`. Remove type aliases for owned locks.
2021-07-01add owned locked stdio handlesTaylor Yu-1/+259
Add stderr_locked, stdin_locked, and stdout_locked free functions to obtain owned locked stdio handles in a single step. Also add into_lock methods to consume a stdio handle and return an owned lock. These methods will make it easier to use locked stdio handles without having to deal with lifetime problems or keeping bindings to the unlocked handles around.
2021-04-22Rework `at_exit` to `cleanup`Christiaan Dirkx-23/+18
2021-04-21Replace all `fmt.pad` with `debug_struct`Christiaan Dirkx-6/+6
2021-02-22Add missing "see its documentation for more" stdioIvan Tham-2/+2
StdoutLock and StderrLock does not have example, it would be better to leave "see its documentation for more" like iter docs.
2021-02-20Rollup merge of #82244 - pickfire:patch-6, r=dtolnayGuillaume Gomez-2/+2
Keep consistency in example for Stdin StdinLock Stdin uses handle whereas StdinLock uses stdin_lock, changed it to handle.
2021-02-18Keep consistency in example for Stdin StdinLockIvan Tham-2/+2
Stdin uses handle whereas StdinLock uses stdin_lock, changed it to handle.
2021-02-18Add missing link from stdio docIvan Tham-4/+4
2020-12-08Use Pin for the 'don't move' requirement of ReentrantMutex.Mara Bos-25/+29
The code in io::stdio before this change misused the ReentrantMutexes, by calling init() on them and moving them afterwards. Now that ReentrantMutex requires Pin for init(), this mistake is no longer easy to make.
2020-12-08Fix outdated comment about not needing to flush stderr.Mara Bos-7/+3
2020-11-16Rollup merge of #78714 - m-ou-se:simplify-local-streams, r=KodrAusMara Bos-133/+44
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-0/+8
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-0/+2
2020-11-13specialize io::copy to use copy_file_range, splice or sendfileThe8472-0/+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-85/+27
There were no use cases for setting them separately. Merging them simplifies some things.
2020-11-10Use Cell instead of RefCell for LOCAL_{STDOUT,STDERR}.Mara Bos-15/+16
2020-11-10Use Vec<u8> for LOCAL_STD{OUT,ERR} instead of dyn Write.Mara Bos-38/+23
It was only ever used with Vec<u8> anyway. This simplifies some things. - It no longer needs to be flushed, because that's a no-op anyway for a Vec<u8>. - Writing to a Vec<u8> never fails. - No #[cfg(test)] code is needed anymore to use `realstd` instead of `std`, because Vec comes from alloc, not std (like Write).
2020-11-10Remove io::LocalOutput and use Arc<Mutex<dyn>> for local streams.Mara Bos-37/+20
2020-10-22Only load LOCAL_STREAMS if they are being usedSergio Benitez-0/+5
2020-10-22Capture output from threads spawned in testsTyler Mandry-5/+28
Fixes #42474.