Skip to main content

Haystack

Trait Haystack 

Source
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§

Source

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§

Source

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.

Source

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

Returns the item last matched in the haystack without making any changes.

Source

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.

Source

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.

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 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 index on this Haystack.

Provided Methods§

Source

fn progress(&mut self)

Source

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

Source

fn rollback(&mut self, state: usize) -> &mut Self

Source

fn skip(&mut self, count: usize)

Source

fn reset(&mut self)

Source

fn is_start(&self) -> bool

Source

fn is_end(&self) -> bool

Source

fn is_line_start(&self) -> bool

Source

fn is_line_end(&self) -> bool

Source

fn is_crlf_start(&self) -> bool

Source

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.

Implementors§

Source§

impl<'a> Haystack<'a> for ArcStrStack<'a>

Source§

type Slice = Substr

Source§

impl<'a> Haystack<'a> for BStrStack<'a>

Source§

type Slice = &'a BStr

Source§

impl<'a> Haystack<'a> for ByteStack<'a>

Source§

type Slice = &'a [u8]

Source§

impl<'a> Haystack<'a> for StrStack<'a>

Source§

type Slice = &'a str

Source§

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

Source§

type Slice = HipByt<'a, B>

Source§

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

Source§

type Slice = HipStr<'a, B>