async_process/
windows.rs

1//! Windows-specific extensions.
2
3use std::ffi::OsStr;
4use std::os::windows::io::{AsRawHandle, RawHandle};
5use std::os::windows::process::CommandExt as _;
6
7use crate::{Child, Command};
8
9/// Windows-specific extensions to the [`Command`] builder.
10///
11/// This trait is sealed: it cannot be implemented outside `async-process`.
12/// This is so that future additional methods are not breaking changes.
13pub trait CommandExt: crate::sealed::Sealed {
14    /// Sets the [process creation flags][1] to be passed to `CreateProcess`.
15    ///
16    /// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
17    ///
18    /// [1]: https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
19    fn creation_flags(&mut self, flags: u32) -> &mut Command;
20
21    /// Append literal text to the command line without any quoting or escaping.
22    ///
23    /// This is useful for passing arguments to `cmd.exe /c`, which doesn't follow
24    /// `CommandLineToArgvW` escaping rules.
25    fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut Command;
26}
27
28impl crate::sealed::Sealed for Command {}
29impl CommandExt for Command {
30    fn creation_flags(&mut self, flags: u32) -> &mut Command {
31        self.inner.creation_flags(flags);
32        self
33    }
34
35    fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut Command {
36        self.inner.raw_arg(text_to_append_as_is);
37        self
38    }
39}
40
41impl AsRawHandle for Child {
42    fn as_raw_handle(&self) -> RawHandle {
43        self.child.lock().unwrap().get_mut().as_raw_handle()
44    }
45}