|
1 | | -//! A cross-platform anonymous pipe. |
2 | | -//! |
3 | | -//! This module provides support for anonymous OS pipes, like [pipe] on Linux or [CreatePipe] on |
4 | | -//! Windows. |
5 | | -//! |
6 | | -//! # Behavior |
7 | | -//! |
8 | | -//! A pipe is a synchronous, unidirectional data channel between two or more processes, like an |
9 | | -//! interprocess [`mpsc`](crate::sync::mpsc) provided by the OS. In particular: |
10 | | -//! |
11 | | -//! * A read on a [`PipeReader`] blocks until the pipe is non-empty. |
12 | | -//! * A write on a [`PipeWriter`] blocks when the pipe is full. |
13 | | -//! * When all copies of a [`PipeWriter`] are closed, a read on the corresponding [`PipeReader`] |
14 | | -//! returns EOF. |
15 | | -//! * [`PipeReader`] can be shared, but only one process will consume the data in the pipe. |
16 | | -//! |
17 | | -//! # Capacity |
18 | | -//! |
19 | | -//! Pipe capacity is platform dependent. To quote the Linux [man page]: |
20 | | -//! |
21 | | -//! > Different implementations have different limits for the pipe capacity. Applications should |
22 | | -//! > not rely on a particular capacity: an application should be designed so that a reading process |
23 | | -//! > consumes data as soon as it is available, so that a writing process does not remain blocked. |
24 | | -//! |
25 | | -//! # Examples |
26 | | -//! |
27 | | -//! ```no_run |
28 | | -//! #![feature(anonymous_pipe)] |
29 | | -//! # #[cfg(miri)] fn main() {} |
30 | | -//! # #[cfg(not(miri))] |
31 | | -//! # fn main() -> std::io::Result<()> { |
32 | | -//! # use std::process::Command; |
33 | | -//! # use std::io::{Read, Write}; |
34 | | -//! let (ping_rx, mut ping_tx) = std::pipe::pipe()?; |
35 | | -//! let (mut pong_rx, pong_tx) = std::pipe::pipe()?; |
36 | | -//! |
37 | | -//! // Spawn a process that echoes its input. |
38 | | -//! let mut echo_server = Command::new("cat").stdin(ping_rx).stdout(pong_tx).spawn()?; |
39 | | -//! |
40 | | -//! ping_tx.write_all(b"hello")?; |
41 | | -//! // Close to unblock echo_server's reader. |
42 | | -//! drop(ping_tx); |
43 | | -//! |
44 | | -//! let mut buf = String::new(); |
45 | | -//! // Block until echo_server's writer is closed. |
46 | | -//! pong_rx.read_to_string(&mut buf)?; |
47 | | -//! assert_eq!(&buf, "hello"); |
48 | | -//! |
49 | | -//! echo_server.wait()?; |
50 | | -//! # Ok(()) |
51 | | -//! # } |
52 | | -//! ``` |
53 | | -//! [pipe]: https://man7.org/linux/man-pages/man2/pipe.2.html |
54 | | -//! [CreatePipe]: https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createpipe |
55 | | -//! [man page]: https://man7.org/linux/man-pages/man7/pipe.7.html |
56 | 1 | use crate::io; |
57 | 2 | use crate::sys::anonymous_pipe::{AnonPipe, pipe as pipe_inner}; |
58 | 3 |
|
59 | 4 | /// Create anonymous pipe that is close-on-exec and blocking. |
60 | 5 | /// |
| 6 | +/// This function provides support for anonymous OS pipes, like [pipe] on Linux or [CreatePipe] on |
| 7 | +/// Windows. |
| 8 | +/// |
| 9 | +/// # Behavior |
| 10 | +/// |
| 11 | +/// A pipe is a synchronous, unidirectional data channel between two or more processes, like an |
| 12 | +/// interprocess [`mpsc`](crate::sync::mpsc) provided by the OS. In particular: |
| 13 | +/// |
| 14 | +/// * A read on a [`PipeReader`] blocks until the pipe is non-empty. |
| 15 | +/// * A write on a [`PipeWriter`] blocks when the pipe is full. |
| 16 | +/// * When all copies of a [`PipeWriter`] are closed, a read on the corresponding [`PipeReader`] |
| 17 | +/// returns EOF. |
| 18 | +/// * [`PipeReader`] can be shared, but only one process will consume the data in the pipe. |
| 19 | +/// |
| 20 | +/// # Capacity |
| 21 | +/// |
| 22 | +/// Pipe capacity is platform dependent. To quote the Linux [man page]: |
| 23 | +/// |
| 24 | +/// > Different implementations have different limits for the pipe capacity. Applications should |
| 25 | +/// > not rely on a particular capacity: an application should be designed so that a reading process |
| 26 | +/// > consumes data as soon as it is available, so that a writing process does not remain blocked. |
| 27 | +/// |
61 | 28 | /// # Examples |
62 | 29 | /// |
63 | | -/// See the [module-level](crate::pipe) documentation for examples. |
| 30 | +/// ```no_run |
| 31 | +/// #![feature(anonymous_pipe)] |
| 32 | +/// # #[cfg(miri)] fn main() {} |
| 33 | +/// # #[cfg(not(miri))] |
| 34 | +/// # fn main() -> std::io::Result<()> { |
| 35 | +/// # use std::process::Command; |
| 36 | +/// # use std::io::{Read, Write}; |
| 37 | +/// let (ping_rx, mut ping_tx) = std::pipe::pipe()?; |
| 38 | +/// let (mut pong_rx, pong_tx) = std::pipe::pipe()?; |
| 39 | +/// |
| 40 | +/// // Spawn a process that echoes its input. |
| 41 | +/// let mut echo_server = Command::new("cat").stdin(ping_rx).stdout(pong_tx).spawn()?; |
| 42 | +/// |
| 43 | +/// ping_tx.write_all(b"hello")?; |
| 44 | +/// // Close to unblock echo_server's reader. |
| 45 | +/// drop(ping_tx); |
| 46 | +/// |
| 47 | +/// let mut buf = String::new(); |
| 48 | +/// // Block until echo_server's writer is closed. |
| 49 | +/// pong_rx.read_to_string(&mut buf)?; |
| 50 | +/// assert_eq!(&buf, "hello"); |
| 51 | +/// |
| 52 | +/// echo_server.wait()?; |
| 53 | +/// # Ok(()) |
| 54 | +/// # } |
| 55 | +/// ``` |
| 56 | +/// [pipe]: https://man7.org/linux/man-pages/man2/pipe.2.html |
| 57 | +/// [CreatePipe]: https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createpipe |
| 58 | +/// [man page]: https://man7.org/linux/man-pages/man7/pipe.7.html |
64 | 59 | #[unstable(feature = "anonymous_pipe", issue = "127154")] |
65 | 60 | #[inline] |
66 | 61 | pub fn pipe() -> io::Result<(PipeReader, PipeWriter)> { |
|
0 commit comments