[][src]Crate read_restrict

read-restrict

An adaption of Rust's standard Read::take implementation modified to return an error when the limit is exceeded.

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

Example

use std::io::{self, ErrorKind, Result};
use std::io::prelude::*;
use std::fs::File;
use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let f = File::open("foo.txt")?.restrict(5);
    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 when it is exceeded.

Traits

ReadExt