about summary refs log tree commit diff
path: root/library/std/src/process.rs
AgeCommit message (Collapse)AuthorLines
2023-03-06Implement read_buf for a few more typesTomasz Miąsko-1/+9
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.
2023-03-03Match unmatched backticks in library/est31-1/+1
2022-12-28delete more `cfg(bootstrap)`Lukas Markeffsky-12/+5
2022-12-27More verbose `Debug` implementation of `std::process:Command`kraktus-0/+9
based on commit: https://github.com/zackmdavis/rust/commit/ccc019aabfdd550944c049625e66c92c815ea1d0 from https://github.com/zackmdavis close https://github.com/rust-lang/rust/issues/42200 Add env variables and cwd to the shell-like debug output. Also use the alternate syntax to display a more verbose display, while not showing internal fields and hiding fields when they have their default value.
2022-12-11Implement blocking outputAyush Singh-4/+2
This allows decoupling `Command::spawn` and `Command::output`. This is useful for targets which do support launching programs in blocking mode but do not support multitasking (Eg: UEFI). This was originally conceived when working on https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-12-08Add read_to_end for AnonPipeAyush Singh-0/+4
Add `read_to_end` method for `sys::{target}::pipe::AnonPipe`. This allows having a more optimized version of `read_to_end` for ChildStdout. Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-10-17Make diagnostic for unsatisfied Termination bounds more preciseLeón Orell Valerian Liehr-2/+10
2022-10-14more dupe word typosRageking8-1/+1
2022-10-07Make tests capture the error printed by a Result returnDavid Tolnay-3/+1
2022-08-21Make doc for stdin field of process consistentNelson Chen-5/+5
The other fields use this format and example.
2022-07-07Rollup merge of #97917 - AronParker:master, r=ChrisDentonMatthias Krüger-0/+16
Implement ExitCodeExt for Windows Fixes #97914 ### Motivation: On Windows it is common for applications to return `HRESULT` (`i32`) or `DWORD` (`u32`) values. These stem from COM based components ([HRESULTS](https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize)), Win32 errors ([GetLastError](https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror)), GUI applications ([WM_QUIT](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-quit)) and more. The newly stabilized `ExitCode` provides an excellent fit for propagating these values, because `std::process::exit` does not run deconstructors which can result in errors. However, `ExitCode` currently only implements `From<u8> for ExitCode`, which disallows the full range of `i32`/`u32` values. This pull requests attempts to address that shortcoming by providing windows specific extensions that accept a `u32` value (which covers all possible `HRESULTS` and Win32 errors) analog to [ExitStatusExt::from_raw](https://doc.rust-lang.org/std/os/windows/process/trait.ExitStatusExt.html#tymethod.from_raw). This was also intended by the original Stabilization https://github.com/rust-lang/rust/pull/93840#issue-1129209143= as pointed out by ``@eggyal`` in https://github.com/rust-lang/rust/issues/97914#issuecomment-1151076755: > Issues around platform specific representations: We resolved this issue by changing the return type of report from i32 to the opaque type ExitCode. __That way we can change the underlying representation without affecting the API, letting us offer full support for platform specific exit code APIs in the future.__ [Emphasis added] ### API ```rust /// Windows-specific extensions to [`process::ExitCode`]. /// /// This trait is sealed: it cannot be implemented outside the standard library. /// This is so that future additional methods are not breaking changes. #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] pub trait ExitCodeExt: Sealed { /// Creates a new `ExitCode` from the raw underlying `u32` return value of /// a process. #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] fn from_raw(raw: u32) -> Self; } #[stable(feature = "windows_process_exit_code_from", since = "1.63.0")] impl ExitCodeExt for process::ExitCode { fn from_raw(raw: u32) -> Self { process::ExitCode::from_inner(From::from(raw)) } } ``` ### Misc I apologize in advance if I misplaced any attributes regarding stabilzation, as far as I learned traits are insta-stable so I chose to make them stable. If this is an error, please let me know and I'll correct it. I also added some additional machinery to make it work, analog to [ExitStatus](https://doc.rust-lang.org/std/process/struct.ExitStatus.html#). EDIT: Proposal: https://github.com/rust-lang/libs-team/issues/48
2022-06-20Rollup merge of #97150 - ChrisDenton:stdio-create_pipe, r=m-ou-seDylan DPC-0/+16
`Stdio::makes_pipe` Wrappers around `std::process::Command` may want to be able to override pipe creation. However, [`std::process::Stdio`](https://doc.rust-lang.org/std/process/struct.Stdio.html) is opaque so there's no way to tell if `Command` was told to create new pipes or not. This is in some ways a more generic (and cross-platform) alternative to #97149. However, unlike that feature, this comes with the price of the user needing to actually create their own pipes rather than reusing the std one. So I think it stands (or not) on its own. # Example ```rust #![feature(stdio_makes_pipe)] use std::process::Stdio; let io = Stdio::piped(); assert_eq!(io.makes_pipe(), true); ```
2022-06-20`Stdio::make_pipe`Chris Denton-0/+16
2022-06-17Impl Termination for Infallible and then make the Result impls of ↵Aria Beingessner-22/+15
Termination into a blanket This allows things like `Result<ExitCode, E>` to 'just work'
2022-06-12Rollup merge of #97970 - dtolnay:terminate, r=joshtriplettDylan DPC-1/+3
Fix Termination impl panic on closed stderr Repro: ```rust #![feature(backtrace)] use std::backtrace::Backtrace; use std::io::{self, Write as _}; use std::panic::{self, PanicInfo}; #[derive(Debug)] pub struct Error; fn panic_hook(panic_info: &PanicInfo) { let backtrace = Backtrace::force_capture(); let _ = write!(io::stdout(), "{}\n{}", panic_info, backtrace); } fn main() -> Result<(), Error> { panic::set_hook(Box::new(panic_hook)); let stderr = io::stderr(); let mut stderr = stderr.lock(); while stderr.write_all(b".\n").is_ok() {} Err(Error) } ``` ### Before: ```console $ target/debug/repro 3>&2 2>&1 1>&3 | head . . . . . . . . . . panicked at 'failed printing to stderr: Broken pipe (os error 32)', library/std/src/io/stdio.rs:1016:9 0: testing::panic_hook at ./src/main.rs:11:21 1: core::ops::function::Fn::call at /git/rust/library/core/src/ops/function.rs:77:5 2: std::panicking::rust_panic_with_hook 3: std::panicking::begin_panic_handler::{{closure}} 4: std::sys_common::backtrace::__rust_end_short_backtrace 5: rust_begin_unwind 6: core::panicking::panic_fmt 7: std::io::stdio::_eprint 8: <core::result::Result<!,E> as std::process::Termination>::report at /git/rust/library/std/src/process.rs:2164:9 9: <core::result::Result<(),E> as std::process::Termination>::report at /git/rust/library/std/src/process.rs:2148:25 10: std::rt::lang_start::{{closure}} at /git/rust/library/std/src/rt.rs:145:18 11: std::rt::lang_start_internal 12: std::rt::lang_start at /git/rust/library/std/src/rt.rs:144:17 13: main 14: __libc_start_main at /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16 15: _start ``` ### After: ```console $ target/debug/repro 3>&2 2>&1 1>&3 | head . . . . . . . . . . ```
2022-06-10Do not panic in Termination impl on closed stderrDavid Tolnay-1/+3
Repro: #![feature(backtrace)] use std::backtrace::Backtrace; use std::io::{self, Write as _}; use std::panic::{self, PanicInfo}; #[derive(Debug)] pub struct Error; fn panic_hook(panic_info: &PanicInfo) { let backtrace = Backtrace::force_capture(); let _ = write!(io::stdout(), "{}\n{}", panic_info, backtrace); } fn main() -> Result<(), Error> { panic::set_hook(Box::new(panic_hook)); let stderr = io::stderr(); let mut stderr = stderr.lock(); while stderr.write_all(b".\n").is_ok() {} Err(Error) } Before: $ RUST_BACKTRACE=1 target/debug/testing 3>&2 2>&1 1>&3 | head . . . . . . . . . . panicked at 'failed printing to stderr: Broken pipe (os error 32)', library/std/src/io/stdio.rs:1016:9 0: testing::panic_hook at ./src/main.rs:11:21 1: core::ops::function::Fn::call at /git/rust/library/core/src/ops/function.rs:77:5 2: std::panicking::rust_panic_with_hook 3: std::panicking::begin_panic_handler::{{closure}} 4: std::sys_common::backtrace::__rust_end_short_backtrace 5: rust_begin_unwind 6: core::panicking::panic_fmt 7: std::io::stdio::_eprint 8: <core::result::Result<!,E> as std::process::Termination>::report at /git/rust/library/std/src/process.rs:2164:9 9: <core::result::Result<(),E> as std::process::Termination>::report at /git/rust/library/std/src/process.rs:2148:25 10: std::rt::lang_start::{{closure}} at /git/rust/library/std/src/rt.rs:145:18 11: std::rt::lang_start_internal 12: std::rt::lang_start at /git/rust/library/std/src/rt.rs:144:17 13: main 14: __libc_start_main at /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16 15: _start After: $ RUST_BACKTRACE=1 target/debug/testing 3>&2 2>&1 1>&3 | head . . . . . . . . . .
2022-06-10docs: Consistently mark ExitStatus as codeMartin Kröning-2/+2
2022-06-10docs: Link to ExitCode instead of ExitStatus in ExitStatusMartin Kröning-2/+2
2022-06-10docs: Fix typo in ExitStatusMartin Kröning-1/+1
2022-06-09Implement ExitCodeExt for WindowsAron Parker-0/+16
2022-05-22small changeProloy Mishra-1/+1
2022-05-19Remove unnecessay .report() on ExitCodebenediktwerner-2/+2
2022-05-16Add tracking issue for ExitCode::exit_processNoa-1/+1
2022-05-13Auto merge of #95356 - coolreader18:exitstatus-exit-method, r=<try>bors-28/+56
ExitCode::exit_process() method cc `@yaahc` / #93840 (eeek, hit ctrl-enter before I meant to and right after realizing the branch name was wrong. oh, well) I feel like it makes sense to have the `exit(ExitCode)` function as a method or at least associated function on ExitCode, but maybe that would hurt discoverability? Probably not as much if it's at the top of the `process::exit()` documentation or something, but idk. Also very unsure about the name, I'd like something that communicates that you are exiting with *this* ExitCode, but with a method name being postfix it doesn't seem to flow. `code.exit_process_with()` ? `.exit_process_with_self()` ? Blech. Maybe it doesn't matter, since ideally just `code.exit()` or something would be clear simply by the name and single parameter but :shrug: Also I'd like to touch up the `ExitCode` docs (which I did a bit here), but that would probably be good in a separate PR, right? Since I think the beta deadline is coming up.
2022-05-13Guarantee less in docsNoa-2/+2
2022-05-13Add ExitCode::exit_process exampleNoa-0/+23
2022-03-30Remove antipattern from process::exit docsNoa-28/+13
2022-03-30Add ExitCode::exit_process()Noa-0/+20
2022-03-29fix since field version for termination stabilizationJane Lusby-13/+13
2022-03-29Rollup merge of #93840 - ↵Dylan DPC-24/+68
yaahc:termination-stabilization-celebration-station, r=joshtriplett Stabilize Termination and ExitCode From https://github.com/rust-lang/rust/issues/43301 This PR stabilizes the Termination trait and associated ExitCode type. It also adjusts the ExitCode feature flag to replace the placeholder flag with a more permanent name, as well as splitting off the `to_i32` method behind its own permanently unstable feature flag. This PR stabilizes the termination trait with the following signature: ```rust pub trait Termination { fn report(self) -> ExitCode; } ``` The existing impls of `Termination` are effectively already stable due to the prior stabilization of `?` in main. This PR also stabilizes the following APIs on exit code ```rust #[derive(Clone, Copy, Debug)] pub struct ExitCode(_); impl ExitCode { pub const SUCCESS: ExitCode; pub const FAILURE: ExitCode; } impl From<u8> for ExitCode { /* ... */ } ``` --- All of the previous blockers have been resolved. The main ones that were resolved recently are: * The trait's name: We decided against changing this since none of the alternatives seemed particularly compelling. Instead we decided to end the bikeshedding and stick with the current name. ([link to the discussion](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Termination.2FExit.20Status.20Stabilization/near/269793887)) * Issues around platform specific representations: We resolved this issue by changing the return type of `report` from `i32` to the opaque type `ExitCode`. That way we can change the underlying representation without affecting the API, letting us offer full support for platform specific exit code APIs in the future. * Custom exit codes: We resolved this by adding `From<u8> for ExitCode`. We choose to only support u8 initially because it is the least common denominator between the sets of exit codes supported by our current platforms. In the future we anticipate adding platform specific extension traits to ExitCode for constructors from larger or negative numbers, as needed.
2022-03-28Touch up ExitCode docsNoa-7/+26
2022-03-25std::process docs: linkify references to output, spawn and statusest31-7/+20
2022-03-22add some fixZHANGWENTAI-3/+4
Signed-off-by: ZHANGWENTAI <2092913428@qq.com>
2022-03-22fix the lint problemZHANGWENTAI-1/+1
Signed-off-by: ZHANGWENTAI <2092913428@qq.com>
2022-03-22update Termination trait docsZHANGWENTAI-0/+4
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-9/+9
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-02-22Stabilize Termination and ExitCodeJane Lusby-19/+44
2022-02-17Rollup merge of #89869 - kpreid:from-doc, r=yaahcMatthias Krüger-4/+4
Add documentation to more `From::from` implementations. For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * The new documentation for construction of maps and sets from arrays of keys mentions the handling of duplicates. Future work could be to do this for *all* code paths that convert an iterable to a map or set. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2022-02-06Add From<u8> for ExitCodeJane Lusby-0/+8
This should cover a mostly cross-platform subset of supported exit codes.
2022-01-31add inline attribute to new methodJane Lusby-1/+2
2022-01-28Change Termination::report return type to ExitCodeJane Lusby-8/+22
2021-12-18Update stdlib to the 2021 editionLucas Kent-1/+0
2021-12-09Auto merge of #81156 - DrMeepster:read_buf, r=joshtriplettbors-13/+1
Implement most of RFC 2930, providing the ReadBuf abstraction This replaces the `Initializer` abstraction for permitting reading into uninitialized buffers, closing #42788. This leaves several APIs described in the RFC out of scope for the initial implementation: * read_buf_vectored * `ReadBufs` Closes #42788, by removing the relevant APIs.
2021-12-04Add documentation to more `From::from` implementations.Kevin Reid-4/+4
For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2021-11-16Rollup merge of #88601 - ibraheemdev:termination-result-infallible, r=yaahcYuki Okushi-0/+9
Implement `Termination` for `Result<Infallible, E>` As noted in #43301, `Result<!, E>` is not usable on stable.
2021-11-11process::ExitStatus: Discuss `exit` vs `_exit` in a comment.Ian Jackson-0/+5
As discussed here https://github.com/rust-lang/rust/pull/88300#issuecomment-936097710 I felt this was the best place to put this (rather than next to ExitStatusExt). After all, it's a property of the ExitStatus type on Unix. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-11-02read_bufDrMeepster-13/+1
2021-10-30Add #[must_use] to remaining std functions (O-Z)John Kugelman-0/+13
2021-10-09Rollup merge of #88436 - lf-:stabilize-command-access, r=yaahcGuillaume Gomez-12/+8
std: Stabilize command_access Tracking issue: #44434 (not yet closed but the FCP is done so that should be soon).
2021-10-05Apply suggestions from code reviewJane Lusby-8/+8