From 21ad43049bf1f03469fb1e00447c91cdf15d1e98 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 5 Aug 2026 09:12:36 +0100 Subject: [PATCH] filedescriptor: Add `into_stdio` conversion (#7789) --- filedescriptor/src/lib.rs | 12 ++++++++++++ filedescriptor/src/unix.rs | 6 ++++++ filedescriptor/src/windows.rs | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/filedescriptor/src/lib.rs b/filedescriptor/src/lib.rs index 2cdc2ed17..c050c48e4 100644 --- a/filedescriptor/src/lib.rs +++ b/filedescriptor/src/lib.rs @@ -304,6 +304,12 @@ impl FileDescriptor { self.as_stdio_impl() } + /// A convenience method for creating a `std::process::Stdio` object, + /// transferring ownership of the underlying handle to the returned `Stdio`. + pub fn into_stdio(self) -> std::process::Stdio { + self.into_stdio_impl() + } + /// A convenience method for creating a `std::fs::File` object. /// The `File` is created using a duplicated handle so /// that the source handle remains alive. @@ -331,6 +337,12 @@ impl FileDescriptor { } } +impl From for std::process::Stdio { + fn from(value: FileDescriptor) -> Self { + value.into_stdio() + } +} + /// Represents the readable and writable ends of a pair of descriptors /// connected via a kernel pipe. /// diff --git a/filedescriptor/src/unix.rs b/filedescriptor/src/unix.rs index 76726e7c6..ece9927ca 100644 --- a/filedescriptor/src/unix.rs +++ b/filedescriptor/src/unix.rs @@ -265,6 +265,12 @@ impl FileDescriptor { Ok(stdio) } + #[inline] + pub(crate) fn into_stdio_impl(self) -> std::process::Stdio { + let fd = self.into_raw_fd(); + unsafe { std::process::Stdio::from_raw_fd(fd) } + } + #[inline] pub(crate) fn as_file_impl(&self) -> Result { let duped = OwnedHandle::dup(self)?; diff --git a/filedescriptor/src/windows.rs b/filedescriptor/src/windows.rs index 4502d09c7..6b43f7f3c 100644 --- a/filedescriptor/src/windows.rs +++ b/filedescriptor/src/windows.rs @@ -233,6 +233,12 @@ impl FileDescriptor { Ok(stdio) } + #[inline] + pub(crate) fn into_stdio_impl(self) -> std::process::Stdio { + let handle = self.into_raw_handle(); + unsafe { std::process::Stdio::from_raw_handle(handle) } + } + #[inline] pub(crate) fn as_file_impl(&self) -> Result { let duped = self.handle.try_clone()?;