1 unstable release

Uses old Rust 2015

0.1.0 Oct 18, 2018

#116 in #closures

BSD-2-Clause

5KB
98 lines

GitHub license Crates.io

This implements the Read trait, calling a function to generate the data.

See the API documentation.

Import Crate

read_with="0.1"

Example

let mut output = vec!();
let many_strings = ["one", "two", "three"];
let mut pos = 0;

std::io::copy(
    &mut ReadWith::new(
        ||
        {
            if pos == many_strings.len() { return None; }
            let o = many_strings[pos];
            pos+=1;
            Some(o)
        }
    ),
    &mut output,
).unwrap();
assert_eq!("onetwothree", str::from_utf8(&output).unwrap());

lib.rs:

Create a Read object that gets its data incrementally from a function.

This lets you read from an a vector of vectors or create a reader that gets blocks from a database or other data source.

Example:

let many_strings = ["one", "two", "three"];
let mut pos = 0;
std::io::copy(
    &mut read_with::ReadWith::new(
        ||
        {
            if pos == many_strings.len() { return None; }
            let o = many_strings[pos];
            pos+=1;
            Some(o)
        }
    ),
    &mut std::io::stdout(),
).unwrap();

No runtime deps