Skip to content

Implement From<OwnedFd/Handle> for ChildStdin/out/err object #98704

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

Merged
merged 3 commits into from
Sep 28, 2023
Merged
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
42 changes: 42 additions & 0 deletions library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
@@ -434,6 +434,20 @@ impl From<crate::process::ChildStdin> for OwnedFd {
}
}

/// Create a `ChildStdin` from the provided `OwnedFd`.
///
/// The provided file descriptor must point to a pipe
/// with the `CLOEXEC` flag set.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedFd> for process::ChildStdin {
#[inline]
fn from(fd: OwnedFd) -> process::ChildStdin {
let fd = sys::fd::FileDesc::from_inner(fd);
let pipe = sys::pipe::AnonPipe::from_inner(fd);
process::ChildStdin::from_inner(pipe)
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for crate::process::ChildStdout {
#[inline]
@@ -450,6 +464,20 @@ impl From<crate::process::ChildStdout> for OwnedFd {
}
}

/// Create a `ChildStdout` from the provided `OwnedFd`.
///
/// The provided file descriptor must point to a pipe
/// with the `CLOEXEC` flag set.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedFd> for process::ChildStdout {
#[inline]
fn from(fd: OwnedFd) -> process::ChildStdout {
let fd = sys::fd::FileDesc::from_inner(fd);
let pipe = sys::pipe::AnonPipe::from_inner(fd);
process::ChildStdout::from_inner(pipe)
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl AsFd for crate::process::ChildStderr {
#[inline]
@@ -466,6 +494,20 @@ impl From<crate::process::ChildStderr> for OwnedFd {
}
}

/// Create a `ChildStderr` from the provided `OwnedFd`.
///
/// The provided file descriptor must point to a pipe
/// with the `CLOEXEC` flag set.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedFd> for process::ChildStderr {
#[inline]
fn from(fd: OwnedFd) -> process::ChildStderr {
let fd = sys::fd::FileDesc::from_inner(fd);
let pipe = sys::pipe::AnonPipe::from_inner(fd);
process::ChildStderr::from_inner(pipe)
}
}

/// Returns the OS-assigned process identifier associated with this process's parent.
#[must_use]
#[stable(feature = "unix_ppid", since = "1.27.0")]
39 changes: 39 additions & 0 deletions library/std/src/os/windows/process.rs
Original file line number Diff line number Diff line change
@@ -106,6 +106,45 @@ impl IntoRawHandle for process::ChildStderr {
}
}

/// Create a `ChildStdin` from the provided `OwnedHandle`.
///
/// The provided handle must be asynchronous, as reading and
/// writing from and to it is implemented using asynchronous APIs.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedHandle> for process::ChildStdin {
fn from(handle: OwnedHandle) -> process::ChildStdin {
let handle = sys::handle::Handle::from_inner(handle);
let pipe = sys::pipe::AnonPipe::from_inner(handle);
process::ChildStdin::from_inner(pipe)
}
}

/// Create a `ChildStdout` from the provided `OwnedHandle`.
///
/// The provided handle must be asynchronous, as reading and
/// writing from and to it is implemented using asynchronous APIs.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedHandle> for process::ChildStdout {
fn from(handle: OwnedHandle) -> process::ChildStdout {
let handle = sys::handle::Handle::from_inner(handle);
let pipe = sys::pipe::AnonPipe::from_inner(handle);
process::ChildStdout::from_inner(pipe)
}
}

/// Create a `ChildStderr` from the provided `OwnedHandle`.
///
/// The provided handle must be asynchronous, as reading and
/// writing from and to it is implemented using asynchronous APIs.
#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")]
impl From<OwnedHandle> for process::ChildStderr {
fn from(handle: OwnedHandle) -> process::ChildStderr {
let handle = sys::handle::Handle::from_inner(handle);
let pipe = sys::pipe::AnonPipe::from_inner(handle);
process::ChildStderr::from_inner(pipe)
}
}

/// Windows-specific extensions to [`process::ExitStatus`].
///
/// This trait is sealed: it cannot be implemented outside the standard library.
8 changes: 7 additions & 1 deletion library/std/src/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use crate::mem;
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::sys::fd::FileDesc;
use crate::sys::{cvt, cvt_r};
use crate::sys_common::IntoInner;
use crate::sys_common::{FromInner, IntoInner};

////////////////////////////////////////////////////////////////////////////////
// Anonymous pipes
@@ -158,3 +158,9 @@ impl FromRawFd for AnonPipe {
Self(FromRawFd::from_raw_fd(raw_fd))
}
}

impl FromInner<FileDesc> for AnonPipe {
fn from_inner(fd: FileDesc) -> Self {
Self(fd)
}
}
8 changes: 7 additions & 1 deletion library/std/src/sys/windows/pipe.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ use crate::sys::c;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::handle::Handle;
use crate::sys::hashmap_random_keys;
use crate::sys_common::IntoInner;
use crate::sys_common::{FromInner, IntoInner};

////////////////////////////////////////////////////////////////////////////////
// Anonymous pipes
@@ -28,6 +28,12 @@ impl IntoInner<Handle> for AnonPipe {
}
}

impl FromInner<Handle> for AnonPipe {
fn from_inner(inner: Handle) -> AnonPipe {
Self { inner }
}
}

pub struct Pipes {
pub ours: AnonPipe,
pub theirs: AnonPipe,