Crate timeout_readwrite [] [src]

Provides TimeoutReader and TimeoutWriter structs to time out reads and writes, respectively.

Example: read from a process with a 5-second timeout

Given a process that writes to standard out, read from the output once it is there, but fail if you have to wait longer than 5-seconds for data to be present on standard out.

use std::io::{Read, Result};
use std::process;
use std::time::Duration;
use timeout_readwrite::TimeoutReader;

fn read_command(mut cmd: process::Command) -> Result<usize> {
    let child = cmd.stdout(process::Stdio::piped())
       .stderr(process::Stdio::null())
       .spawn()
       .expect("spawing did not succeed");
    let stdout = child.stdout.expect("stdout must be there");

    let mut data = String::new();
    let mut rdr = TimeoutReader::new(stdout, Duration::new(5, 0));
    rdr.read_to_string(&mut data)
}

Structs

TimeoutReader

The TimeoutReader struct adds read timeouts to any reader.

TimeoutWriter

The TimeoutWriter struct adds write timeouts to any writer.