HaystackIter

Trait HaystackIter 

Source
pub trait HaystackIter<'a>:
    Debug
    + Clone
    + Iterator<Item = <Self::Slice as HaystackSlice<'a>>::Item> {
    type Slice: HaystackSlice<'a>;

    // Required methods
    fn current_item(&self) -> Option<Self::Item>;
    fn current_index(&self) -> usize;
    fn whole_slice(&self) -> Self::Slice;
    fn remainder_as_slice(&self) -> Self::Slice;
    fn slice_with(&self, range: Range<usize>) -> Self::Slice;
    fn go_to(&mut self, index: usize);
}
Expand description

The main underlying trait for Haystack types, HaystackIter should be implemented on new types that understand slicing and iterating over a haystack that can be sliced into instances of Self::Slice.

For unicode-based haystacks like &str, the implementing type needs to be able to deal with the contained variable width code points.

This trait requires that implementors also implement Iterator<Item = Self::Slice::Item>. When Iterator::next is called, on a HaystackIter it should return the same value that previous calls to current_item have, before progressing the index to the next item. When the last item has been returned by next, the iterators should return None. Any future calls should avoid incrementing the index.

Additionally, HaystackIters should be cheap to clone and able to produce and restore an index representing the current position.

Although possible, there is no point implementing a HaystackIter that shares a Slice with another HaystackIter.

Required Associated Types§

Source

type Slice: HaystackSlice<'a>

The HaystackSlice returned by this type when slicing the underlying haystack. This type is usually also contained within the implementor used to create an instance via IntoHaystack.

Required Methods§

Source

fn current_item(&self) -> Option<Self::Item>

Returns the item currently being matched in the haystack. Repeatedly calling this method should return the same item, until progressed with Iterator::next.

Source

fn current_index(&self) -> usize

Returns the index of the current item in the original haystack. The returned value should be valid to pass to Self::go_to without causing a panic.

Source

fn whole_slice(&self) -> Self::Slice

Returns the underlying slice, as it was when this HaystackIter was created - representing the entire haystack being matched against.

Source

fn remainder_as_slice(&self) -> Self::Slice

Returns the remaining contents of this haystack, as a Slice. For slice based haystacks, this is can be implemented as &self.inner[self.index..].

Source

fn slice_with(&self, range: Range<usize>) -> Self::Slice

Slices the original haystack with the provided (half-open) range, used for retrieving values of capture groups.

Source

fn go_to(&mut self, index: usize)

Restores the index of the haystack to the provided one. This should only be called with indexes obtained by calling current_index on this HaystackIter.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> HaystackIter<'a> for ArcStrStack<'a>

Source§

type Slice = Substr

Source§

impl<'a> HaystackIter<'a> for BStrStack<'a>

Source§

type Slice = &'a BStr

Source§

impl<'a> HaystackIter<'a> for ByteStack<'a>

Source§

type Slice = &'a [u8]

Source§

impl<'a> HaystackIter<'a> for StrStack<'a>

Source§

type Slice = &'a str

Source§

impl<'a, B> HaystackIter<'a> for HipBytStack<'a, B>
where B: Backend,

Source§

type Slice = HipByt<'a, B>

Source§

impl<'a, B> HaystackIter<'a> for HipStrStack<'a, B>
where B: Backend,

Source§

type Slice = HipStr<'a, B>