pub trait Haystack<'a>:
Debug
+ Clone
+ Iterator<Item = <Self::Slice as HaystackSlice<'a>>::Item> {
type Slice: HaystackSlice<'a>;
Show 17 methods
// Required methods
fn item(&self) -> Option<Self::Item>;
fn prev_item(&self) -> Option<Self::Item>;
fn index(&self) -> usize;
fn inner_slice(&self) -> Self::Slice;
fn remainder_as_slice(&self) -> Self::Slice;
fn go_to(&mut self, index: usize);
// Provided methods
fn progress(&mut self) { ... }
fn slice_with(&self, range: Range<usize>) -> Self::Slice { ... }
fn rollback(&mut self, state: usize) -> &mut Self { ... }
fn skip(&mut self, count: usize) { ... }
fn reset(&mut self) { ... }
fn is_start(&self) -> bool { ... }
fn is_end(&self) -> bool { ... }
fn is_line_start(&self) -> bool { ... }
fn is_line_end(&self) -> bool { ... }
fn is_crlf_start(&self) -> bool { ... }
fn is_crlf_end(&self) -> bool { ... }
}Expand description
A trait used to interface the haystack types use when matching of capturing against a
Regex, including tracking progression and slicing captures.
It is rare that users will have to interact with this trait, apart from Trait bounds. All public
methods will take an impl IntoHaystack<'a, H> as an argument.
Haystack is accompanied by another trait, HaystackItem, representing items that can be
matched against a Regex.
Haystacks are stateful and therefore can’t be matched against multiple times without being
reset first, or they will continue where the first pattern finished. They store
their state as a usize, which can be obtained via index and restored via
rollback. Additionally, Haystacks are cheap to clone, relying on shallow
clones or reference counting.
§Implementing
Haystack can be implemented for other types to allow searching, matching and capturing within
other string and byte slice-like types.
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 implementers also implement
Iterator<Item = Self::Slice::Item>. When Iterator::next is called, on a
Haystack it should return the same value that previous calls to 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, Haystacks 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 Haystack that shares a Slice with
another Haystack.
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 implementer used to create an instance via
IntoHaystack.
Required Methods§
Sourcefn item(&self) -> Option<Self::Item>
fn 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 prev_item(&self) -> Option<Self::Item>
fn prev_item(&self) -> Option<Self::Item>
Returns the item last matched in the haystack without making any changes.
Sourcefn index(&self) -> usize
fn 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 inner_slice(&self) -> Self::Slice
fn inner_slice(&self) -> Self::Slice
Returns the underlying slice, as it was when this Haystack 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..].
Provided Methods§
fn progress(&mut self)
fn slice_with(&self, range: Range<usize>) -> Self::Slice
fn rollback(&mut self, state: usize) -> &mut Self
fn skip(&mut self, count: usize)
fn reset(&mut self)
fn is_start(&self) -> bool
fn is_end(&self) -> bool
fn is_line_start(&self) -> bool
fn is_line_end(&self) -> bool
fn is_crlf_start(&self) -> bool
fn is_crlf_end(&self) -> bool
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.