Skip to content

Commit c9682d0

Browse files
committed
Getting rid of boxed iterator, fix documentation
1 parent f9cc5d6 commit c9682d0

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/re.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
// except according to those terms.
1010

1111
use std::borrow::Cow;
12+
use std::collections::HashMap;
1213
use std::fmt;
1314
use std::ops::Index;
1415
#[cfg(feature = "pattern")]
1516
use std::str::pattern::{Pattern, Searcher, SearchStep};
1617
use std::str::FromStr;
17-
use std::collections::HashMap;
1818
use std::sync::Arc;
1919

2020
use exec::{Exec, ExecBuilder};
@@ -837,16 +837,28 @@ impl NamedGroups {
837837
}
838838
}
839839

840-
fn iter<'n>(&'n self) -> Box<Iterator<Item=(&'n str, usize)> + 'n> {
840+
fn iter<'n>(&'n self) -> NamedGroupsIter<'n> {
841841
match *self {
842-
NamedGroups::Native(groups) => {
843-
Box::new(groups.iter().map(|&v| v))
844-
as Box<Iterator<Item=(&'n str, usize)> + 'n>
845-
},
846-
NamedGroups::Dynamic(ref groups) => {
847-
Box::new(groups.iter().map(|(s, i)| (&s[..], *i)))
848-
as Box<Iterator<Item=(&'n str, usize)> + 'n>
849-
},
842+
NamedGroups::Native(g) => NamedGroupsIter::Native(g.iter()),
843+
NamedGroups::Dynamic(ref g) => NamedGroupsIter::Dynamic(g.iter()),
844+
}
845+
}
846+
}
847+
848+
enum NamedGroupsIter<'n> {
849+
Native(::std::slice::Iter<'static, (&'static str, usize)>),
850+
Dynamic(::std::collections::hash_map::Iter<'n, String, usize>),
851+
}
852+
853+
impl<'n> Iterator for NamedGroupsIter<'n> {
854+
type Item = (&'n str, usize);
855+
856+
fn next(&mut self) -> Option<Self::Item> {
857+
match *self {
858+
NamedGroupsIter::Native(ref mut it) =>
859+
it.next().map(|&v| v),
860+
NamedGroupsIter::Dynamic(ref mut it) =>
861+
it.next().map(|(s, i)| (s.as_ref(), *i))
850862
}
851863
}
852864
}
@@ -994,6 +1006,7 @@ impl<'t> Index<&'t str> for Captures<'t> {
9941006
/// expression.
9951007
///
9961008
/// `'t` is the lifetime of the matched text.
1009+
/// `'c` is the lifetime of the captures.
9971010
pub struct SubCaptures<'c, 't: 'c> {
9981011
idx: usize,
9991012
caps: &'c Captures<'t>,
@@ -1017,7 +1030,7 @@ impl<'c, 't> Iterator for SubCaptures<'c, 't> {
10171030
///
10181031
/// Positions are byte indices in terms of the original string matched.
10191032
///
1020-
/// `'t` is the lifetime of the matched text.
1033+
/// `'c` is the lifetime of the captures.
10211034
pub struct SubCapturesPos<'c> {
10221035
idx: usize,
10231036
locs: &'c [Option<usize>]
@@ -1044,9 +1057,10 @@ impl<'c> Iterator for SubCapturesPos<'c> {
10441057
/// name and the value.
10451058
///
10461059
/// `'t` is the lifetime of the matched text.
1060+
/// `'c` is the lifetime of the captures.
10471061
pub struct SubCapturesNamed<'c, 't: 'c> {
10481062
caps: &'c Captures<'t>,
1049-
names: Box<Iterator<Item=(&'c str, usize)> + 'c>,
1063+
names: NamedGroupsIter<'c>,
10501064
}
10511065

10521066
impl<'c, 't: 'c> Iterator for SubCapturesNamed<'c, 't> {

0 commit comments

Comments
 (0)