Trait 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 more complex cases, where specific portions of read bytes should be included, this function can 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,