From 7ebbdc6e3b8c8cec8dc78c20f961c4fae415a5a0 Mon Sep 17 00:00:00 2001 From: Vincent Thiberville Date: Wed, 6 Jul 2022 15:29:17 +0200 Subject: [PATCH 1/3] add FromOwnedFd/FromOwnedHandle for ChildStdin/out/err --- library/std/src/os/unix/process.rs | 30 +++++++++++++++++++++++++++ library/std/src/os/windows/process.rs | 27 ++++++++++++++++++++++++ library/std/src/sys/unix/pipe.rs | 8 ++++++- library/std/src/sys/windows/pipe.rs | 8 ++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/library/std/src/os/unix/process.rs b/library/std/src/os/unix/process.rs index 729c63d184f2c..5c1841016f41f 100644 --- a/library/std/src/os/unix/process.rs +++ b/library/std/src/os/unix/process.rs @@ -434,6 +434,16 @@ impl From for OwnedFd { } } +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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 +460,16 @@ impl From for OwnedFd { } } +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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 +486,16 @@ impl From for OwnedFd { } } +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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")] diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs index 073168cf2d209..719b804ac199b 100644 --- a/library/std/src/os/windows/process.rs +++ b/library/std/src/os/windows/process.rs @@ -106,6 +106,33 @@ impl IntoRawHandle for process::ChildStderr { } } +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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) + } +} + +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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) + } +} + +#[stable(feature = "io_safety", since = "1.63.0")] +impl From 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. diff --git a/library/std/src/sys/unix/pipe.rs b/library/std/src/sys/unix/pipe.rs index 938a46bfdd833..a9c783b46237e 100644 --- a/library/std/src/sys/unix/pipe.rs +++ b/library/std/src/sys/unix/pipe.rs @@ -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 for AnonPipe { + fn from_inner(fd: FileDesc) -> Self { + Self(fd) + } +} diff --git a/library/std/src/sys/windows/pipe.rs b/library/std/src/sys/windows/pipe.rs index d07147eccc1d3..7624e746f5c89 100644 --- a/library/std/src/sys/windows/pipe.rs +++ b/library/std/src/sys/windows/pipe.rs @@ -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 for AnonPipe { } } +impl FromInner for AnonPipe { + fn from_inner(inner: Handle) -> AnonPipe { + Self { inner } + } +} + pub struct Pipes { pub ours: AnonPipe, pub theirs: AnonPipe, From 9de32a7a3b7928b3679118cf423fd69246de8784 Mon Sep 17 00:00:00 2001 From: Vincent Thiberville Date: Thu, 9 Mar 2023 10:46:38 +0100 Subject: [PATCH 2/3] add doc on From impl for ChildStd* --- library/std/src/os/unix/process.rs | 12 ++++++++++++ library/std/src/os/windows/process.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/library/std/src/os/unix/process.rs b/library/std/src/os/unix/process.rs index 5c1841016f41f..95f10bcb04e3f 100644 --- a/library/std/src/os/unix/process.rs +++ b/library/std/src/os/unix/process.rs @@ -434,6 +434,10 @@ impl From 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStdin { #[inline] @@ -460,6 +464,10 @@ impl From 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStdout { #[inline] @@ -486,6 +494,10 @@ impl From 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStderr { #[inline] diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs index 719b804ac199b..2f989a77f14ec 100644 --- a/library/std/src/os/windows/process.rs +++ b/library/std/src/os/windows/process.rs @@ -106,6 +106,10 @@ 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStdin { fn from(handle: OwnedHandle) -> process::ChildStdin { @@ -115,6 +119,10 @@ impl From for process::ChildStdin { } } +/// 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStdout { fn from(handle: OwnedHandle) -> process::ChildStdout { @@ -124,6 +132,10 @@ impl From for process::ChildStdout { } } +/// 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 = "io_safety", since = "1.63.0")] impl From for process::ChildStderr { fn from(handle: OwnedHandle) -> process::ChildStderr { From 9bdf9e754e82cadfacdb081ef2b0a0fe8df9be25 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 28 Sep 2023 00:13:02 -0700 Subject: [PATCH 3/3] Update stability attribute for child stream From impls --- library/std/src/os/unix/process.rs | 6 +++--- library/std/src/os/windows/process.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/std/src/os/unix/process.rs b/library/std/src/os/unix/process.rs index 95f10bcb04e3f..80eae4a63920a 100644 --- a/library/std/src/os/unix/process.rs +++ b/library/std/src/os/unix/process.rs @@ -438,7 +438,7 @@ impl From for OwnedFd { /// /// The provided file descriptor must point to a pipe /// with the `CLOEXEC` flag set. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStdin { #[inline] fn from(fd: OwnedFd) -> process::ChildStdin { @@ -468,7 +468,7 @@ impl From for OwnedFd { /// /// The provided file descriptor must point to a pipe /// with the `CLOEXEC` flag set. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStdout { #[inline] fn from(fd: OwnedFd) -> process::ChildStdout { @@ -498,7 +498,7 @@ impl From for OwnedFd { /// /// The provided file descriptor must point to a pipe /// with the `CLOEXEC` flag set. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStderr { #[inline] fn from(fd: OwnedFd) -> process::ChildStderr { diff --git a/library/std/src/os/windows/process.rs b/library/std/src/os/windows/process.rs index 2f989a77f14ec..875b531c5cae2 100644 --- a/library/std/src/os/windows/process.rs +++ b/library/std/src/os/windows/process.rs @@ -110,7 +110,7 @@ impl IntoRawHandle for process::ChildStderr { /// /// The provided handle must be asynchronous, as reading and /// writing from and to it is implemented using asynchronous APIs. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStdin { fn from(handle: OwnedHandle) -> process::ChildStdin { let handle = sys::handle::Handle::from_inner(handle); @@ -123,7 +123,7 @@ impl From for process::ChildStdin { /// /// The provided handle must be asynchronous, as reading and /// writing from and to it is implemented using asynchronous APIs. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStdout { fn from(handle: OwnedHandle) -> process::ChildStdout { let handle = sys::handle::Handle::from_inner(handle); @@ -136,7 +136,7 @@ impl From for process::ChildStdout { /// /// The provided handle must be asynchronous, as reading and /// writing from and to it is implemented using asynchronous APIs. -#[stable(feature = "io_safety", since = "1.63.0")] +#[stable(feature = "child_stream_from_fd", since = "CURRENT_RUSTC_VERSION")] impl From for process::ChildStderr { fn from(handle: OwnedHandle) -> process::ChildStderr { let handle = sys::handle::Handle::from_inner(handle);