Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add windows::CommandExt::raw_arg on Rust 1.62+ #32

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ fn main() {
}
};

if !cfg.probe_rustc_version(1, 62) {
autocfg::emit("async_process_no_windows_raw_arg");
}
if !cfg.probe_rustc_version(1, 63) {
autocfg::emit("async_process_no_io_safety");
}
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub mod unix;
#[cfg(windows)]
pub mod windows;

mod sealed {
pub trait Sealed {}
}

/// An event delivered every time the SIGCHLD signal occurs.
static SIGCHLD: Event = Event::new();

Expand Down Expand Up @@ -516,13 +520,15 @@ impl AsRawFd for ChildStdin {
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl AsFd for ChildStdin {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl TryFrom<ChildStdin> for OwnedFd {
type Error = io::Error;
Expand Down Expand Up @@ -600,13 +606,15 @@ impl AsRawFd for ChildStdout {
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl AsFd for ChildStdout {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl TryFrom<ChildStdout> for OwnedFd {
type Error = io::Error;
Expand Down Expand Up @@ -673,13 +681,15 @@ impl AsRawFd for ChildStderr {
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl AsFd for ChildStderr {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

/// **Note:** This implementation is only available on Rust 1.63+.
#[cfg(all(not(async_process_no_io_safety), unix))]
impl TryFrom<ChildStderr> for OwnedFd {
type Error = io::Error;
Expand Down
6 changes: 5 additions & 1 deletion src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::os::unix::process::CommandExt as _;
use crate::Command;

/// Unix-specific extensions to the [`Command`] builder.
pub trait CommandExt {
///
/// This trait is sealed: it cannot be implemented outside `async-process`.
/// This is so that future additional methods are not breaking changes.
pub trait CommandExt: crate::sealed::Sealed {
/// Sets the child process's user ID. This translates to a
/// `setuid` call in the child process. Failure in the `setuid`
/// call will cause the spawn to fail.
Expand Down Expand Up @@ -88,6 +91,7 @@ pub trait CommandExt {
S: AsRef<OsStr>;
}

impl crate::sealed::Sealed for Command {}
impl CommandExt for Command {
fn uid(&mut self, id: u32) -> &mut Command {
self.inner.uid(id);
Expand Down
23 changes: 22 additions & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
//! Windows-specific extensions.

#[cfg(not(async_process_no_windows_raw_arg))]
use std::ffi::OsStr;
use std::os::windows::io::{AsRawHandle, RawHandle};
use std::os::windows::process::CommandExt as _;

use crate::{Child, Command};

/// Windows-specific extensions to the [`Command`] builder.
pub trait CommandExt {
///
/// This trait is sealed: it cannot be implemented outside `async-process`.
/// This is so that future additional methods are not breaking changes.
pub trait CommandExt: crate::sealed::Sealed {
/// Sets the [process creation flags][1] to be passed to `CreateProcess`.
///
/// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.
///
/// [1]: https://docs.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
fn creation_flags(&mut self, flags: u32) -> &mut Command;

/// Append literal text to the command line without any quoting or escaping.
///
/// This is useful for passing arguments to `cmd.exe /c`, which doesn't follow
/// `CommandLineToArgvW` escaping rules.
///
/// **Note:** This method is only available on Rust 1.62+.
#[cfg(not(async_process_no_windows_raw_arg))]
fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut Command;
}

impl crate::sealed::Sealed for Command {}
impl CommandExt for Command {
fn creation_flags(&mut self, flags: u32) -> &mut Command {
self.inner.creation_flags(flags);
self
}

#[cfg(not(async_process_no_windows_raw_arg))]
fn raw_arg<S: AsRef<OsStr>>(&mut self, text_to_append_as_is: S) -> &mut Command {
self.inner.raw_arg(text_to_append_as_is);
self
}
}

impl AsRawHandle for Child {
Expand Down