Skip to content

Commit 84c62eb

Browse files
committed
Auto merge of rust-lang#127153 - NobodyXu:pipe, r=<try>
Initial implementation of anonymous_pipe API ACP completed in rust-lang/libs-team#375 Tracking issue: rust-lang#127154 try-job: x86_64-msvc try-job: i686-mingw
2 parents 73a2281 + d55fb41 commit 84c62eb

File tree

12 files changed

+459
-3
lines changed

12 files changed

+459
-3
lines changed

library/std/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ pub use core::{
685685
module_path, option_env, stringify, trace_macros,
686686
};
687687

688+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
689+
pub use crate::sys::anonymous_pipe as pipe;
690+
688691
#[unstable(
689692
feature = "concat_bytes",
690693
issue = "87555",
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//! Module for anonymous pipe
2+
//!
3+
//! ```
4+
//! #![feature(anonymous_pipe)]
5+
//!
6+
//! # #[cfg(miri)] fn main() {}
7+
//! # #[cfg(not(miri))]
8+
//! # fn main() -> std::io::Result<()> {
9+
//! let (reader, writer) = std::pipe::pipe()?;
10+
//! # Ok(())
11+
//! # }
12+
//! ```
13+
14+
use crate::{io, sys::pipe::AnonPipe};
15+
16+
/// Create anonymous pipe that is close-on-exec and blocking.
17+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
18+
#[inline]
19+
pub fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
20+
cfg_if::cfg_if! {
21+
if #[cfg(unix)] {
22+
unix::pipe()
23+
} else if #[cfg(windows)] {
24+
windows::pipe()
25+
} else {
26+
Err(io::Error::UNSUPPORTED_PLATFORM)
27+
}
28+
}
29+
}
30+
31+
/// Read end of the anonymous pipe.
32+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
33+
#[derive(Debug)]
34+
pub struct PipeReader(AnonPipe);
35+
36+
/// Write end of the anonymous pipe.
37+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
38+
#[derive(Debug)]
39+
pub struct PipeWriter(AnonPipe);
40+
41+
impl PipeReader {
42+
/// Create a new [`PipeReader`] instance that shares the same underlying file description.
43+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
44+
pub fn try_clone(&self) -> io::Result<Self> {
45+
self.0.try_clone().map(Self)
46+
}
47+
}
48+
49+
impl PipeWriter {
50+
/// Create a new [`PipeWriter`] instance that shares the same underlying file description.
51+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
52+
pub fn try_clone(&self) -> io::Result<Self> {
53+
self.0.try_clone().map(Self)
54+
}
55+
}
56+
57+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
58+
impl io::Read for &PipeReader {
59+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
60+
self.0.read(buf)
61+
}
62+
fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
63+
self.0.read_vectored(bufs)
64+
}
65+
#[inline]
66+
fn is_read_vectored(&self) -> bool {
67+
self.0.is_read_vectored()
68+
}
69+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
70+
self.0.read_to_end(buf)
71+
}
72+
fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
73+
self.0.read_buf(buf)
74+
}
75+
}
76+
77+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
78+
impl io::Read for PipeReader {
79+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
80+
self.0.read(buf)
81+
}
82+
fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
83+
self.0.read_vectored(bufs)
84+
}
85+
#[inline]
86+
fn is_read_vectored(&self) -> bool {
87+
self.0.is_read_vectored()
88+
}
89+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
90+
self.0.read_to_end(buf)
91+
}
92+
fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
93+
self.0.read_buf(buf)
94+
}
95+
}
96+
97+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
98+
impl io::Write for &PipeWriter {
99+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
100+
self.0.write(buf)
101+
}
102+
#[inline]
103+
fn flush(&mut self) -> io::Result<()> {
104+
Ok(())
105+
}
106+
107+
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
108+
self.0.write_vectored(bufs)
109+
}
110+
111+
#[inline]
112+
fn is_write_vectored(&self) -> bool {
113+
self.0.is_write_vectored()
114+
}
115+
}
116+
117+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
118+
impl io::Write for PipeWriter {
119+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
120+
self.0.write(buf)
121+
}
122+
#[inline]
123+
fn flush(&mut self) -> io::Result<()> {
124+
Ok(())
125+
}
126+
127+
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
128+
self.0.write_vectored(bufs)
129+
}
130+
131+
#[inline]
132+
fn is_write_vectored(&self) -> bool {
133+
self.0.is_write_vectored()
134+
}
135+
}
136+
137+
#[cfg(unix)]
138+
mod unix;
139+
140+
#[cfg(windows)]
141+
mod windows;
142+
143+
#[cfg(all(test, not(miri)))]
144+
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use super::*;
2+
use crate::{
3+
io::{Read, Write},
4+
process,
5+
thread::spawn,
6+
};
7+
8+
#[test]
9+
fn pipe_creation_clone_and_rw() {
10+
let (rx, tx) = pipe().unwrap();
11+
12+
let thread = spawn({
13+
let mut tx = tx.try_clone().unwrap();
14+
move || {
15+
tx.write_all(b"12345").unwrap();
16+
drop(tx);
17+
}
18+
});
19+
drop(tx);
20+
21+
let mut rx2 = rx.try_clone().unwrap();
22+
drop(rx);
23+
24+
let mut s = String::new();
25+
rx2.read_to_string(&mut s).unwrap();
26+
drop(rx2);
27+
assert_eq!(s, "12345");
28+
29+
thread.join().unwrap();
30+
}
31+
32+
#[test]
33+
fn subprocess() {
34+
let (rx, tx) = pipe().unwrap();
35+
36+
assert!(process::Command::new("cargo").arg("--version").stdout(tx).status().unwrap().success());
37+
38+
let mut s = String::new();
39+
(&rx).read_to_string(&mut s).unwrap();
40+
drop(rx);
41+
assert!(s.starts_with("cargo 1."));
42+
}
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use super::*;
2+
3+
use crate::{
4+
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
5+
process::Stdio,
6+
sys::{
7+
fd::FileDesc,
8+
pipe::{anon_pipe, AnonPipe},
9+
},
10+
sys_common::{FromInner, IntoInner},
11+
};
12+
13+
#[inline]
14+
pub(super) fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
15+
anon_pipe().map(|(rx, tx)| (PipeReader(rx), PipeWriter(tx)))
16+
}
17+
18+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
19+
impl AsFd for PipeReader {
20+
fn as_fd(&self) -> BorrowedFd<'_> {
21+
self.0.as_fd()
22+
}
23+
}
24+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
25+
impl AsRawFd for PipeReader {
26+
fn as_raw_fd(&self) -> RawFd {
27+
self.0.as_raw_fd()
28+
}
29+
}
30+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
31+
impl From<PipeReader> for OwnedFd {
32+
fn from(pipe: PipeReader) -> Self {
33+
FileDesc::into_inner(AnonPipe::into_inner(pipe.0))
34+
}
35+
}
36+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
37+
impl FromRawFd for PipeReader {
38+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
39+
Self(AnonPipe::from_raw_fd(raw_fd))
40+
}
41+
}
42+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
43+
impl IntoRawFd for PipeReader {
44+
fn into_raw_fd(self) -> RawFd {
45+
self.0.into_raw_fd()
46+
}
47+
}
48+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
49+
impl From<PipeReader> for Stdio {
50+
fn from(pipe: PipeReader) -> Self {
51+
Self::from(OwnedFd::from(pipe))
52+
}
53+
}
54+
55+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
56+
impl AsFd for PipeWriter {
57+
fn as_fd(&self) -> BorrowedFd<'_> {
58+
self.0.as_fd()
59+
}
60+
}
61+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
62+
impl AsRawFd for PipeWriter {
63+
fn as_raw_fd(&self) -> RawFd {
64+
self.0.as_raw_fd()
65+
}
66+
}
67+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
68+
impl From<PipeWriter> for OwnedFd {
69+
fn from(pipe: PipeWriter) -> Self {
70+
FileDesc::into_inner(AnonPipe::into_inner(pipe.0))
71+
}
72+
}
73+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
74+
impl FromRawFd for PipeWriter {
75+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
76+
Self(AnonPipe::from_raw_fd(raw_fd))
77+
}
78+
}
79+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
80+
impl IntoRawFd for PipeWriter {
81+
fn into_raw_fd(self) -> RawFd {
82+
self.0.into_raw_fd()
83+
}
84+
}
85+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
86+
impl From<PipeWriter> for Stdio {
87+
fn from(pipe: PipeWriter) -> Self {
88+
Self::from(OwnedFd::from(pipe))
89+
}
90+
}
91+
92+
fn convert_to_pipe(owned_fd: OwnedFd) -> AnonPipe {
93+
AnonPipe::from_inner(FileDesc::from_inner(OwnedFd::from(owned_fd)))
94+
}
95+
96+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
97+
impl From<OwnedFd> for PipeReader {
98+
fn from(owned_fd: OwnedFd) -> Self {
99+
Self(convert_to_pipe(owned_fd))
100+
}
101+
}
102+
103+
#[unstable(feature = "anonymous_pipe", issue = "127154")]
104+
impl From<OwnedFd> for PipeWriter {
105+
fn from(owned_fd: OwnedFd) -> Self {
106+
Self(convert_to_pipe(owned_fd))
107+
}
108+
}

0 commit comments

Comments
 (0)