[][src]Crate read_restrict

read-restrict

An adaptor around Rust's standard io::Take which returns an error of of the kind ErrorKind::InvalidData when the limit is exceeded.

This may be useful for enforcing resource limits while not silently truncating when they are exceeded.

Example

use std::io::{self, Read, ErrorKind};

use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let f = std::fs::File::open("foo.txt")?;
    let mut handle = f.restrict(5);
    let mut buf = [0; 8];
    assert_eq!(5, handle.read(&mut buf)?); // reads at most 5 bytes
    assert_eq!(0, handle.restriction()); // is now exhausted
    assert_eq!(ErrorKind::InvalidData, handle.read(&mut buf).unwrap_err().kind());
    Ok(())
}

Structs

Restrict

Reader adaptor which restricts the bytes read from an underlying reader, returning an IO error of the kind ErrorKind::InvalidData when it is exceeded.

Traits

ReadExt