Struct Stateful

struct Stateful<I, S> { ... }

Thread global state through your parsers

Use cases

Example

# use std::cell::Cell;
# use winnow::prelude::*;
# use winnow::stream::Stateful;
# use winnow::ascii::alpha1;
# type Error = ();

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

impl<'s> State<'s> {
    fn count(&mut self) {
        *self.0 += 1;
    }
}

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

fn word<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> {
  i.state.count();
  alpha1.parse_next(i)
}

let data = "Hello";
let mut state = 0;
let input = Stream { input: data, state: State(&mut state) };
let output = word.parse(input).unwrap();
assert_eq!(state, 1);

Fields

input: I

Inner input being wrapped in state

state: S

User-provided state

Implementations

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

fn as_bstr(self: &Self) -> &[u8]

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

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

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

fn as_ref(self: &Self) -> &I

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

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

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

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

fn previous_token_end(self: &Self) -> usize
fn current_token_start(self: &Self) -> usize

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

fn offset_from(self: &Self, other: &<Stateful<I, S> as Stream>::Checkpoint) -> usize

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

fn offset_from(self: &Self, start: &Self) -> usize

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

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

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

fn slice_len(self: &Self) -> usize

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

fn complete(self: &mut Self) -> <Self as >::PartialState
fn restore_partial(self: &mut Self, state: <Self as >::PartialState)
fn is_partial_supported() -> bool
fn is_partial(self: &Self) -> bool

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

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

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

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

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

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

fn update_slice(self: Self, inner: <Self as >::Slice) -> Self

impl<I, S, T> FindSlice for Stateful<I, S>

fn find_slice(self: &Self, substr: T) -> Option<Range<usize>>

impl<I, S, U> Compare for Stateful<I, S>

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

impl<I: $crate::clone::Clone, S: $crate::clone::Clone> Clone for Stateful<I, S>

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

impl<I: $crate::cmp::Eq, S: $crate::cmp::Eq> Eq for Stateful<I, S>

impl<I: $crate::cmp::PartialEq, S: $crate::cmp::PartialEq> PartialEq for Stateful<I, S>

fn eq(self: &Self, other: &Stateful<I, S>) -> bool

impl<I: $crate::default::Default, S: $crate::default::Default> Default for Stateful<I, S>

fn default() -> Stateful<I, S>

impl<I: $crate::fmt::Debug, S: $crate::fmt::Debug> Debug for Stateful<I, S>

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

impl<I: $crate::marker::Copy, S: $crate::marker::Copy> Copy for Stateful<I, S>

impl<I: Stream, S: crate::lib::std::fmt::Debug> Stream for Stateful<I, S>

fn iter_offsets(self: &Self) -> <Self as >::IterOffsets
fn eof_offset(self: &Self) -> usize
fn next_token(self: &mut Self) -> Option<<Self as >::Token>
fn peek_token(self: &Self) -> Option<<Self as >::Token>
fn offset_for<P>(self: &Self, predicate: P) -> Option<usize>
where
    P: Fn(<Self as >::Token) -> bool
fn offset_at(self: &Self, tokens: usize) -> Result<usize, Needed>
fn next_slice(self: &mut Self, offset: usize) -> <Self as >::Slice
unsafe fn next_slice_unchecked(self: &mut Self, offset: usize) -> <Self as >::Slice
fn peek_slice(self: &Self, offset: usize) -> <Self as >::Slice
unsafe fn peek_slice_unchecked(self: &Self, offset: usize) -> <Self as >::Slice
fn checkpoint(self: &Self) -> <Self as >::Checkpoint
fn reset(self: &mut Self, checkpoint: &<Self as >::Checkpoint)
fn raw(self: &Self) -> &dyn Debug
fn trace(self: &Self, f: &mut Formatter<'_>) -> Result

impl<I: crate::lib::std::fmt::Display, S> Display for Stateful<I, S>

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

impl<P, T> Receiver for Stateful<I, S>

impl<T> Any for Stateful<I, S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Stateful<I, S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Stateful<I, S>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Stateful<I, S>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Stateful<I, S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Stateful<I, S>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Stateful<I, S>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Stateful<I, S>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>