Struct nom8::input::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§

Casts the input type to a byte slice
Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Compares self to another value for equality
Compares self to another value for equality independently of the case. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
The current input type is a sequence of that Item type. Read more
The type that will be produced
Create a new Extend of the correct type
Accumulate the input into an accumulator
Returns the byte position of the substring if it is found
Converts the value of self to a hex dump, returning the owned String.
Converts the value of self to a hex dump beginning at from address, returning the owned String.
Complete counterpart Read more
Streaming counterpart Read more
Convert to complete counterpart
Convert to streaming counterpart
Complete counterpart Read more
Streaming counterpart Read more
Convert to complete counterpart
Convert to streaming counterpart
The current input type is a sequence of that Item type. Read more
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
An iterator over the input type, producing the item
Returns an iterator over the elements and their byte offsets
Returns an iterator over the elements
Finds the byte position of the element
Get the byte offset from the element’s position in the stream
Calculates the input length, as indicated by its name, and the name of the trait itself
Returns a slice of count bytes. panics if count > length
Split the stream at the count byte offset. panics if count > length
The current input type is a sequence of that Item type. Read more
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
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
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
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
Output type
Convert an Input into an appropriate Output type
Convert an Output type to be used as Input
Number of indices input has advanced since start of parsing
Offset between the first byte of self and the first byte of the argument
Succeeds if parse() succeeded. The byte slice implementation will first convert it to a &str, then apply the parse() function
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Slices self according to the range argument

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.