pub struct Parser<'a> {
pub bytes: &'a [u8],
}
Expand description
A byte-oriented parser, for use in decoding CodeView records.
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