std/sys/anonymous_pipe/
unix.rs

1use crate::io::{self, PipeReader, PipeWriter};
2use crate::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
3use crate::process::Stdio;
4use crate::sys::fd::FileDesc;
5use crate::sys::pipe::anon_pipe;
6use crate::sys_common::{FromInner, IntoInner};
7
8pub type AnonPipe = FileDesc;
9
10#[inline]
11pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
12    anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner()))
13}
14
15#[unstable(feature = "anonymous_pipe", issue = "127154")]
16impl AsFd for PipeReader {
17    fn as_fd(&self) -> BorrowedFd<'_> {
18        self.0.as_fd()
19    }
20}
21#[unstable(feature = "anonymous_pipe", issue = "127154")]
22impl AsRawFd for PipeReader {
23    fn as_raw_fd(&self) -> RawFd {
24        self.0.as_raw_fd()
25    }
26}
27#[unstable(feature = "anonymous_pipe", issue = "127154")]
28impl From<PipeReader> for OwnedFd {
29    fn from(pipe: PipeReader) -> Self {
30        FileDesc::into_inner(pipe.0)
31    }
32}
33#[unstable(feature = "anonymous_pipe", issue = "127154")]
34impl FromRawFd for PipeReader {
35    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
36        unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
37    }
38}
39#[unstable(feature = "anonymous_pipe", issue = "127154")]
40impl IntoRawFd for PipeReader {
41    fn into_raw_fd(self) -> RawFd {
42        self.0.into_raw_fd()
43    }
44}
45#[unstable(feature = "anonymous_pipe", issue = "127154")]
46impl From<PipeReader> for Stdio {
47    fn from(pipe: PipeReader) -> Self {
48        Self::from(OwnedFd::from(pipe))
49    }
50}
51
52#[unstable(feature = "anonymous_pipe", issue = "127154")]
53impl AsFd for PipeWriter {
54    fn as_fd(&self) -> BorrowedFd<'_> {
55        self.0.as_fd()
56    }
57}
58#[unstable(feature = "anonymous_pipe", issue = "127154")]
59impl AsRawFd for PipeWriter {
60    fn as_raw_fd(&self) -> RawFd {
61        self.0.as_raw_fd()
62    }
63}
64#[unstable(feature = "anonymous_pipe", issue = "127154")]
65impl From<PipeWriter> for OwnedFd {
66    fn from(pipe: PipeWriter) -> Self {
67        FileDesc::into_inner(pipe.0)
68    }
69}
70#[unstable(feature = "anonymous_pipe", issue = "127154")]
71impl FromRawFd for PipeWriter {
72    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
73        unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
74    }
75}
76#[unstable(feature = "anonymous_pipe", issue = "127154")]
77impl IntoRawFd for PipeWriter {
78    fn into_raw_fd(self) -> RawFd {
79        self.0.into_raw_fd()
80    }
81}
82#[unstable(feature = "anonymous_pipe", issue = "127154")]
83impl From<PipeWriter> for Stdio {
84    fn from(pipe: PipeWriter) -> Self {
85        Self::from(OwnedFd::from(pipe))
86    }
87}
88
89#[unstable(feature = "anonymous_pipe", issue = "127154")]
90impl From<OwnedFd> for PipeReader {
91    fn from(owned_fd: OwnedFd) -> Self {
92        Self(FileDesc::from_inner(owned_fd))
93    }
94}
95
96#[unstable(feature = "anonymous_pipe", issue = "127154")]
97impl From<OwnedFd> for PipeWriter {
98    fn from(owned_fd: OwnedFd) -> Self {
99        Self(FileDesc::from_inner(owned_fd))
100    }
101}