Struct domain::base::octets::Parser

source ·
pub struct Parser<Ref> { /* private fields */ }
Expand description

A parser for sequentially extracting data from an octets sequence.

The parser wraps an octets reference and remembers the read position on the referenced sequence. Methods allow reading out data and progressing the position beyond processed data.

Implementations§

source§

impl<Ref> Parser<Ref>

source

pub fn from_ref(octets: Ref) -> Selfwhere Ref: AsRef<[u8]>,

Creates a new parser atop a reference to an octet sequence.

source

pub fn octets_ref(&self) -> Refwhere Ref: Copy,

Returns the wrapped reference to the underlying octets sequence.

source

pub fn pos(&self) -> usize

Returns the current parse position as an index into the octets.

source

pub fn len(&self) -> usize

Returns the length of the underlying octet sequence.

This is not the number of octets left for parsing. Use remaining for that.

source

pub fn is_empty(&self) -> bool

Returns whether the underlying octets sequence is empty.

This does not return whether there are no more octets left to parse.

source§

impl Parser<&'static [u8]>

source

pub fn from_static(slice: &'static [u8]) -> Self

Creates a new parser atop a static byte slice.

This function is most useful for testing.

source§

impl<Ref: AsRef<[u8]>> Parser<Ref>

source

pub fn as_slice(&self) -> &[u8]

Returns an octets slice of the underlying sequence.

The slice covers the entire sequence, not just the remaining data. You can use peek for that.

source

pub fn as_slice_mut(&mut self) -> &mut [u8] where Ref: AsMut<[u8]>,

Returns a mutable octets slice of the underlying sequence.

The slice covers the entire sequence, not just the remaining data.

source

pub fn remaining(&self) -> usize

Returns the number of remaining octets to parse.

source

pub fn peek(&self, len: usize) -> Result<&[u8], ParseError>

Returns a slice for the next len octets.

If less than len octets are left, returns an error.

source

pub fn peek_all(&self) -> &[u8]

Returns a slice of the data left to parse.

source

pub fn seek(&mut self, pos: usize) -> Result<(), ParseError>

Repositions the parser to the given index.

It is okay to reposition anywhere within the sequence. However, if pos is larger than the length of the sequence, an error is returned.

source

pub fn advance(&mut self, len: usize) -> Result<(), ParseError>

Advances the parser‘s position by len octets.

If this would take the parser beyond its end, an error is returned.

source

pub fn advance_to_end(&mut self)

Advances to the end of the parser.

source

pub fn check_len(&self, len: usize) -> Result<(), ParseError>

Checks that there are len octets left to parse.

If there aren’t, returns an error.

source§

impl<Ref: AsRef<[u8]>> Parser<Ref>

source

pub fn parse_octets(&mut self, len: usize) -> Result<Ref::Range, ParseError>where Ref: OctetsRef,

Takes and returns the next len octets.

Advances the parser by len octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ParseError>

Fills the provided buffer by taking octets from the parser.

Copies as many octets as the buffer is long from the parser into the buffer and advances the parser by that many octets.

If there aren’t enough octets left in the parser to fill the buffer completely, returns an error and leaves the parser untouched.

source

pub fn parse_i8(&mut self) -> Result<i8, ParseError>

Takes an i8 from the beginning of the parser.

Advances the parser by one octet. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u8(&mut self) -> Result<u8, ParseError>

Takes a u8 from the beginning of the parser.

Advances the parser by one octet. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i16(&mut self) -> Result<i16, ParseError>

Takes an i16 from the beginning of the parser.

The value is converted from network byte order into the system’s own byte order if necessary. The parser is advanced by two octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u16(&mut self) -> Result<u16, ParseError>

Takes a u16 from the beginning of the parser.

The value is converted from network byte order into the system’s own byte order if necessary. The parser is advanced by two ocetets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_i32(&mut self) -> Result<i32, ParseError>

Takes an i32 from the beginning of the parser.

The value is converted from network byte order into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_u32(&mut self) -> Result<u32, ParseError>

Takes a u32 from the beginning of the parser.

The value is converted from network byte order into the system’s own byte order if necessary. The parser is advanced by four octets. If there aren’t enough octets left, leaves the parser untouched and returns an error instead.

source

pub fn parse_block<F, U>( &mut self, limit: usize, op: F ) -> Result<U, ParseError>where F: FnOnce(&mut Self) -> Result<U, ParseError>,

Parses a given amount of octets through a closure.

Parses a block of limit octets and moves the parser to the end of that block or, if less than limit octets are still available, to the end of the parser.

The closure op will be allowed to parse up to limit octets. If it does so successfully or returns with a form error, the method returns its return value. If it returns with a short buffer error, the method returns a form error. If it returns successfully with less than limit octets parsed, returns a form error indicating trailing data. If the limit is larger than the remaining number of octets, returns a ParseError::ShortInput.

Trait Implementations§

source§

impl<Ref: Clone> Clone for Parser<Ref>

source§

fn clone(&self) -> Parser<Ref>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<Ref: Debug> Debug for Parser<Ref>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Ref: Copy> Copy for Parser<Ref>

Auto Trait Implementations§

§

impl<Ref> RefUnwindSafe for Parser<Ref>where Ref: RefUnwindSafe,

§

impl<Ref> Send for Parser<Ref>where Ref: Send,

§

impl<Ref> Sync for Parser<Ref>where Ref: Sync,

§

impl<Ref> Unpin for Parser<Ref>where Ref: Unpin,

§

impl<Ref> UnwindSafe for Parser<Ref>where Ref: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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<Source, Target> OctetsInto<Target> for Sourcewhere Target: OctetsFrom<Source>,

source§

fn octets_into(self) -> Result<Target, ShortBuf>

Performs the conversion.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

source§

fn vzip(self) -> V