Struct NomBytes

Source
pub struct NomBytes(/* private fields */);
Expand description

A wrapper around bytes::Bytes to be able to use it with nom.

Implementations§

Source§

impl NomBytes

Source

pub fn new(bytes: Bytes) -> Self

Creates a new NomBytes wrapping the provided Bytes.

§Examples
use bytes::Bytes;
use nombytes::NomBytes;

let b = Bytes::new();
let nb = NomBytes::new(b);
Source

pub fn to_str(&self) -> &str

Returns a string slice to the contents of the inner Bytes.

§Examples
use bytes::Bytes;
use nombytes::NomBytes;

let nb = NomBytes::new(Bytes::from("hello"));
assert_eq!(nb.to_str(), "hello");
§Panics

Panics if the Bytes slice is not UTF-8.

Source

pub fn try_to_str(&self) -> Result<&str, Utf8Error>

Returns a string slice to the contents of the inner Bytes.

§Examples
use bytes::Bytes;
use nombytes::NomBytes;

let nb = NomBytes::new(Bytes::from("hello"));
assert_eq!(nb.try_to_str().unwrap(), "hello");
§Errors

Returns Err if the Bytes slice is not UTF-8 with a description as to why the provided slice is not UTF-8.

Source

pub fn to_bytes(&self) -> Bytes

Returns a Bytes representing the real state of the inner one.

Be careful using a Bytes returned from this function to create a new NomBytes for use in the same parsing session, as due to an optimization in Bytes, creating an empty slice (e.g. asking for the slice of 0..0 or ..0, which nom sometimes does) results in a Bytes that is unrelated to its source, which causes later offset calculations to give incorrect results.

This behavior is accounted for internally, so as long as you stick to only using NomBytes directly without going to Bytes and back, you won’t be affected by this optimization behavior.

§Examples
use bytes::Bytes;
use nombytes::NomBytes;

let nb = NomBytes::new(Bytes::from("hello"));
let b = nb.to_bytes();
assert_eq!(b.as_ref(), b"hello");
Source

pub fn into_bytes(self) -> Bytes

Returns a Bytes representing the real state of the inner one.

Be careful using a Bytes returned from this function to create a new NomBytes for use in the same parsing session, as due to an optimization in Bytes, creating an empty slice (e.g. asking for the slice of 0..0 or ..0, which nom sometimes does) results in a Bytes that is unrelated to its source, which causes later offset calculations to give incorrect results.

This behavior is accounted for internally, so as long as you stick to only using NomBytes directly without going to Bytes and back, you won’t be affected by this optimization behavior.

§Examples
use bytes::Bytes;
use nombytes::NomBytes;

let nb = NomBytes::new(Bytes::from("hello"));
let b = nb.into_bytes();
assert_eq!(b.as_ref(), b"hello");
Source

pub fn into_raw(self) -> (Bytes, Option<RangeType<usize>>)

Returns the values from the inner representation of this type.

See into_bytes for an explanation of why this inner representation exists.

Source

pub fn from_raw((bytes, range_type): (Bytes, Option<RangeType<usize>>)) -> Self

Returns a new NomBytes using the raw values passed in. If these values represent something invalid, you’ll likely see incorrect behavior or even panics. Regular usage should create values using new instead.

See into_bytes for an explanation of why this inner representation exists.

Trait Implementations§

Source§

impl AsBytes for NomBytes

Source§

fn as_bytes(&self) -> &[u8]

Casts the input type to a byte slice
Source§

impl Clone for NomBytes

Source§

fn clone(&self) -> NomBytes

Returns a duplicate 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 Compare<&str> for NomBytes

Source§

fn compare(&self, t: &str) -> CompareResult

Compares self to another value for equality
Source§

fn compare_no_case(&self, t: &str) -> CompareResult

Compares self to another value for equality independently of the case. Read more
Source§

impl Compare<NomBytes> for NomBytes

Source§

fn compare(&self, t: NomBytes) -> CompareResult

Compares self to another value for equality
Source§

fn compare_no_case(&self, t: NomBytes) -> CompareResult

Compares self to another value for equality independently of the case. Read more
Source§

impl Debug for NomBytes

Source§

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

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

impl<'de> Deserialize<'de> for NomBytes

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for NomBytes

Source§

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

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

impl From<&[u8]> for NomBytes

Source§

fn from(byte_slice: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for NomBytes

Source§

fn from(string: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for NomBytes

Available on crate feature std only.
Source§

fn from(string: String) -> Self

Converts to this type from the input type.
Source§

impl InputIter for NomBytes

Source§

type Item = u8

The current input type is a sequence of that Item type. Read more
Source§

type Iter = Enumerate<<NomBytes as InputIter>::IterElem>

An iterator over the input type, producing the item and its position for use with Slice. If we’re iterating over &str, the position corresponds to the byte index of the character
Source§

type IterElem = IntoIter<Bytes>

An iterator over the input type, producing the item
Source§

fn iter_indices(&self) -> Self::Iter

Returns an iterator over the elements and their byte offsets
Source§

fn iter_elements(&self) -> Self::IterElem

Returns an iterator over the elements
Source§

fn position<P>(&self, predicate: P) -> Option<usize>
where P: Fn(Self::Item) -> bool,

Finds the byte position of the element
Source§

fn slice_index(&self, count: usize) -> Result<usize, Needed>

Get the byte offset from the element’s position in the stream
Source§

impl InputLength for NomBytes

Source§

fn input_len(&self) -> usize

Calculates the input length, as indicated by its name, and the name of the trait itself
Source§

impl InputTake for NomBytes

Source§

fn take(&self, count: usize) -> Self

Returns a slice of count bytes. panics if count > length
Source§

fn take_split(&self, count: usize) -> (Self, Self)

Split the stream at the count byte offset. panics if count > length
Source§

impl InputTakeAtPosition for NomBytes

Source§

type Item = <NomBytes as InputIter>::Item

The current input type is a sequence of that Item type. Read more
Source§

fn split_at_position<P, E: ParseError<Self>>( &self, predicate: P, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

Looks for the first element of the input type for which the condition returns true, and returns the input up to this position. Read more
Source§

fn split_at_position1<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

Looks for the first element of the input type for which the condition returns true and returns the input up to this position. Read more
Source§

fn split_at_position_complete<P, E: ParseError<Self>>( &self, predicate: P, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

Looks for the first element of the input type for which the condition returns true, and returns the input up to this position. Read more
Source§

fn split_at_position1_complete<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

Looks for the first element of the input type for which the condition returns true and returns the input up to this position. Read more
Source§

impl Offset for NomBytes

Source§

fn offset(&self, second: &Self) -> usize

Offset between the first byte of self and the first byte of the argument
Source§

impl Ord for NomBytes

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NomBytes

Source§

fn eq(&self, other: &Self) -> 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 PartialOrd for NomBytes

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for NomBytes

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Slice<Range<usize>> for NomBytes

Source§

fn slice(&self, range: Range<usize>) -> Self

Slices self according to the range argument
Source§

impl Slice<RangeFrom<usize>> for NomBytes

Source§

fn slice(&self, range: RangeFrom<usize>) -> Self

Slices self according to the range argument
Source§

impl Slice<RangeFull> for NomBytes

Source§

fn slice(&self, range: RangeFull) -> Self

Slices self according to the range argument
Source§

impl Slice<RangeTo<usize>> for NomBytes

Source§

fn slice(&self, range: RangeTo<usize>) -> Self

Slices self according to the range argument
Source§

impl SourceCode for NomBytes

Available on crate feature miette only.
Source§

fn read_span<'a>( &'a self, span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, ) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>

Read the bytes for a specific span from this SourceCode, keeping a certain number of lines before and after the span as context.
Source§

impl Eq for NomBytes

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,