pub struct Restrict<T> { /* private fields */ }Expand description
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§
Source§impl<T> Restrict<T>
impl<T> Restrict<T>
Sourcepub fn restriction(&self) -> u64
pub fn restriction(&self) -> u64
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(())
}Sourcepub fn set_restriction(&mut self, restriction: u64)
pub fn set_restriction(&mut self, restriction: u64)
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(())
}Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
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(())
}Sourcepub fn get_ref(&self) -> &T
pub fn get_ref(&self) -> &T
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(())
}Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
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§
Source§impl<T: BufRead> BufRead for Restrict<T>
impl<T: BufRead> BufRead for Restrict<T>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Read methods, if empty. Read moreSource§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amount of additional bytes from the internal buffer as having been read.
Subsequent calls to read only return bytes that have not been marked as read. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left)read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA byte) is reached, and append
them to the provided String buffer. Read moreSource§impl<T: Read> Read for Restrict<T>
impl<T: Read> Read for Restrict<T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more