pub fn pipe() -> Result<(Sender, Recver)>Expand description
Creates a new pipe with the default creation settings and returns Tokio-based handles to its sending end and receiving end.
The platform-specific builders in the os module of the crate might be more helpful if extra
configuration for the pipe is needed.
§Examples
§Basic communication
In a parent process, within a Tokio runtime:
use {
interprocess::unnamed_pipe::pipe,
std::io::{prelude::*, BufReader},
};
// Create the unnamed pipe, yielding a sender and a receiver.
let (tx, rx) = pipe()?;
// Let's extract the raw handle or file descriptor of the sender. Note that `OwnedHandle` and
// `OwnedFd` both implement `From<unnamed_pipe::Sender>`.
let txh = tx.into();
// Now deliver `txh` to the child process. This may be done by starting it here with a
// command-line argument or via stdin. This works because child processes inherit handles and
// file descriptors to unnamed pipes. You can also use a different platform-specific way of
// transferring handles or file descriptors across a process boundary.
let mut buf = String::with_capacity(128);
// We'd like to receive a line, so buffer our input.
let mut rx = BufReader::new(rx);
// Receive the line from the other process.
rx.read_line(&mut buf)?;
assert_eq!(buf.trim(), "Hello from side B!");In a child process, within a Tokio runtime:
use {interprocess::unnamed_pipe, std::io::prelude::*};
// `handle` here is an `OwnedHandle` or an `OwnedFd` from the standard library. Those
// implement `FromRawHandle` and `FromRawFd` respectively. The actual value can be transferred
// via a command-line parameter since it's numerically equal to the value obtained in the
// parent process via `OwnedHandle::from()`/`OwnedFd::from()` thanks to handle inheritance.
let mut tx = unnamed_pipe::Sender::from(handle);
// Send our message to the other side.
tx.write_all(b"Hello from side B!\n")?;