Module io_providers::stream [] [src]

Providers of input/output/error streams (i.e. stdin, stdout and stderr).

Examples

extern crate io_providers;

use std::io::Write;
use std::path::PathBuf;
use io_providers::stream;
use io_providers::stream::Provider;

/// Takes input from stdin and prints it to stdout
fn mirror<S: stream::Provider>(streams: &mut S)  {
    let mut input = String::new();
    streams.input().read_to_string(&mut input).unwrap();
    write!(streams.output(), "{}", input);
}

fn main() {
    test_mirror();

    // Use a standard stream provider here to interact with the console
    let mut std = stream::Std::new();
    mirror(&mut std);
}

fn test_mirror() {
    // Use a virtual stream provider here to verify the functionality of `mirror()`
    let mut streams = stream::Virtual::new();
    let expected = "test";
    streams.write_input(expected.as_bytes());

    mirror(&mut streams);

    let actual = ::std::str::from_utf8(streams.read_output()).unwrap();
    assert_eq!(expected, actual);
}

Structs

Std

Provides access to the standard streams (stdin, stdout and stderr).

Virtual

Provides virtual input/output/error streams: input can be provided using Virtual::write_input(), and output can be observed using Virtual::read_output() and Virtual::read_error().

Traits

Provider

Provides access to input, output and error streams.