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
sourceimpl<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>,
sourcefn compare(&self, other: U) -> CompareResult
fn compare(&self, other: U) -> CompareResult
Compares self to another value for equality
sourcefn 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
sourceimpl<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 moretype Extender = <I as ExtendInto>::Extender
type Extender = <I as ExtendInto>::Extender
The type that will be produced
sourcefn new_builder(&self) -> Self::Extender
fn new_builder(&self) -> Self::Extender
Create a new
Extend
of the correct typesourcefn extend_into(&self, extender: &mut Self::Extender)
fn extend_into(&self, extender: &mut Self::Extender)
Accumulate the input into an accumulator
sourceimpl<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>,
sourcefn 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
sourceimpl<I, S> HexDisplay for Stateful<I, S>where
I: HexDisplay,
impl<I, S> HexDisplay for Stateful<I, S>where
I: HexDisplay,
sourceimpl<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
sourcefn into_complete(self) -> Self::Complete
fn into_complete(self) -> Self::Complete
Convert to complete counterpart
sourcefn into_streaming(self) -> Self::Streaming
fn into_streaming(self) -> Self::Streaming
Convert to streaming counterpart
sourceimpl<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>,
type Complete = Stateful<<I as InputIsStreaming<true>>::Complete, S>
type Complete = Stateful<<I as InputIsStreaming<true>>::Complete, S>
Complete counterpart Read more
sourcefn into_complete(self) -> Self::Complete
fn into_complete(self) -> Self::Complete
Convert to complete counterpart
sourcefn into_streaming(self) -> Self::Streaming
fn into_streaming(self) -> Self::Streaming
Convert to streaming counterpart
sourceimpl<I, S> InputIter for Stateful<I, S>where
I: InputIter,
impl<I, S> InputIter for Stateful<I, S>where
I: InputIter,
sourcefn iter_indices(&self) -> Self::Iter
fn iter_indices(&self) -> Self::Iter
Returns an iterator over the elements and their byte offsets
sourcefn iter_elements(&self) -> Self::IterElem
fn iter_elements(&self) -> Self::IterElem
Returns an iterator over the elements
sourceimpl<I, S> InputLength for Stateful<I, S>where
I: InputLength,
impl<I, S> InputLength for Stateful<I, S>where
I: InputLength,
sourceimpl<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,
sourcefn 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 > lengthsourceimpl<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 moresourcefn 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
sourcefn 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
sourcefn 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
sourcefn 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
sourceimpl<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
sourcefn into_output(self) -> Self::Output
fn into_output(self) -> Self::Output
Convert an
Input
into an appropriate Output
typesourcefn merge_output(self, inner: Self::Output) -> Self
fn merge_output(self, inner: Self::Output) -> Self
Convert an
Output
type to be used as Input
sourceimpl<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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more