pub struct Parser<'a> {
pub bytes: &'a [u8],
}Expand description
A byte-oriented parser.
Fields§
§bytes: &'a [u8]The bytes that have not yet been parsed.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn peek_rest(&self) -> &'a [u8] ⓘ
pub fn peek_rest(&self) -> &'a [u8] ⓘ
Gets the rest of the unparsed bytes in the parser. The parser still retains a reference to the same data.
Sourcepub fn into_rest(self) -> &'a [u8] ⓘ
pub fn into_rest(self) -> &'a [u8] ⓘ
Consumes this Parser and returns the unparsed bytes within it.
This should be used in situations where there is no valid reason to use the Parser
after taking the rest of the bytes within it. In situations where a parse() method only
has access to &mut Parser, then this function cannot be used, and the caller should use
Parser::take_rest.
Sourcepub fn needs(&self, n: usize) -> Result<(), ParserError>
pub fn needs(&self, n: usize) -> Result<(), ParserError>
Checks that the buffer has at least n bytes.
This can be used as an optimization improvement in some situations. Ordinarily, code like this will compile to a series of bounds checks:
let mut p = Parser::new(bytes);
let a = p.u32()?;
let b = p.u16()?;
let c = p.u16()?;
let d = p.u32()?;Inserting a a.needs(12)? statement can sometimes enable the compiler to collapse a
series of bounds checks (4, in this case) to a single bounds check.
Sourcepub fn bytes(&mut self, n: usize) -> Result<&'a [u8], ParserError>
pub fn bytes(&mut self, n: usize) -> Result<&'a [u8], ParserError>
Takes the next n bytes of input and returns a slice to it. The parser is advanced by n.
Sourcepub fn get<T: FromBytes + Unaligned + KnownLayout + Immutable>(
&mut self,
) -> Result<&'a T, ParserError>
pub fn get<T: FromBytes + Unaligned + KnownLayout + Immutable>( &mut self, ) -> Result<&'a T, ParserError>
Parses a reference to a structure. The input must contain at least size_of::<T>() bytes.
Sourcepub fn copy<T: FromBytes + Unaligned>(&mut self) -> Result<T, ParserError>
pub fn copy<T: FromBytes + Unaligned>(&mut self) -> Result<T, ParserError>
Parses a copy of a structure. The input must contain at least size_of::<T>() bytes.
Sourcepub fn parse<T: Parse<'a>>(&mut self) -> Result<T, ParserError>
pub fn parse<T: Parse<'a>>(&mut self) -> Result<T, ParserError>
Parses a T from the input, if T knows how to read from a Parser.
This exists mainly to allow more succinct calls, using type inference.
Sourcepub fn slice<T: FromBytes + Unaligned + Immutable>(
&mut self,
len: usize,
) -> Result<&'a [T], ParserError>
pub fn slice<T: FromBytes + Unaligned + Immutable>( &mut self, len: usize, ) -> Result<&'a [T], ParserError>
Parses a slice of items. The input must contain at least [size_of::<T>() * n] bytes.
Sourcepub fn array<const N: usize>(&mut self) -> Result<[u8; N], ParserError>
pub fn array<const N: usize>(&mut self) -> Result<[u8; N], ParserError>
Copies an array of items with a constant size and advances the parser.
Sourcepub fn u8(&mut self) -> Result<u8, ParserError>
pub fn u8(&mut self) -> Result<u8, ParserError>
Reads one byte and advances.
Sourcepub fn i8(&mut self) -> Result<i8, ParserError>
pub fn i8(&mut self) -> Result<i8, ParserError>
Reads one signed byte and advances.
Sourcepub fn i16(&mut self) -> Result<i16, ParserError>
pub fn i16(&mut self) -> Result<i16, ParserError>
Reads an i16 (in little-endian order) and advances.
Sourcepub fn i32(&mut self) -> Result<i32, ParserError>
pub fn i32(&mut self) -> Result<i32, ParserError>
Reads an i32 (in little-endian order) and advances.
Sourcepub fn i64(&mut self) -> Result<i64, ParserError>
pub fn i64(&mut self) -> Result<i64, ParserError>
Reads an i64 (in little-endian order) and advances.
Sourcepub fn u16(&mut self) -> Result<u16, ParserError>
pub fn u16(&mut self) -> Result<u16, ParserError>
Reads an u16 (in little-endian order) and advances.
Sourcepub fn u32(&mut self) -> Result<u32, ParserError>
pub fn u32(&mut self) -> Result<u32, ParserError>
Reads an u32 (in little-endian order) and advances.
Sourcepub fn u64(&mut self) -> Result<u64, ParserError>
pub fn u64(&mut self) -> Result<u64, ParserError>
Reads an u64 (in little-endian order) and advances.
Sourcepub fn u128(&mut self) -> Result<u128, ParserError>
pub fn u128(&mut self) -> Result<u128, ParserError>
Reads an u128 (in little-endian order) and advances.
Sourcepub fn i128(&mut self) -> Result<i128, ParserError>
pub fn i128(&mut self) -> Result<i128, ParserError>
Reads an i128 (in little-endian order) and advances.
Sourcepub fn f32(&mut self) -> Result<f32, ParserError>
pub fn f32(&mut self) -> Result<f32, ParserError>
Reads an f32 (in little-endian order) and advances.
Sourcepub fn f64(&mut self) -> Result<f64, ParserError>
pub fn f64(&mut self) -> Result<f64, ParserError>
Reads an f64 (in little-endian order) and advances.
Sourcepub fn skip_strz(&mut self) -> Result<(), ParserError>
pub fn skip_strz(&mut self) -> Result<(), ParserError>
Skips over a NUL-terminated string.
Sourcepub fn strz(&mut self) -> Result<&'a BStr, ParserError>
pub fn strz(&mut self) -> Result<&'a BStr, ParserError>
Reads a NUL-terminated string, without checking that it is UTF-8 encoded.
Sourcepub fn strt_raw(&mut self) -> Result<&'a BStr, ParserError>
pub fn strt_raw(&mut self) -> Result<&'a BStr, ParserError>
Reads a length-prefixed string, without checking that it is UTF-8 encoded.
Sourcepub fn strt(&mut self) -> Result<&'a str, ParserError>
pub fn strt(&mut self) -> Result<&'a str, ParserError>
Reads a length-prefixed string.
Sourcepub fn type_index(&mut self) -> Result<TypeIndex, ParserError>
pub fn type_index(&mut self) -> Result<TypeIndex, ParserError>
Parses a 32-bit TypeIndex.
Sourcepub fn number(&mut self) -> Result<Number<'a>, ParserError>
pub fn number(&mut self) -> Result<Number<'a>, ParserError>
Parses a generic number value.
See Section 4, numeric leaves
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Parser<'a>
impl<'a> RefUnwindSafe for Parser<'a>
impl<'a> Send for Parser<'a>
impl<'a> Sync for Parser<'a>
impl<'a> Unpin for Parser<'a>
impl<'a> UnwindSafe for Parser<'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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.