Struct untrusted::Reader

source ·
pub struct Reader<'a> {
    input: Slice<'a>,
    i: usize,
}
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.

Fields§

§input: Slice<'a>§i: usize

Implementations§

source§

impl<'a> Reader<'a>

source

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.

source

pub fn at_end(&self) -> bool

Returns true if the reader is at the end of the input, and false otherwise.

source

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.

source

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.

source

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.

source

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.

source

pub fn read_partial<F, R, E>(&mut self, read: F) -> Result<(Input<'a>, R), E>
where F: FnOnce(&mut Reader<'a>) -> Result<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.

source

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.

source

pub fn skip_to_end(&mut self)

Skips the reader to the end of the input.

Trait Implementations§

source§

impl Debug for Reader<'_>

Avoids writing the value or position to avoid creating a side channel, though Reader can’t avoid leaking the position via timing.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.