🔬This is a nightly-only experimental API. (
anonymous_pipe #127154)Expand description
A cross-platform anonymous pipe.
This module provides support for anonymous OS pipes, like pipe on Linux or CreatePipe on Windows.
§Behavior
A pipe is a synchronous, unidirectional data channel between two or more processes, like an
interprocess mpsc provided by the OS. In particular:
- A read on a PipeReaderblocks until the pipe is non-empty.
- A write on a PipeWriterblocks when the pipe is full.
- When all copies of a PipeWriterare closed, a read on the correspondingPipeReaderreturns EOF.
- PipeReadercan be shared, but only one process will consume the data in the pipe.
§Capacity
Pipe capacity is platform dependent. To quote the Linux man page:
Different implementations have different limits for the pipe capacity. Applications should not rely on a particular capacity: an application should be designed so that a reading process consumes data as soon as it is available, so that a writing process does not remain blocked.
§Examples
#![feature(anonymous_pipe)]
let (ping_rx, mut ping_tx) = std::pipe::pipe()?;
let (mut pong_rx, pong_tx) = std::pipe::pipe()?;
// Spawn a process that echoes its input.
let mut echo_server = Command::new("cat").stdin(ping_rx).stdout(pong_tx).spawn()?;
ping_tx.write_all(b"hello")?;
// Close to unblock echo_server's reader.
drop(ping_tx);
let mut buf = String::new();
// Block until echo_server's writer is closed.
pong_rx.read_to_string(&mut buf)?;
assert_eq!(&buf, "hello");
echo_server.wait()?;Structs§
- PipeReader Experimental Read end of the anonymous pipe.
- PipeWriter Experimental Write end of the anonymous pipe.
Functions§
- pipeExperimental Create anonymous pipe that is close-on-exec and blocking.