pub struct Reader<'a> { /* private fields */ }
Expand description
A read-only, forward-only cursor into the data in an Input
.
Using Reader
to parse input helps to ensure that no byte of the input
will be accidentally processed more than once. Using Reader
in
conjunction with read_all
and read_all_optional
helps ensure that no
byte of the input is accidentally left unprocessed. The methods of Reader
never panic, so Reader
also assists the writing of panic-free code.
Intentionally avoids implementing PartialEq
and Eq
to avoid implicit
non-constant-time comparisons.
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn new(input: Input<'a>) -> Self
pub fn new(input: Input<'a>) -> Self
Construct a new Reader for the given input. Use read_all
or
read_all_optional
instead of Reader::new
whenever possible.
Sourcepub fn at_end(&self) -> bool
pub fn at_end(&self) -> bool
Returns true
if the reader is at the end of the input, and false
otherwise.
Sourcepub fn peek(&self, b: u8) -> bool
pub fn peek(&self, b: u8) -> bool
Returns true
if there is at least one more byte in the input and that
byte is equal to b
, and false otherwise.
Sourcepub fn read_byte(&mut self) -> Result<u8, EndOfInput>
pub fn read_byte(&mut self) -> Result<u8, EndOfInput>
Reads the next input byte.
Returns Ok(b)
where b
is the next input byte, or Err(EndOfInput)
if the Reader
is at the end of the input.
Sourcepub fn read_bytes(&mut self, num_bytes: usize) -> Result<Input<'a>, EndOfInput>
pub fn read_bytes(&mut self, num_bytes: usize) -> Result<Input<'a>, EndOfInput>
Skips num_bytes
of the input, returning the skipped input as an
Input
.
Returns Ok(i)
if there are at least num_bytes
of input remaining,
and Err(EndOfInput)
otherwise.
Sourcepub fn read_bytes_to_end(&mut self) -> Input<'a>
pub fn read_bytes_to_end(&mut self) -> Input<'a>
Skips the reader to the end of the input, returning the skipped input
as an Input
.
Sourcepub fn read_partial<F, R, E>(&mut self, read: F) -> Result<(Input<'a>, R), E>
pub fn read_partial<F, R, E>(&mut self, read: F) -> Result<(Input<'a>, R), E>
Calls read()
with the given input as a Reader
. On success, returns a
pair (bytes_read, r)
where bytes_read
is what read()
consumed and
r
is read()
’s return value.
Sourcepub fn skip(&mut self, num_bytes: usize) -> Result<(), EndOfInput>
pub fn skip(&mut self, num_bytes: usize) -> Result<(), EndOfInput>
Skips num_bytes
of the input.
Returns Ok(i)
if there are at least num_bytes
of input remaining,
and Err(EndOfInput)
otherwise.
Sourcepub fn skip_to_end(&mut self)
pub fn skip_to_end(&mut self)
Skips the reader to the end of the input.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Reader<'a>
impl<'a> RefUnwindSafe for Reader<'a>
impl<'a> Send for Reader<'a>
impl<'a> Sync for Reader<'a>
impl<'a> Unpin for Reader<'a>
impl<'a> UnwindSafe for Reader<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 24 bytes