pub struct Parser<B, E> { /* private fields */ }Expand description
A ParseConfig combined with a ParseBuf.
This type is the base on which all parsing in this library occurs. It has a
number of helper methods that do common parsing operations needed when
implementing Parse for a record type.
§Important Methods
If you are using this library to parse an perf event stream emitted either
by perf_event_open(2) or by parsing a perf.data file then likely
want one of
If you are implementing Parse for a type then you will likely be using
parse, and,parse_bytes
Other methods are provided if they were needed but those should be the main ones.
Implementations§
Source§impl<'p, B, E> Parser<B, E>
impl<'p, B, E> Parser<B, E>
Sourcepub fn new(data: B, config: ParseConfig<E>) -> Parser<B, E>
pub fn new(data: B, config: ParseConfig<E>) -> Parser<B, E>
Create a new parser.
Sourcepub fn config(&self) -> &ParseConfig<E>
pub fn config(&self) -> &ParseConfig<E>
Get the ParseConfig instance for this Parser.
Sourcepub fn parse_bytes(&mut self, len: usize) -> Result<Cow<'p, [u8]>, ParseError>
pub fn parse_bytes(&mut self, len: usize) -> Result<Cow<'p, [u8]>, ParseError>
Directly get a reference to the next len bytes in the input buffer.
Sourcepub fn parse<P>(&mut self) -> Result<P, ParseError>where
P: Parse<'p>,
pub fn parse<P>(&mut self) -> Result<P, ParseError>where
P: Parse<'p>,
Parse a type.
If the type fails to parse then this parser will not be modified.
Sourcepub fn parse_with<F, R>(&mut self, func: F) -> Result<R, ParseError>
pub fn parse_with<F, R>(&mut self, func: F) -> Result<R, ParseError>
Parse with an explicit parsing function.
Sourcepub fn parse_if<P>(&mut self, parse: bool) -> Result<Option<P>, ParseError>where
P: Parse<'p>,
pub fn parse_if<P>(&mut self, parse: bool) -> Result<Option<P>, ParseError>where
P: Parse<'p>,
Parse a type only if parse is true.
Sourcepub fn parse_if_with<F, R>(
&mut self,
parse: bool,
func: F,
) -> Result<Option<R>, ParseError>
pub fn parse_if_with<F, R>( &mut self, parse: bool, func: F, ) -> Result<Option<R>, ParseError>
parse_if but using an explicit parsing function.
Sourcepub fn parse_u8(&mut self) -> Result<u8, ParseError>
pub fn parse_u8(&mut self) -> Result<u8, ParseError>
Parse a single byte out of the source buffer.
Sourcepub fn parse_u16(&mut self) -> Result<u16, ParseError>
pub fn parse_u16(&mut self) -> Result<u16, ParseError>
Parse a u16 out of the source data.
Sourcepub fn parse_u32(&mut self) -> Result<u32, ParseError>
pub fn parse_u32(&mut self) -> Result<u32, ParseError>
Parse a u32 out of the source data.
Sourcepub fn parse_u64(&mut self) -> Result<u64, ParseError>
pub fn parse_u64(&mut self) -> Result<u64, ParseError>
Parse a u64 out of the source data.
Sourcepub fn parse_rest(&mut self) -> Result<Cow<'p, [u8]>, ParseError>
pub fn parse_rest(&mut self) -> Result<Cow<'p, [u8]>, ParseError>
Consume the rest of the buffer and return it as a slice.
Sourcepub fn parse_rest_trim_nul(&mut self) -> Result<Cow<'p, [u8]>, ParseError>
pub fn parse_rest_trim_nul(&mut self) -> Result<Cow<'p, [u8]>, ParseError>
Parse the rest of the bytes in the buffer but trim trailing nul bytes.
Sourcepub unsafe fn parse_slice_direct<T>(
&mut self,
len: usize,
) -> Result<Option<&'p [T]>, ParseError>where
T: Copy,
pub unsafe fn parse_slice_direct<T>(
&mut self,
len: usize,
) -> Result<Option<&'p [T]>, ParseError>where
T: Copy,
Attempt to directly transmute a slice in the source buffer to one of type T.
This method will only succeed if
- the source endianness is the same as the endianness of this program, and
- the buffer is properly aligned for
T.
This method is mainly meant to reduce copying when parsing the records emitted directly from the kernel. If you are parsing from a buffer read in from a file then it is unlikely that you will meet all the required preconditions.
§Safety
It must be valid to transmute T directly from bytes. The Copy bound
is a step towards ensuring this.
Sourcepub unsafe fn parse_slice<T>(
&mut self,
len: usize,
) -> Result<Cow<'p, [T]>, ParseError>
pub unsafe fn parse_slice<T>( &mut self, len: usize, ) -> Result<Cow<'p, [T]>, ParseError>
Attempt to directly transmute a slice in the source buffer and, if that fails, parse it instead.
§Safety
This has all the same safety preconditions as
parse_slice_direct. That is, is must be
valid to transmute bytes in to a T instance.
Sourcepub fn parse_repeated<T>(&mut self, len: usize) -> Result<Vec<T>, ParseError>where
T: Parse<'p>,
pub fn parse_repeated<T>(&mut self, len: usize) -> Result<Vec<T>, ParseError>where
T: Parse<'p>,
Parse a sequence of len Ts.
Sourcepub fn parse_metadata(
&mut self,
) -> Result<(Parser<impl ParseBuf<'p>, E>, RecordMetadata), ParseError>
pub fn parse_metadata( &mut self, ) -> Result<(Parser<impl ParseBuf<'p>, E>, RecordMetadata), ParseError>
Parse record metadata and return a parser for the bytes of the record.
If you have already read the record header, use
parse_metadata_with_header
instead.
Sourcepub fn parse_metadata_with_header(
&mut self,
header: perf_event_header,
) -> Result<(Parser<impl ParseBuf<'p>, E>, RecordMetadata), ParseError>
pub fn parse_metadata_with_header( &mut self, header: perf_event_header, ) -> Result<(Parser<impl ParseBuf<'p>, E>, RecordMetadata), ParseError>
Parse the record metadata and return a parser for only the record bytes.
Sourcepub fn parse_record<V>(
&mut self,
visitor: V,
) -> Result<<V as Visitor<'p>>::Output, ParseError>where
V: Visitor<'p>,
pub fn parse_record<V>(
&mut self,
visitor: V,
) -> Result<<V as Visitor<'p>>::Output, ParseError>where
V: Visitor<'p>,
Parse a record, the record types will be visited by the visitor.
Sourcepub fn parse_record_with_header<V>(
&mut self,
visitor: V,
header: perf_event_header,
) -> Result<<V as Visitor<'p>>::Output, ParseError>where
V: Visitor<'p>,
pub fn parse_record_with_header<V>(
&mut self,
visitor: V,
header: perf_event_header,
) -> Result<<V as Visitor<'p>>::Output, ParseError>where
V: Visitor<'p>,
Same as parse_record but required that the
header be provided.