Skip to content

Commit fb7f1a8

Browse files
committed
std::process (unix): Implement From<io::Stdout> etc. for imp::Stdio
This involves adding a new variant `imp::Stdio::StaticFd`. Signed-off-by: Ian Jackson <[email protected]>
1 parent f0580df commit fb7f1a8

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

library/std/src/sys/unix/process/process_common.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::sys::fd::FileDesc;
1313
use crate::sys::fs::File;
1414
use crate::sys::pipe::{self, AnonPipe};
1515
use crate::sys_common::process::{CommandEnv, CommandEnvs};
16-
use crate::sys_common::IntoInner;
16+
use crate::sys_common::{FromInner, IntoInner};
1717

1818
#[cfg(not(target_os = "fuchsia"))]
1919
use crate::sys::fs::OpenOptions;
@@ -150,6 +150,7 @@ pub enum Stdio {
150150
Null,
151151
MakePipe,
152152
Fd(FileDesc),
153+
StaticFd(BorrowedFd<'static>),
153154
}
154155

155156
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -463,6 +464,11 @@ impl Stdio {
463464
}
464465
}
465466

467+
Stdio::StaticFd(fd) => {
468+
let fd = FileDesc::from_inner(fd.try_clone_to_owned()?);
469+
Ok((ChildStdio::Owned(fd), None))
470+
}
471+
466472
Stdio::MakePipe => {
467473
let (reader, writer) = pipe::anon_pipe()?;
468474
let (ours, theirs) = if readable { (writer, reader) } else { (reader, writer) };
@@ -497,6 +503,28 @@ impl From<File> for Stdio {
497503
}
498504
}
499505

506+
impl From<io::Stdout> for Stdio {
507+
fn from(_: io::Stdout) -> Stdio {
508+
// This ought really to be is Stdio::StaticFd(input_argument.as_fd()).
509+
// But AsFd::as_fd takes its argument by reference, and yields
510+
// a bounded lifetime, so it's no use here. There is no AsStaticFd.
511+
//
512+
// Additionally AsFd is only implemented for the *locked* versions.
513+
// We don't want to lock them here. (The implications of not locking
514+
// are the same as those for process::Stdio::inherit().)
515+
//
516+
// Arguably the hypothetical AsStaticFd and AsFd<'static>
517+
// should be implemented for io::Stdout, not just for StdoutLocked.
518+
Stdio::StaticFd(unsafe { BorrowedFd::borrow_raw(libc::STDOUT_FILENO) })
519+
}
520+
}
521+
522+
impl From<io::Stderr> for Stdio {
523+
fn from(_: io::Stderr) -> Stdio {
524+
Stdio::StaticFd(unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) })
525+
}
526+
}
527+
500528
impl ChildStdio {
501529
pub fn fd(&self) -> Option<c_int> {
502530
match *self {

0 commit comments

Comments
 (0)