Struct Parser

Source
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

Other methods are provided if they were needed but those should be the main ones.

Implementations§

Source§

impl<'p, B, E> Parser<B, E>
where E: Endian, B: ParseBuf<'p>,

Source

pub fn new(data: B, config: ParseConfig<E>) -> Self

Create a new parser.

Source

pub fn config(&self) -> &ParseConfig<E>

Get the ParseConfig instance for this Parser.

Source

pub fn endian(&self) -> &E

Get the endian configuration type.

Source

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.

Source

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.

Source

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.

Source

pub fn parse_if<P: Parse<'p>>(&mut self, parse: bool) -> ParseResult<Option<P>>

Parse a type only if parse is true.

Source

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.

Source

pub fn parse_u8(&mut self) -> ParseResult<u8>

Parse a single byte out of the source buffer.

Source

pub fn parse_u16(&mut self) -> ParseResult<u16>

Parse a u16 out of the source data.

Source

pub fn parse_u32(&mut self) -> ParseResult<u32>

Parse a u32 out of the source data.

Source

pub fn parse_u64(&mut self) -> ParseResult<u64>

Parse a u64 out of the source data.

Source

pub fn parse_rest(&mut self) -> ParseResult<Cow<'p, [u8]>>

Consume the rest of the buffer and return it as a slice.

Source

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.

Source

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

  1. the source endianness is the same as the endianness of this program, and
  2. 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.

Source

pub unsafe fn parse_slice<T>(&mut self, len: usize) -> ParseResult<Cow<'p, [T]>>
where T: Parse<'p> + Copy,

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.

Source

pub fn parse_repeated<T: Parse<'p>>( &mut self, len: usize, ) -> ParseResult<Vec<T>>

Parse a sequence of len Ts.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<B: Clone, E: Clone> Clone for Parser<B, E>

Source§

fn clone(&self) -> Parser<B, E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<B, E> Freeze for Parser<B, E>
where E: Freeze, B: Freeze,

§

impl<B, E> RefUnwindSafe for Parser<B, E>

§

impl<B, E> Send for Parser<B, E>
where E: Send, B: Send,

§

impl<B, E> Sync for Parser<B, E>
where E: Sync, B: Sync,

§

impl<B, E> Unpin for Parser<B, E>
where E: Unpin, B: Unpin,

§

impl<B, E> UnwindSafe for Parser<B, E>
where E: UnwindSafe, B: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.