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§
Sourcetype Slice: HaystackSlice<'a>
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§
Sourcefn current_item(&self) -> Option<Self::Item>
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.
Sourcefn current_index(&self) -> usize
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.
Sourcefn whole_slice(&self) -> Self::Slice
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.
Sourcefn remainder_as_slice(&self) -> Self::Slice
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..].
Sourcefn slice_with(&self, range: Range<usize>) -> Self::Slice
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.
Sourcefn go_to(&mut self, index: usize)
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.