1 //! Windows-specific extensions to primitives in the [`std::process`] module.
3 //! [`std::process`]: crate::process
5 #![stable(feature = "process_extensions", since = "1.2.0")]
8 use crate::os::windows::io::{
9 AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
12 use crate::sealed::Sealed;
14 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
16 #[stable(feature = "process_extensions", since = "1.2.0")]
17 impl FromRawHandle for process::Stdio {
18 unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
19 let handle = sys::handle::Handle::from_raw_handle(handle as *mut _);
20 let io = sys::process::Stdio::Handle(handle);
21 process::Stdio::from_inner(io)
25 #[stable(feature = "io_safety", since = "1.63.0")]
26 impl From<OwnedHandle> for process::Stdio {
27 fn from(handle: OwnedHandle) -> process::Stdio {
28 let handle = sys::handle::Handle::from_inner(handle);
29 let io = sys::process::Stdio::Handle(handle);
30 process::Stdio::from_inner(io)
34 #[stable(feature = "process_extensions", since = "1.2.0")]
35 impl AsRawHandle for process::Child {
37 fn as_raw_handle(&self) -> RawHandle {
38 self.as_inner().handle().as_raw_handle() as *mut _
42 #[stable(feature = "io_safety", since = "1.63.0")]
43 impl AsHandle for process::Child {
45 fn as_handle(&self) -> BorrowedHandle<'_> {
46 self.as_inner().handle().as_handle()
50 #[stable(feature = "into_raw_os", since = "1.4.0")]
51 impl IntoRawHandle for process::Child {
52 fn into_raw_handle(self) -> RawHandle {
53 self.into_inner().into_handle().into_raw_handle() as *mut _
57 #[stable(feature = "io_safety", since = "1.63.0")]
58 impl From<process::Child> for OwnedHandle {
59 fn from(child: process::Child) -> OwnedHandle {
60 child.into_inner().into_handle().into_inner()
64 #[stable(feature = "process_extensions", since = "1.2.0")]
65 impl AsRawHandle for process::ChildStdin {
67 fn as_raw_handle(&self) -> RawHandle {
68 self.as_inner().handle().as_raw_handle() as *mut _
72 #[stable(feature = "process_extensions", since = "1.2.0")]
73 impl AsRawHandle for process::ChildStdout {
75 fn as_raw_handle(&self) -> RawHandle {
76 self.as_inner().handle().as_raw_handle() as *mut _
80 #[stable(feature = "process_extensions", since = "1.2.0")]
81 impl AsRawHandle for process::ChildStderr {
83 fn as_raw_handle(&self) -> RawHandle {
84 self.as_inner().handle().as_raw_handle() as *mut _
88 #[stable(feature = "into_raw_os", since = "1.4.0")]
89 impl IntoRawHandle for process::ChildStdin {
90 fn into_raw_handle(self) -> RawHandle {
91 self.into_inner().into_handle().into_raw_handle() as *mut _
95 #[stable(feature = "into_raw_os", since = "1.4.0")]
96 impl IntoRawHandle for process::ChildStdout {
97 fn into_raw_handle(self) -> RawHandle {
98 self.into_inner().into_handle().into_raw_handle() as *mut _
102 #[stable(feature = "into_raw_os", since = "1.4.0")]
103 impl IntoRawHandle for process::ChildStderr {
104 fn into_raw_handle(self) -> RawHandle {
105 self.into_inner().into_handle().into_raw_handle() as *mut _
109 /// Windows-specific extensions to [`process::ExitStatus`].
111 /// This trait is sealed: it cannot be implemented outside the standard library.
112 /// This is so that future additional methods are not breaking changes.
113 #[stable(feature = "exit_status_from", since = "1.12.0")]
114 pub trait ExitStatusExt: Sealed {
115 /// Creates a new `ExitStatus` from the raw underlying `u32` return value of
117 #[stable(feature = "exit_status_from", since = "1.12.0")]
118 fn from_raw(raw: u32) -> Self;
121 #[stable(feature = "exit_status_from", since = "1.12.0")]
122 impl ExitStatusExt for process::ExitStatus {
123 fn from_raw(raw: u32) -> Self {
124 process::ExitStatus::from_inner(From::from(raw))
128 /// Windows-specific extensions to the [`process::Command`] builder.
130 /// This trait is sealed: it cannot be implemented outside the standard library.
131 /// This is so that future additional methods are not breaking changes.
132 #[stable(feature = "windows_process_extensions", since = "1.16.0")]
133 pub trait CommandExt: Sealed {
134 /// Sets the [process creation flags][1] to be passed to `CreateProcess`.
136 /// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
138 /// [1]: https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
139 #[stable(feature = "windows_process_extensions", since = "1.16.0")]
140 fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
142 /// Forces all arguments to be wrapped in quote (`"`) characters.
144 /// This is useful for passing arguments to [MSYS2/Cygwin][1] based
145 /// executables: these programs will expand unquoted arguments containing
146 /// wildcard characters (`?` and `*`) by searching for any file paths
147 /// matching the wildcard pattern.
149 /// Adding quotes has no effect when passing arguments to programs
150 /// that use [msvcrt][2]. This includes programs built with both
153 /// [1]: <https://github.com/msys2/MSYS2-packages/issues/2176>
154 /// [2]: <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>
155 #[unstable(feature = "windows_process_extensions_force_quotes", issue = "82227")]
156 fn force_quotes(&mut self, enabled: bool) -> &mut process::Command;
158 /// Append literal text to the command line without any quoting or escaping.
160 /// This is useful for passing arguments to `cmd.exe /c`, which doesn't follow
161 /// `CommandLineToArgvW` escaping rules.
162 #[stable(feature = "windows_process_extensions_raw_arg", since = "1.62.0")]
163 fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut process::Command;
165 /// When [`process::Command`] creates pipes, request that our side is always async.
167 /// By default [`process::Command`] may choose to use pipes where both ends
168 /// are opened for synchronous read or write operations. By using
169 /// `async_pipes(true)`, this behavior is overridden so that our side is
172 /// This is important because if doing async I/O a pipe or a file has to be
173 /// opened for async access.
175 /// The end of the pipe sent to the child process will always be synchronous
176 /// regardless of this option.
181 /// #![feature(windows_process_extensions_async_pipes)]
182 /// use std::os::windows::process::CommandExt;
183 /// use std::process::{Command, Stdio};
185 /// # let program = "";
187 /// Command::new(program)
188 /// .async_pipes(true)
189 /// .stdin(Stdio::piped())
190 /// .stdout(Stdio::piped())
191 /// .stderr(Stdio::piped());
193 #[unstable(feature = "windows_process_extensions_async_pipes", issue = "98289")]
194 fn async_pipes(&mut self, always_async: bool) -> &mut process::Command;
197 #[stable(feature = "windows_process_extensions", since = "1.16.0")]
198 impl CommandExt for process::Command {
199 fn creation_flags(&mut self, flags: u32) -> &mut process::Command {
200 self.as_inner_mut().creation_flags(flags);
204 fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
205 self.as_inner_mut().force_quotes(enabled);
209 fn raw_arg<S: AsRef<OsStr>>(&mut self, raw_text: S) -> &mut process::Command {
210 self.as_inner_mut().raw_arg(raw_text.as_ref());
214 fn async_pipes(&mut self, always_async: bool) -> &mut process::Command {
215 // FIXME: This currently has an intentional no-op implementation.
216 // For the time being our side of the pipes will always be async.
217 // Once the ecosystem has adjusted, we may then be able to start making
218 // use of synchronous pipes within the standard library.
219 let _ = always_async;
224 #[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
225 pub trait ChildExt: Sealed {
226 /// Extracts the main thread raw handle, without taking ownership
227 #[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
228 fn main_thread_handle(&self) -> BorrowedHandle<'_>;
231 #[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
232 impl ChildExt for process::Child {
233 fn main_thread_handle(&self) -> BorrowedHandle<'_> {
234 self.handle.main_thread_handle()
238 /// Windows-specific extensions to [`process::ExitCode`].
240 /// This trait is sealed: it cannot be implemented outside the standard library.
241 /// This is so that future additional methods are not breaking changes.
242 #[unstable(feature = "windows_process_exit_code_from", issue = "none")]
243 pub trait ExitCodeExt: Sealed {
244 /// Creates a new `ExitCode` from the raw underlying `u32` return value of
247 /// The exit code should not be 259, as this conflicts with the `STILL_ACTIVE`
248 /// macro returned from the `GetExitCodeProcess` function to signal that the
249 /// process has yet to run to completion.
250 #[unstable(feature = "windows_process_exit_code_from", issue = "none")]
251 fn from_raw(raw: u32) -> Self;
254 #[unstable(feature = "windows_process_exit_code_from", issue = "none")]
255 impl ExitCodeExt for process::ExitCode {
256 fn from_raw(raw: u32) -> Self {
257 process::ExitCode::from_inner(From::from(raw))