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, U> Compare<U> for Stateful<I, S>where
I: Compare<U>,
impl<I, S, U> Compare<U> for Stateful<I, S>where I: Compare<U>,
source§fn compare(&self, other: U) -> CompareResult
fn compare(&self, other: U) -> CompareResult
Compares self to another value for equality
source§fn compare_no_case(&self, other: U) -> CompareResult
fn compare_no_case(&self, other: U) -> CompareResult
Compares self to another value for equality
independently of the case. Read more
source§impl<I, S> ExtendInto for Stateful<I, S>where
I: ExtendInto,
impl<I, S> ExtendInto for Stateful<I, S>where I: ExtendInto,
§type Item = <I as ExtendInto>::Item
type Item = <I as ExtendInto>::Item
The current input type is a sequence of that
Item
type. Read more§type Extender = <I as ExtendInto>::Extender
type Extender = <I as ExtendInto>::Extender
The type that will be produced
source§fn new_builder(&self) -> Self::Extender
fn new_builder(&self) -> Self::Extender
Create a new
Extend
of the correct typesource§fn extend_into(&self, extender: &mut Self::Extender)
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>,
impl<I, S, T> FindSubstring<T> for Stateful<I, S>where I: FindSubstring<T>,
source§fn find_substring(&self, substr: T) -> Option<usize>
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,
impl<I, S> HexDisplay for Stateful<I, S>where I: HexDisplay,
source§impl<I, S> InputIsStreaming<false> for Stateful<I, S>where
I: InputIsStreaming<false>,
impl<I, S> InputIsStreaming<false> for Stateful<I, S>where I: InputIsStreaming<false>,
§type Streaming = Stateful<<I as InputIsStreaming<false>>::Streaming, S>
type Streaming = Stateful<<I as InputIsStreaming<false>>::Streaming, S>
Streaming counterpart Read more
source§fn into_complete(self) -> Self::Complete
fn into_complete(self) -> Self::Complete
Convert to complete counterpart
source§fn into_streaming(self) -> Self::Streaming
fn into_streaming(self) -> Self::Streaming
Convert to streaming counterpart
source§impl<I, S> InputIsStreaming<true> for Stateful<I, S>where
I: InputIsStreaming<true>,
impl<I, S> InputIsStreaming<true> for Stateful<I, S>where I: InputIsStreaming<true>,
source§fn into_complete(self) -> Self::Complete
fn into_complete(self) -> Self::Complete
Convert to complete counterpart
source§fn into_streaming(self) -> Self::Streaming
fn into_streaming(self) -> Self::Streaming
Convert to streaming counterpart
source§impl<I, S> InputIter for Stateful<I, S>where
I: InputIter,
impl<I, S> InputIter for Stateful<I, S>where I: InputIter,
§type Item = <I as InputIter>::Item
type Item = <I as InputIter>::Item
The current input type is a sequence of that
Item
type. Read more§type Iter = <I as InputIter>::Iter
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 charactersource§fn iter_indices(&self) -> Self::Iter
fn iter_indices(&self) -> Self::Iter
Returns an iterator over the elements and their byte offsets
source§fn iter_elements(&self) -> Self::IterElem
fn iter_elements(&self) -> Self::IterElem
Returns an iterator over the elements
source§impl<I, S> InputLength for Stateful<I, S>where
I: InputLength,
impl<I, S> InputLength for Stateful<I, S>where I: InputLength,
source§impl<I, S> InputTake for Stateful<I, S>where
I: InputTake,
S: Clone,
impl<I, S> InputTake for Stateful<I, S>where I: InputTake, S: Clone,
source§fn take_split(&self, count: usize) -> (Self, Self)
fn take_split(&self, count: usize) -> (Self, Self)
Split the stream at the
count
byte offset. panics if count > lengthsource§impl<I, S> InputTakeAtPosition for Stateful<I, S>where
I: InputTakeAtPosition,
S: Clone,
impl<I, S> InputTakeAtPosition for Stateful<I, S>where I: InputTakeAtPosition, S: Clone,
§type Item = <I as InputTakeAtPosition>::Item
type Item = <I as InputTakeAtPosition>::Item
The current input type is a sequence of that
Item
type. Read moresource§fn split_at_position_complete<P, E>(
&self,
predicate: P
) -> IResult<Self, Self, E>where
P: Fn(Self::Item) -> bool,
E: ParseError<Self>,
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>,
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>,
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>,
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,
impl<I, S> IntoOutput for Stateful<I, S>where I: IntoOutput,
§type Output = <I as IntoOutput>::Output
type Output = <I as IntoOutput>::Output
Output type
source§fn into_output(self) -> Self::Output
fn into_output(self) -> Self::Output
Convert an
Input
into an appropriate Output
typesource§fn merge_output(self, inner: Self::Output) -> Self
fn merge_output(self, inner: Self::Output) -> Self
Convert an
Output
type to be used as Input
source§impl<I: PartialEq, S: PartialEq> PartialEq<Stateful<I, S>> for Stateful<I, S>
impl<I: PartialEq, S: PartialEq> PartialEq<Stateful<I, S>> for Stateful<I, S>
impl<I: Copy, S: Copy> Copy for Stateful<I, S>
impl<I: Eq, S: Eq> Eq for Stateful<I, S>
impl<I, S> StructuralEq for Stateful<I, S>
impl<I, S> StructuralPartialEq for Stateful<I, S>
Auto Trait Implementations§
impl<I, S> RefUnwindSafe for Stateful<I, S>where I: RefUnwindSafe, S: RefUnwindSafe,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more