Expand description

Unix pipes.

To create a new pipe use the new function. new will return a Sender and Receiver, which are two sides of the pipe.

In addition to creating a new pipe it’s also possible to create a pipe from a process’ standard I/O when spawning another process. For this use Sender::from_child_stdin, Receiver::from_child_stdout and Receiver::from_child_stderr methods. See the example below.

Notes

Both the Sender and Receiver types are bound to an actor. See the Bound trait for more information.

Examples

Creating a new Unix pipe.

use std::io;

use heph::actor;
use heph_rt::{self as rt, pipe};

const DATA: &[u8] = b"Hello, world!";

async fn process_handler<RT>(mut ctx: actor::Context<!, RT>) -> io::Result<()>
    where RT: rt::Access,
{
    let (mut sender, mut receiver) = pipe::new(&mut ctx)?;

    // Write some data.
    sender.write_all(DATA).await?;
    drop(sender); // Close the sending side.

    // And read the data back.
    let mut buf = Vec::with_capacity(DATA.len() + 1);
    receiver.read_n(&mut buf, DATA.len()).await?;
    assert_eq!(buf, DATA);
    Ok(())
}

Spawn a process using a pipe for standard in, out and error of the spawned process.

use std::io;
use std::process::{Command, Stdio};

use heph::actor;
use heph_rt::{self as rt, pipe};

const DATA: &[u8] = b"Hello, world!";

async fn process_handler<RT>(mut ctx: actor::Context<!, RT>) -> io::Result<()>
    where RT: rt::Access,
{
    // Spawn a "echo" that echo everything read from standard in to standard
    // out.
    let mut process = Command::new("echo")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()?;

    // Create our process standard in and out.
    let mut stdin = pipe::Sender::from_child_stdin(&mut ctx, process.stdin.take().unwrap())?;
    let mut stdout = pipe::Receiver::from_child_stdout(&mut ctx, process.stdout.take().unwrap())?;

    // Write some data.
    stdin.write_all(DATA).await?;
    drop(stdin); // Close standard in for the child process.

    // And read the data back.
    let mut buf = Vec::with_capacity(DATA.len() + 1);
    stdout.read_n(&mut buf, DATA.len()).await?;
    assert_eq!(buf, DATA);
    Ok(())
}

Structs

Functions

Create a new Unix pipe.