Function tokio::net::unix::pipe::pipe

source ·
pub fn pipe() -> Result<(Sender, Receiver)>
Expand description

Creates a new anonymous Unix pipe.

This function will open a new pipe and associate both pipe ends with the default event loop.

If you need to create a pipe for communication with a spawned process, you can use Stdio::piped() instead.

§Errors

If creating a pipe fails, this function will return with the related OS error.

§Examples

Create a pipe and pass the writing end to a spawned process.

use tokio::net::unix::pipe;
use tokio::process::Command;

let (tx, mut rx) = pipe::pipe()?;
let mut buffer = String::new();

let status = Command::new("echo")
    .arg("Hello, world!")
    .stdout(tx.into_blocking_fd()?)
    .status();
rx.read_to_string(&mut buffer).await?;

assert!(status.await?.success());
assert_eq!(buffer, "Hello, world!\n");

§Panics

This function panics if it is not called from within a runtime with IO enabled.

The runtime is usually set implicitly when this function is called from a future driven by a tokio runtime, otherwise runtime can be set explicitly with Runtime::enter function.