Skip to content

Commit 7ebbdc6

Browse files
committed
add FromOwnedFd/FromOwnedHandle for ChildStdin/out/err
1 parent 3266c36 commit 7ebbdc6

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

Diff for: library/std/src/os/unix/process.rs

+30
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ impl From<crate::process::ChildStdin> for OwnedFd {
434434
}
435435
}
436436

437+
#[stable(feature = "io_safety", since = "1.63.0")]
438+
impl From<OwnedFd> for process::ChildStdin {
439+
#[inline]
440+
fn from(fd: OwnedFd) -> process::ChildStdin {
441+
let fd = sys::fd::FileDesc::from_inner(fd);
442+
let pipe = sys::pipe::AnonPipe::from_inner(fd);
443+
process::ChildStdin::from_inner(pipe)
444+
}
445+
}
446+
437447
#[stable(feature = "io_safety", since = "1.63.0")]
438448
impl AsFd for crate::process::ChildStdout {
439449
#[inline]
@@ -450,6 +460,16 @@ impl From<crate::process::ChildStdout> for OwnedFd {
450460
}
451461
}
452462

463+
#[stable(feature = "io_safety", since = "1.63.0")]
464+
impl From<OwnedFd> for process::ChildStdout {
465+
#[inline]
466+
fn from(fd: OwnedFd) -> process::ChildStdout {
467+
let fd = sys::fd::FileDesc::from_inner(fd);
468+
let pipe = sys::pipe::AnonPipe::from_inner(fd);
469+
process::ChildStdout::from_inner(pipe)
470+
}
471+
}
472+
453473
#[stable(feature = "io_safety", since = "1.63.0")]
454474
impl AsFd for crate::process::ChildStderr {
455475
#[inline]
@@ -466,6 +486,16 @@ impl From<crate::process::ChildStderr> for OwnedFd {
466486
}
467487
}
468488

489+
#[stable(feature = "io_safety", since = "1.63.0")]
490+
impl From<OwnedFd> for process::ChildStderr {
491+
#[inline]
492+
fn from(fd: OwnedFd) -> process::ChildStderr {
493+
let fd = sys::fd::FileDesc::from_inner(fd);
494+
let pipe = sys::pipe::AnonPipe::from_inner(fd);
495+
process::ChildStderr::from_inner(pipe)
496+
}
497+
}
498+
469499
/// Returns the OS-assigned process identifier associated with this process's parent.
470500
#[must_use]
471501
#[stable(feature = "unix_ppid", since = "1.27.0")]

Diff for: library/std/src/os/windows/process.rs

+27
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ impl IntoRawHandle for process::ChildStderr {
106106
}
107107
}
108108

109+
#[stable(feature = "io_safety", since = "1.63.0")]
110+
impl From<OwnedHandle> for process::ChildStdin {
111+
fn from(handle: OwnedHandle) -> process::ChildStdin {
112+
let handle = sys::handle::Handle::from_inner(handle);
113+
let pipe = sys::pipe::AnonPipe::from_inner(handle);
114+
process::ChildStdin::from_inner(pipe)
115+
}
116+
}
117+
118+
#[stable(feature = "io_safety", since = "1.63.0")]
119+
impl From<OwnedHandle> for process::ChildStdout {
120+
fn from(handle: OwnedHandle) -> process::ChildStdout {
121+
let handle = sys::handle::Handle::from_inner(handle);
122+
let pipe = sys::pipe::AnonPipe::from_inner(handle);
123+
process::ChildStdout::from_inner(pipe)
124+
}
125+
}
126+
127+
#[stable(feature = "io_safety", since = "1.63.0")]
128+
impl From<OwnedHandle> for process::ChildStderr {
129+
fn from(handle: OwnedHandle) -> process::ChildStderr {
130+
let handle = sys::handle::Handle::from_inner(handle);
131+
let pipe = sys::pipe::AnonPipe::from_inner(handle);
132+
process::ChildStderr::from_inner(pipe)
133+
}
134+
}
135+
109136
/// Windows-specific extensions to [`process::ExitStatus`].
110137
///
111138
/// This trait is sealed: it cannot be implemented outside the standard library.

Diff for: library/std/src/sys/unix/pipe.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::mem;
33
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
44
use crate::sys::fd::FileDesc;
55
use crate::sys::{cvt, cvt_r};
6-
use crate::sys_common::IntoInner;
6+
use crate::sys_common::{FromInner, IntoInner};
77

88
////////////////////////////////////////////////////////////////////////////////
99
// Anonymous pipes
@@ -158,3 +158,9 @@ impl FromRawFd for AnonPipe {
158158
Self(FromRawFd::from_raw_fd(raw_fd))
159159
}
160160
}
161+
162+
impl FromInner<FileDesc> for AnonPipe {
163+
fn from_inner(fd: FileDesc) -> Self {
164+
Self(fd)
165+
}
166+
}

Diff for: library/std/src/sys/windows/pipe.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::sys::c;
1212
use crate::sys::fs::{File, OpenOptions};
1313
use crate::sys::handle::Handle;
1414
use crate::sys::hashmap_random_keys;
15-
use crate::sys_common::IntoInner;
15+
use crate::sys_common::{FromInner, IntoInner};
1616

1717
////////////////////////////////////////////////////////////////////////////////
1818
// Anonymous pipes
@@ -28,6 +28,12 @@ impl IntoInner<Handle> for AnonPipe {
2828
}
2929
}
3030

31+
impl FromInner<Handle> for AnonPipe {
32+
fn from_inner(inner: Handle) -> AnonPipe {
33+
Self { inner }
34+
}
35+
}
36+
3137
pub struct Pipes {
3238
pub ours: AnonPipe,
3339
pub theirs: AnonPipe,

0 commit comments

Comments
 (0)