Skip to content

Commit 8a81699

Browse files
authored
api: add missing implementations of core iterator traits
We add these for "good sense" reasons, although it's not clear how useful or beneficial they are in practice. PR #734
1 parent 0b15654 commit 8a81699

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/re_bytes.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::fmt;
4+
use std::iter::FusedIterator;
45
use std::ops::{Index, Range};
56
use std::str::FromStr;
67
use std::sync::Arc;
@@ -701,6 +702,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
701702
}
702703
}
703704

705+
impl<'r, 't> FusedIterator for Matches<'r, 't> {}
706+
704707
/// An iterator that yields all non-overlapping capture groups matching a
705708
/// particular regular expression.
706709
///
@@ -724,6 +727,8 @@ impl<'r, 't> Iterator for CaptureMatches<'r, 't> {
724727
}
725728
}
726729

730+
impl<'r, 't> FusedIterator for CaptureMatches<'r, 't> {}
731+
727732
/// Yields all substrings delimited by a regular expression match.
728733
///
729734
/// `'r` is the lifetime of the compiled regular expression and `'t` is the
@@ -757,6 +762,8 @@ impl<'r, 't> Iterator for Split<'r, 't> {
757762
}
758763
}
759764

765+
impl<'r, 't> FusedIterator for Split<'r, 't> {}
766+
760767
/// Yields at most `N` substrings delimited by a regular expression match.
761768
///
762769
/// The last substring will be whatever remains after splitting.
@@ -790,8 +797,14 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
790797
Some(&text[self.splits.last..])
791798
}
792799
}
800+
801+
fn size_hint(&self) -> (usize, Option<usize>) {
802+
(0, Some(self.n))
803+
}
793804
}
794805

806+
impl<'r, 't> FusedIterator for SplitN<'r, 't> {}
807+
795808
/// An iterator over the names of all possible captures.
796809
///
797810
/// `None` indicates an unnamed capture; the first element (capture 0, the
@@ -813,8 +826,16 @@ impl<'r> Iterator for CaptureNames<'r> {
813826
fn size_hint(&self) -> (usize, Option<usize>) {
814827
self.0.size_hint()
815828
}
829+
830+
fn count(self) -> usize {
831+
self.0.count()
832+
}
816833
}
817834

835+
impl<'r> ExactSizeIterator for CaptureNames<'r> {}
836+
837+
impl<'r> FusedIterator for CaptureNames<'r> {}
838+
818839
/// CaptureLocations is a low level representation of the raw offsets of each
819840
/// submatch.
820841
///
@@ -1073,6 +1094,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
10731094
}
10741095
}
10751096

1097+
impl<'c, 't> FusedIterator for SubCaptureMatches<'c, 't> {}
1098+
10761099
/// Replacer describes types that can be used to replace matches in a byte
10771100
/// string.
10781101
///

src/re_set.rs

+4
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ impl DoubleEndedIterator for SetMatchesIntoIter {
352352
}
353353
}
354354

355+
impl iter::FusedIterator for SetMatchesIntoIter {}
356+
355357
/// A borrowed iterator over the set of matches from a regex set.
356358
///
357359
/// The lifetime `'a` refers to the lifetime of a `SetMatches` value.
@@ -392,6 +394,8 @@ impl<'a> DoubleEndedIterator for SetMatchesIter<'a> {
392394
}
393395
}
394396

397+
impl<'a> iter::FusedIterator for SetMatchesIter<'a> {}
398+
395399
#[doc(hidden)]
396400
impl From<Exec> for RegexSet {
397401
fn from(exec: Exec) -> Self {

src/re_trait.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter::FusedIterator;
2+
13
/// Slot is a single saved capture location. Note that there are two slots for
24
/// every capture in a regular expression (one slot each for the start and end
35
/// of the capture).
@@ -73,6 +75,8 @@ impl<'c> Iterator for SubCapturesPosIter<'c> {
7375
}
7476
}
7577

78+
impl<'c> FusedIterator for SubCapturesPosIter<'c> {}
79+
7680
/// `RegularExpression` describes types that can implement regex searching.
7781
///
7882
/// This trait is my attempt at reducing code duplication and to standardize
@@ -205,6 +209,13 @@ where
205209
}
206210
}
207211

212+
impl<'t, R> FusedIterator for Matches<'t, R>
213+
where
214+
R: RegularExpression,
215+
R::Text: 't + AsRef<[u8]>,
216+
{
217+
}
218+
208219
/// An iterator over all non-overlapping successive leftmost-first matches with
209220
/// captures.
210221
pub struct CaptureMatches<'t, R>(Matches<'t, R>)
@@ -260,3 +271,10 @@ where
260271
Some(locs)
261272
}
262273
}
274+
275+
impl<'t, R> FusedIterator for CaptureMatches<'t, R>
276+
where
277+
R: RegularExpression,
278+
R::Text: 't + AsRef<[u8]>,
279+
{
280+
}

src/re_unicode.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::fmt;
4+
use std::iter::FusedIterator;
45
use std::ops::{Index, Range};
56
use std::str::FromStr;
67
use std::sync::Arc;
@@ -762,8 +763,16 @@ impl<'r> Iterator for CaptureNames<'r> {
762763
fn size_hint(&self) -> (usize, Option<usize>) {
763764
self.0.size_hint()
764765
}
766+
767+
fn count(self) -> usize {
768+
self.0.count()
769+
}
765770
}
766771

772+
impl<'r> ExactSizeIterator for CaptureNames<'r> {}
773+
774+
impl<'r> FusedIterator for CaptureNames<'r> {}
775+
767776
/// Yields all substrings delimited by a regular expression match.
768777
///
769778
/// `'r` is the lifetime of the compiled regular expression and `'t` is the
@@ -797,6 +806,8 @@ impl<'r, 't> Iterator for Split<'r, 't> {
797806
}
798807
}
799808

809+
impl<'r, 't> FusedIterator for Split<'r, 't> {}
810+
800811
/// Yields at most `N` substrings delimited by a regular expression match.
801812
///
802813
/// The last substring will be whatever remains after splitting.
@@ -830,8 +841,14 @@ impl<'r, 't> Iterator for SplitN<'r, 't> {
830841
Some(&text[self.splits.last..])
831842
}
832843
}
844+
845+
fn size_hint(&self) -> (usize, Option<usize>) {
846+
(0, Some(self.n))
847+
}
833848
}
834849

850+
impl<'r, 't> FusedIterator for SplitN<'r, 't> {}
851+
835852
/// CaptureLocations is a low level representation of the raw offsets of each
836853
/// submatch.
837854
///
@@ -1075,6 +1092,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
10751092
}
10761093
}
10771094

1095+
impl<'c, 't> FusedIterator for SubCaptureMatches<'c, 't> {}
1096+
10781097
/// An iterator that yields all non-overlapping capture groups matching a
10791098
/// particular regular expression.
10801099
///
@@ -1098,6 +1117,8 @@ impl<'r, 't> Iterator for CaptureMatches<'r, 't> {
10981117
}
10991118
}
11001119

1120+
impl<'r, 't> FusedIterator for CaptureMatches<'r, 't> {}
1121+
11011122
/// An iterator over all non-overlapping matches for a particular string.
11021123
///
11031124
/// The iterator yields a `Match` value. The iterator stops when no more
@@ -1116,6 +1137,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
11161137
}
11171138
}
11181139

1140+
impl<'r, 't> FusedIterator for Matches<'r, 't> {}
1141+
11191142
/// Replacer describes types that can be used to replace matches in a string.
11201143
///
11211144
/// In general, users of this crate shouldn't need to implement this trait,

0 commit comments

Comments
 (0)