[][src]Struct read_restrict::Restrict

pub struct Restrict<T> { /* fields omitted */ }

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

This struct is generally created by calling restrict on a reader. Please see the documentation of restrict for more details.

Implementations

impl<T> Restrict<T>[src]

pub fn restriction(&self) -> u64[src]

Returns the number of bytes that can be read before this instance will return an error.

Examples

use std::io;
use std::io::prelude::*;
use read_restrict::ReadExt;
use std::fs::File;

fn main() -> io::Result<()> {
    let f = File::open("foo.txt")?;

    // read at most five bytes
    let handle = f.restrict(5);

    println!("restriction: {}", handle.restriction());
    Ok(())
}

pub fn set_restriction(&mut self, restriction: u64)[src]

Sets the number of bytes that can be read before this instance will return an error. This is the same as constructing a new Restrict instance, so the amount of bytes read and the previous restriction value don't matter when calling this method.

Examples

use std::io;
use std::io::prelude::*;
use std::fs::File;
use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let f = File::open("foo.txt")?;

    // read at most five bytes
    let mut handle = f.restrict(5);
    handle.set_restriction(10);

    assert_eq!(handle.restriction(), 10);
    Ok(())
}

pub fn into_inner(self) -> T[src]

Consumes the Restrict, returning the wrapped reader.

Examples

use std::io;
use std::io::prelude::*;
use std::fs::File;
use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.restrict(5);
    handle.read(&mut buffer)?;

    let file = handle.into_inner();
    Ok(())
}

pub fn get_ref(&self) -> &T[src]

Gets a reference to the underlying reader.

Examples

use std::io;
use std::io::prelude::*;
use std::fs::File;
use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.restrict(5);
    handle.read(&mut buffer)?;

    let file = handle.get_ref();
    Ok(())
}

pub fn get_mut(&mut self) -> &mut T[src]

Gets a mutable reference to the underlying reader.

Care should be taken to avoid modifying the internal I/O state of the underlying reader as doing so may corrupt the internal limit of this Restrict.

Examples

use std::io;
use std::io::prelude::*;
use std::fs::File;
use read_restrict::ReadExt;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.restrict(5);
    handle.read(&mut buffer)?;

    let file = handle.get_mut();
    Ok(())
}

Trait Implementations

impl<T: BufRead> BufRead for Restrict<T>[src]

impl<T: Debug> Debug for Restrict<T>[src]

impl<T: Read> Read for Restrict<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Restrict<T> where
    T: RefUnwindSafe

impl<T> Send for Restrict<T> where
    T: Send

impl<T> Sync for Restrict<T> where
    T: Sync

impl<T> Unpin for Restrict<T> where
    T: Unpin

impl<T> UnwindSafe for Restrict<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<R> ReadExt for R where
    R: Read
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.