bparse

Struct Bytes

Source
pub struct Bytes<'i> { /* private fields */ }
Expand description

A byte slice with a movable cursor.

The cursor may be manually moved using Seek methods or by applying Patterns.

Bytes can be created from anything that implements AsRef<[u8]>:

use bparse::Bytes;

let _ = Bytes::from(b"hello");
let _ = Bytes::from("hello");
let _ = Bytes::from(&[1, 2, 3]);

Implementations§

Source§

impl<'i> Bytes<'i>

Source

pub fn parse<P: Pattern>(&mut self, pattern: P) -> Option<&'i [u8]>

Tests pattern against the bytes at the current position.

If the pattern matches, the cursor is moved, and the matching bytes are returned.

Source

pub fn parse_while<'b, P: Pattern>( &'b mut self, pattern: P, ) -> BytesMatchIterMut<'b, 'i, P>

Returns an iterator that repeatedly tests pattern against self until it stops matching.

Advancing the iterator also advances the cursor.

Source

pub fn parse_delimited<L, R>(&mut self, left: L, right: R) -> Option<&'i [u8]>
where L: Pattern, R: Pattern,

Extracts the byte slice delimited by the given patterns.

The cursor is set to right after the right match, and the bytes between the left and right matches are returned.

Returns None if left does not match. Returns None if no match for right is found after left

Source

pub fn take_until<P: Pattern>(&mut self, pattern: P) -> &'i [u8]

Advances the cursor until the pattern matches or the end of the input is encountered.

The cursor is set right before the match, and the bytes leading up to the match are returned.

Source

pub fn accept<P: Pattern>(&mut self, pattern: P) -> bool

Tests the pattern against the bytes at the current position.

If the pattern matches, the cursor is moved and the function returns true.

Source

pub fn peek<P: Pattern>(&mut self, pattern: P) -> bool

Like Bytes::accept, but the cursor is not moved after a successful match.

Source

pub fn position(&self) -> usize

Returns the cursor’s offset from the start of the input.

Source

pub fn eof(&self) -> bool

Returns true if the cursor is at the end of the input.

Note that you can still use Seek methods to move the cursor back.

Source

pub fn get_ref(&self) -> &'i [u8]

Returns a reference to the complete underlying byte slice

Trait Implementations§

Source§

impl<'i> Clone for Bytes<'i>

Source§

fn clone(&self) -> Bytes<'i>

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<'i> Debug for Bytes<'i>

Source§

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

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

impl<'i, B> From<&'i B> for Bytes<'i>
where B: ?Sized + AsRef<[u8]>,

Source§

fn from(value: &'i B) -> Self

Converts to this type from the input type.
Source§

impl<'i> PartialEq for Bytes<'i>

Source§

fn eq(&self, other: &Bytes<'i>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Seek for Bytes<'_>

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more
Source§

impl<'i> Copy for Bytes<'i>

Source§

impl<'i> Eq for Bytes<'i>

Source§

impl<'i> StructuralPartialEq for Bytes<'i>

Auto Trait Implementations§

§

impl<'i> Freeze for Bytes<'i>

§

impl<'i> RefUnwindSafe for Bytes<'i>

§

impl<'i> Send for Bytes<'i>

§

impl<'i> Sync for Bytes<'i>

§

impl<'i> Unpin for Bytes<'i>

§

impl<'i> UnwindSafe for Bytes<'i>

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.