Skip to content

Commit

Permalink
api: add missing implementations of core iterator traits
Browse files Browse the repository at this point in the history
We add these for "good sense" reasons, although it's not
clear how useful or beneficial they are in practice.

PR #734
  • Loading branch information
lopopolo committed Dec 29, 2020
1 parent 0b15654 commit 8a81699
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/re_bytes.rs
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::iter::FusedIterator;
use std::ops::{Index, Range};
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -701,6 +702,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
}
}

impl<'r, 't> FusedIterator for Matches<'r, 't> {}

/// An iterator that yields all non-overlapping capture groups matching a
/// particular regular expression.
///
Expand All @@ -724,6 +727,8 @@ impl<'r, 't> Iterator for CaptureMatches<'r, 't> {
}
}

impl<'r, 't> FusedIterator for CaptureMatches<'r, 't> {}

/// Yields all substrings delimited by a regular expression match.
///
/// `'r` is the lifetime of the compiled regular expression and `'t` is the
Expand Down Expand Up @@ -757,6 +762,8 @@ impl<'r, 't> Iterator for Split<'r, 't> {
}
}

impl<'r, 't> FusedIterator for Split<'r, 't> {}

/// Yields at most `N` substrings delimited by a regular expression match.
///
/// The last substring will be whatever remains after splitting.
Expand Down Expand Up @@ -790,8 +797,14 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
Some(&text[self.splits.last..])
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.n))
}
}

impl<'r, 't> FusedIterator for SplitN<'r, 't> {}

/// An iterator over the names of all possible captures.
///
/// `None` indicates an unnamed capture; the first element (capture 0, the
Expand All @@ -813,8 +826,16 @@ impl<'r> Iterator for CaptureNames<'r> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}

fn count(self) -> usize {
self.0.count()
}
}

impl<'r> ExactSizeIterator for CaptureNames<'r> {}

impl<'r> FusedIterator for CaptureNames<'r> {}

/// CaptureLocations is a low level representation of the raw offsets of each
/// submatch.
///
Expand Down Expand Up @@ -1073,6 +1094,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
}
}

impl<'c, 't> FusedIterator for SubCaptureMatches<'c, 't> {}

/// Replacer describes types that can be used to replace matches in a byte
/// string.
///
Expand Down
4 changes: 4 additions & 0 deletions src/re_set.rs
Expand Up @@ -352,6 +352,8 @@ impl DoubleEndedIterator for SetMatchesIntoIter {
}
}

impl iter::FusedIterator for SetMatchesIntoIter {}

/// A borrowed iterator over the set of matches from a regex set.
///
/// The lifetime `'a` refers to the lifetime of a `SetMatches` value.
Expand Down Expand Up @@ -392,6 +394,8 @@ impl<'a> DoubleEndedIterator for SetMatchesIter<'a> {
}
}

impl<'a> iter::FusedIterator for SetMatchesIter<'a> {}

#[doc(hidden)]
impl From<Exec> for RegexSet {
fn from(exec: Exec) -> Self {
Expand Down
18 changes: 18 additions & 0 deletions src/re_trait.rs
@@ -1,3 +1,5 @@
use std::iter::FusedIterator;

/// Slot is a single saved capture location. Note that there are two slots for
/// every capture in a regular expression (one slot each for the start and end
/// of the capture).
Expand Down Expand Up @@ -73,6 +75,8 @@ impl<'c> Iterator for SubCapturesPosIter<'c> {
}
}

impl<'c> FusedIterator for SubCapturesPosIter<'c> {}

/// `RegularExpression` describes types that can implement regex searching.
///
/// This trait is my attempt at reducing code duplication and to standardize
Expand Down Expand Up @@ -205,6 +209,13 @@ where
}
}

impl<'t, R> FusedIterator for Matches<'t, R>
where
R: RegularExpression,
R::Text: 't + AsRef<[u8]>,
{
}

/// An iterator over all non-overlapping successive leftmost-first matches with
/// captures.
pub struct CaptureMatches<'t, R>(Matches<'t, R>)
Expand Down Expand Up @@ -260,3 +271,10 @@ where
Some(locs)
}
}

impl<'t, R> FusedIterator for CaptureMatches<'t, R>
where
R: RegularExpression,
R::Text: 't + AsRef<[u8]>,
{
}
23 changes: 23 additions & 0 deletions src/re_unicode.rs
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::iter::FusedIterator;
use std::ops::{Index, Range};
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -762,8 +763,16 @@ impl<'r> Iterator for CaptureNames<'r> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}

fn count(self) -> usize {
self.0.count()
}
}

impl<'r> ExactSizeIterator for CaptureNames<'r> {}

impl<'r> FusedIterator for CaptureNames<'r> {}

/// Yields all substrings delimited by a regular expression match.
///
/// `'r` is the lifetime of the compiled regular expression and `'t` is the
Expand Down Expand Up @@ -797,6 +806,8 @@ impl<'r, 't> Iterator for Split<'r, 't> {
}
}

impl<'r, 't> FusedIterator for Split<'r, 't> {}

/// Yields at most `N` substrings delimited by a regular expression match.
///
/// The last substring will be whatever remains after splitting.
Expand Down Expand Up @@ -830,8 +841,14 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
Some(&text[self.splits.last..])
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.n))
}
}

impl<'r, 't> FusedIterator for SplitN<'r, 't> {}

/// CaptureLocations is a low level representation of the raw offsets of each
/// submatch.
///
Expand Down Expand Up @@ -1075,6 +1092,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
}
}

impl<'c, 't> FusedIterator for SubCaptureMatches<'c, 't> {}

/// An iterator that yields all non-overlapping capture groups matching a
/// particular regular expression.
///
Expand All @@ -1098,6 +1117,8 @@ impl<'r, 't> Iterator for CaptureMatches<'r, 't> {
}
}

impl<'r, 't> FusedIterator for CaptureMatches<'r, 't> {}

/// An iterator over all non-overlapping matches for a particular string.
///
/// The iterator yields a `Match` value. The iterator stops when no more
Expand All @@ -1116,6 +1137,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
}
}

impl<'r, 't> FusedIterator for Matches<'r, 't> {}

/// Replacer describes types that can be used to replace matches in a string.
///
/// In general, users of this crate shouldn't need to implement this trait,
Expand Down

0 comments on commit 8a81699

Please sign in to comment.