Skip to content

Commit d047e8b

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 + 34de465 commit d047e8b

File tree

14 files changed

+521
-15
lines changed

14 files changed

+521
-15
lines changed

library/std/Cargo.toml

+44-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ core = { path = "../core", public = true }
2020
compiler_builtins = { version = "0.1.105" }
2121
profiler_builtins = { path = "../profiler_builtins", optional = true }
2222
unwind = { path = "../unwind" }
23-
hashbrown = { version = "0.14", default-features = false, features = ['rustc-dep-of-std'] }
24-
std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = ['rustc-dep-of-std'] }
23+
hashbrown = { version = "0.14", default-features = false, features = [
24+
'rustc-dep-of-std',
25+
] }
26+
std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = [
27+
'rustc-dep-of-std',
28+
] }
2529

2630
# Dependencies of the `backtrace` crate
2731
rustc-demangle = { version = "0.1.24", features = ['rustc-dep-of-std'] }
@@ -31,13 +35,27 @@ miniz_oxide = { version = "0.7.0", optional = true, default-features = false }
3135
addr2line = { version = "0.22.0", optional = true, default-features = false }
3236

3337
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
34-
libc = { version = "0.2.153", default-features = false, features = ['rustc-dep-of-std'], public = true }
38+
libc = { version = "0.2.153", default-features = false, features = [
39+
'rustc-dep-of-std',
40+
], public = true }
3541

3642
[target.'cfg(all(not(target_os = "aix"), not(all(windows, target_env = "msvc", not(target_vendor = "uwp")))))'.dependencies]
37-
object = { version = "0.36.0", default-features = false, optional = true, features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive'] }
43+
object = { version = "0.36.0", default-features = false, optional = true, features = [
44+
'read_core',
45+
'elf',
46+
'macho',
47+
'pe',
48+
'unaligned',
49+
'archive',
50+
] }
3851

3952
[target.'cfg(target_os = "aix")'.dependencies]
40-
object = { version = "0.36.0", default-features = false, optional = true, features = ['read_core', 'xcoff', 'unaligned', 'archive'] }
53+
object = { version = "0.36.0", default-features = false, optional = true, features = [
54+
'read_core',
55+
'xcoff',
56+
'unaligned',
57+
'archive',
58+
] }
4159

4260
[dev-dependencies]
4361
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
@@ -47,23 +65,29 @@ rand_xorshift = "0.3.0"
4765
dlmalloc = { version = "0.2.4", features = ['rustc-dep-of-std'] }
4866

4967
[target.x86_64-fortanix-unknown-sgx.dependencies]
50-
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public = true }
68+
fortanix-sgx-abi = { version = "0.5.0", features = [
69+
'rustc-dep-of-std',
70+
], public = true }
5171

5272
[target.'cfg(target_os = "hermit")'.dependencies]
53-
hermit-abi = { version = "0.4.0", features = ['rustc-dep-of-std'], public = true }
73+
hermit-abi = { version = "0.4.0", features = [
74+
'rustc-dep-of-std',
75+
], public = true }
5476

5577
[target.'cfg(target_os = "wasi")'.dependencies]
56-
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }
78+
wasi = { version = "0.11.0", features = [
79+
'rustc-dep-of-std',
80+
], default-features = false }
5781

5882
[target.'cfg(target_os = "uefi")'.dependencies]
5983
r-efi = { version = "4.5.0", features = ['rustc-dep-of-std'] }
6084
r-efi-alloc = { version = "1.0.0", features = ['rustc-dep-of-std'] }
6185

6286
[features]
6387
backtrace = [
64-
'addr2line/rustc-dep-of-std',
65-
'object/rustc-dep-of-std',
66-
'miniz_oxide/rustc-dep-of-std',
88+
'addr2line/rustc-dep-of-std',
89+
'object/rustc-dep-of-std',
90+
'miniz_oxide/rustc-dep-of-std',
6791
]
6892

6993
panic-unwind = ["panic_unwind"]
@@ -77,7 +101,10 @@ llvm-libunwind = ["unwind/llvm-libunwind"]
77101
system-llvm-libunwind = ["unwind/system-llvm-libunwind"]
78102

79103
# Make panics and failed asserts immediately abort without formatting any message
80-
panic_immediate_abort = ["core/panic_immediate_abort", "alloc/panic_immediate_abort"]
104+
panic_immediate_abort = [
105+
"core/panic_immediate_abort",
106+
"alloc/panic_immediate_abort",
107+
]
81108
# Choose algorithms that are optimized for binary size instead of runtime performance
82109
optimize_for_size = ["core/optimize_for_size", "alloc/optimize_for_size"]
83110

@@ -97,6 +124,11 @@ threads = 125
97124
# Maximum heap size
98125
heap_size = 0x8000000
99126

127+
[[test]]
128+
name = "pipe-subprocess"
129+
path = "tests/pipe_subprocess.rs"
130+
harness = false
131+
100132
[[bench]]
101133
name = "stdbenches"
102134
path = "benches/lib.rs"

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,29 @@
1+
use super::*;
2+
use crate::{
3+
io::{Read, Write},
4+
thread::spawn,
5+
};
6+
7+
#[test]
8+
fn pipe_creation_clone_and_rw() {
9+
let (rx, tx) = pipe().unwrap();
10+
11+
let thread = spawn({
12+
let mut tx = tx.try_clone().unwrap();
13+
move || {
14+
tx.write_all(b"12345").unwrap();
15+
drop(tx);
16+
}
17+
});
18+
drop(tx);
19+
20+
let mut rx2 = rx.try_clone().unwrap();
21+
drop(rx);
22+
23+
let mut s = String::new();
24+
rx2.read_to_string(&mut s).unwrap();
25+
drop(rx2);
26+
assert_eq!(s, "12345");
27+
28+
thread.join().unwrap();
29+
}
+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)