Trait process_control::PipeFilter

source ·
pub trait PipeFilter:
    'static
    + FnMut(&[u8]) -> Result<bool>
    + Send { }
Expand description

A function to be called for reads from a specific process pipe (stdout or stderr).

When additional bytes are read from the pipe, they are passed to this function, which determines whether to include them in Output. The number of bytes is not guaranteed to be consistent and may not match the number written at any time by the command on the other side of the stream.

If this function returns Ok(false), the passed output will be discarded and not included in Output. Errors will be propagated to Control::wait. For any more complex cases, where only slices of bytes should be included in some cases, this function can always return false and maintain the output buffer itself.

§Examples

use std::io;
use std::io::Write;
use std::process::Command;
use std::process::Stdio;

use process_control::ChildExt;
use process_control::Control;

let message = "foobar";
let output = Command::new("echo")
    .arg(message)
    .stdout(Stdio::piped())
    .spawn()?
    .controlled_with_output()
    // Stream output while collecting it.
    .stdout_filter(|x| io::stdout().write_all(x).map(|()| true))
    .wait()?
    .expect("process timed out");
assert!(output.status.success());
assert_eq!(message.as_bytes(), &output.stdout[..message.len()]);

Implementors§

source§

impl<T> PipeFilter for T
where T: 'static + FnMut(&[u8]) -> Result<bool> + Send,