nom8::input

Struct Stateful

Source
pub struct Stateful<I, S> {
    pub input: I,
    pub state: S,
}
Expand description

Thread global state through your parsers

Use cases

  • Recusion checks
  • Errror recovery
  • Debugging

§Example


#[derive(Clone, Debug)]
struct State<'s>(&'s Cell<u32>);

impl<'s> State<'s> {
    fn count(&self) {
        self.0.set(self.0.get() + 1);
    }
}

type Input<'is> = Stateful<&'is str, State<'is>>;

fn word(i: Input<'_>) -> IResult<Input<'_>, &str> {
  i.state.count();
  alpha1(i)
}

let data = "Hello";
let state = Cell::new(0);
let input = Input { input: data, state: State(&state) };
let output = word.parse(input).finish().unwrap();
assert_eq!(state.get(), 1);

Fields§

§input: I

Inner input being wrapped in state

§state: S

User-provided state

Trait Implementations§

Source§

impl<I, S> AsBytes for Stateful<I, S>
where I: AsBytes,

Source§

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

Casts the input type to a byte slice
Source§

impl<I, S> AsRef<I> for Stateful<I, S>

Source§

fn as_ref(&self) -> &I

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<I: Clone, S: Clone> Clone for Stateful<I, S>

Source§

fn clone(&self) -> Stateful<I, S>

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, S, U> Compare<U> for Stateful<I, S>
where I: Compare<U>,

Source§

fn compare(&self, other: U) -> CompareResult

Compares self to another value for equality
Source§

fn compare_no_case(&self, other: U) -> CompareResult

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

impl<I: Debug, S: Debug> Debug for Stateful<I, S>

Source§

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

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

impl<I, S> Deref for Stateful<I, S>

Source§

type Target = I

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<I, S> ExtendInto for Stateful<I, S>
where I: ExtendInto,

Source§

type Item = <I as ExtendInto>::Item

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

type Extender = <I as ExtendInto>::Extender

The type that will be produced
Source§

fn new_builder(&self) -> Self::Extender

Create a new Extend of the correct type
Source§

fn extend_into(&self, extender: &mut Self::Extender)

Accumulate the input into an accumulator
Source§

impl<I, S, T> FindSubstring<T> for Stateful<I, S>
where I: FindSubstring<T>,

Source§

fn find_substring(&self, substr: T) -> Option<usize>

Returns the byte position of the substring if it is found
Source§

impl<I, S> HexDisplay for Stateful<I, S>
where I: HexDisplay,

Source§

fn to_hex(&self, chunk_size: usize) -> String

Converts the value of self to a hex dump, returning the owned String.
Source§

fn to_hex_from(&self, chunk_size: usize, from: usize) -> String

Converts the value of self to a hex dump beginning at from address, returning the owned String.
Source§

impl<I, S> InputIsStreaming<false> for Stateful<I, S>
where I: InputIsStreaming<false>,

Source§

type Complete = Stateful<I, S>

Complete counterpart Read more
Source§

type Streaming = Stateful<<I as InputIsStreaming<false>>::Streaming, S>

Streaming counterpart Read more
Source§

fn into_complete(self) -> Self::Complete

Convert to complete counterpart
Source§

fn into_streaming(self) -> Self::Streaming

Convert to streaming counterpart
Source§

impl<I, S> InputIsStreaming<true> for Stateful<I, S>
where I: InputIsStreaming<true>,

Source§

type Complete = Stateful<<I as InputIsStreaming<true>>::Complete, S>

Complete counterpart Read more
Source§

type Streaming = Stateful<I, S>

Streaming counterpart Read more
Source§

fn into_complete(self) -> Self::Complete

Convert to complete counterpart
Source§

fn into_streaming(self) -> Self::Streaming

Convert to streaming counterpart
Source§

impl<I, S> InputIter for Stateful<I, S>
where I: InputIter,

Source§

type Item = <I as InputIter>::Item

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

type Iter = <I as InputIter>::Iter

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 = <I as InputIter>::IterElem

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<I, S> InputLength for Stateful<I, S>
where I: InputLength,

Source§

fn input_len(&self) -> usize

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

impl<I, S> InputTake for Stateful<I, S>
where I: InputTake, S: Clone,

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<I, S> InputTakeAtPosition for Stateful<I, S>

Source§

type Item = <I as InputTakeAtPosition>::Item

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

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

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_streaming<P, E>( &self, predicate: P, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool, E: ParseError<Self>,

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_streaming<P, E>( &self, predicate: P, kind: ErrorKind, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool, E: ParseError<Self>,

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>( &self, predicate: P, kind: ErrorKind, ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool, E: ParseError<Self>,

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<I, S> IntoOutput for Stateful<I, S>
where I: IntoOutput,

Source§

type Output = <I as IntoOutput>::Output

Output type
Source§

fn into_output(self) -> Self::Output

Convert an Input into an appropriate Output type
Source§

fn merge_output(self, inner: Self::Output) -> Self

Convert an Output type to be used as Input
Source§

impl<I, S> Location for Stateful<I, S>
where I: Location,

Source§

fn location(&self) -> usize

Number of indices input has advanced since start of parsing
Source§

impl<I, S> Offset for Stateful<I, S>
where I: Offset,

Source§

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

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

impl<I, S, R> ParseTo<R> for Stateful<I, S>
where I: ParseTo<R>,

Source§

fn parse_to(&self) -> Option<R>

Succeeds if parse() succeeded. The byte slice implementation will first convert it to a &str, then apply the parse() function
Source§

impl<I: PartialEq, S: PartialEq> PartialEq for Stateful<I, S>

Source§

fn eq(&self, other: &Stateful<I, S>) -> 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<I, S, R> Slice<R> for Stateful<I, S>
where I: Slice<R>, S: Clone,

Source§

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

Slices self according to the range argument
Source§

impl<I: Copy, S: Copy> Copy for Stateful<I, S>

Source§

impl<I: Eq, S: Eq> Eq for Stateful<I, S>

Source§

impl<I, S> StructuralPartialEq for Stateful<I, S>

Auto Trait Implementations§

§

impl<I, S> Freeze for Stateful<I, S>
where I: Freeze, S: Freeze,

§

impl<I, S> RefUnwindSafe for Stateful<I, S>

§

impl<I, S> Send for Stateful<I, S>
where I: Send, S: Send,

§

impl<I, S> Sync for Stateful<I, S>
where I: Sync, S: Sync,

§

impl<I, S> Unpin for Stateful<I, S>
where I: Unpin, S: Unpin,

§

impl<I, S> UnwindSafe for Stateful<I, S>
where I: UnwindSafe, S: 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, 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.