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>) -> Self
pub fn new(data: B, config: ParseConfig<E>) -> Self
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) -> ParseResult<Cow<'p, [u8]>>
pub fn parse_bytes(&mut self, len: usize) -> ParseResult<Cow<'p, [u8]>>
Directly get a reference to the next len
bytes in the input buffer.
Sourcepub fn parse<P: Parse<'p>>(&mut self) -> ParseResult<P>
pub fn parse<P: Parse<'p>>(&mut self) -> ParseResult<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) -> ParseResult<R>where
F: FnOnce(&mut Self) -> ParseResult<R>,
pub fn parse_with<F, R>(&mut self, func: F) -> ParseResult<R>where
F: FnOnce(&mut Self) -> ParseResult<R>,
Parse with an explicit parsing function.
Sourcepub fn parse_if<P: Parse<'p>>(&mut self, parse: bool) -> ParseResult<Option<P>>
pub fn parse_if<P: Parse<'p>>(&mut self, parse: bool) -> ParseResult<Option<P>>
Parse a type only if parse
is true.
Sourcepub fn parse_if_with<F, R>(
&mut self,
parse: bool,
func: F,
) -> ParseResult<Option<R>>where
F: FnOnce(&mut Self) -> ParseResult<R>,
pub fn parse_if_with<F, R>(
&mut self,
parse: bool,
func: F,
) -> ParseResult<Option<R>>where
F: FnOnce(&mut Self) -> ParseResult<R>,
parse_if
but using an explicit parsing function.
Sourcepub fn parse_u8(&mut self) -> ParseResult<u8>
pub fn parse_u8(&mut self) -> ParseResult<u8>
Parse a single byte out of the source buffer.
Sourcepub fn parse_u16(&mut self) -> ParseResult<u16>
pub fn parse_u16(&mut self) -> ParseResult<u16>
Parse a u16
out of the source data.
Sourcepub fn parse_u32(&mut self) -> ParseResult<u32>
pub fn parse_u32(&mut self) -> ParseResult<u32>
Parse a u32
out of the source data.
Sourcepub fn parse_u64(&mut self) -> ParseResult<u64>
pub fn parse_u64(&mut self) -> ParseResult<u64>
Parse a u64
out of the source data.
Sourcepub fn parse_rest(&mut self) -> ParseResult<Cow<'p, [u8]>>
pub fn parse_rest(&mut self) -> ParseResult<Cow<'p, [u8]>>
Consume the rest of the buffer and return it as a slice.
Sourcepub fn parse_rest_trim_nul(&mut self) -> ParseResult<Cow<'p, [u8]>>
pub fn parse_rest_trim_nul(&mut self) -> ParseResult<Cow<'p, [u8]>>
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,
) -> ParseResult<Option<&'p [T]>>where
T: Copy,
pub unsafe fn parse_slice_direct<T>(
&mut self,
len: usize,
) -> ParseResult<Option<&'p [T]>>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) -> ParseResult<Cow<'p, [T]>>
pub unsafe fn parse_slice<T>(&mut self, len: usize) -> ParseResult<Cow<'p, [T]>>
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: Parse<'p>>(
&mut self,
len: usize,
) -> ParseResult<Vec<T>>
pub fn parse_repeated<T: Parse<'p>>( &mut self, len: usize, ) -> ParseResult<Vec<T>>
Parse a sequence of len
T
s.
Sourcepub fn parse_metadata(
&mut self,
) -> ParseResult<(Parser<impl ParseBuf<'p>, E>, RecordMetadata)>
pub fn parse_metadata( &mut self, ) -> ParseResult<(Parser<impl ParseBuf<'p>, E>, RecordMetadata)>
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,
) -> ParseResult<(Parser<impl ParseBuf<'p>, E>, RecordMetadata)>
pub fn parse_metadata_with_header( &mut self, header: perf_event_header, ) -> ParseResult<(Parser<impl ParseBuf<'p>, E>, RecordMetadata)>
Parse the record metadata and return a parser for only the record bytes.
Sourcepub fn parse_record<V: Visitor<'p>>(
&mut self,
visitor: V,
) -> ParseResult<V::Output>
pub fn parse_record<V: Visitor<'p>>( &mut self, visitor: V, ) -> ParseResult<V::Output>
Parse a record, the record types will be visited by the visitor
.
Sourcepub fn parse_record_with_header<V: Visitor<'p>>(
&mut self,
visitor: V,
header: perf_event_header,
) -> ParseResult<V::Output>
pub fn parse_record_with_header<V: Visitor<'p>>( &mut self, visitor: V, header: perf_event_header, ) -> ParseResult<V::Output>
Same as parse_record
but required that the
header be provided.