Skip to content

Commit c2bff14

Browse files
committed
Re-added Clone impls to all str iterators
1 parent 1b4cddc commit c2bff14

File tree

1 file changed

+82
-9
lines changed

1 file changed

+82
-9
lines changed

src/libcore/str/mod.rs

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,21 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
413413
}
414414
}
415415

416+
/// This macro generates a Clone impl for string pattern API
417+
/// wrapper types of the form X<'a, P>
418+
macro_rules! derive_pattern_clone {
419+
(clone $t:ident with |$s:ident| $e:expr) => {
420+
impl<'a, P: Pattern<'a>> Clone for $t<'a, P>
421+
where P::Searcher: Clone
422+
{
423+
fn clone(&self) -> Self {
424+
let $s = self;
425+
$e
426+
}
427+
}
428+
}
429+
}
430+
416431
/// This macro generates two public iterator structs
417432
/// wrapping an private internal one that makes use of the `Pattern` API.
418433
///
@@ -488,6 +503,15 @@ macro_rules! generate_pattern_iterators {
488503
}
489504
}
490505

506+
$(#[$common_stability_attribute])*
507+
impl<'a, P: Pattern<'a>> Clone for $forward_iterator<'a, P>
508+
where P::Searcher: Clone
509+
{
510+
fn clone(&self) -> Self {
511+
$forward_iterator(self.0.clone())
512+
}
513+
}
514+
491515
$(#[$reverse_iterator_attribute])*
492516
$(#[$common_stability_attribute])*
493517
pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
@@ -504,6 +528,15 @@ macro_rules! generate_pattern_iterators {
504528
}
505529
}
506530

531+
$(#[$common_stability_attribute])*
532+
impl<'a, P: Pattern<'a>> Clone for $reverse_iterator<'a, P>
533+
where P::Searcher: Clone
534+
{
535+
fn clone(&self) -> Self {
536+
$reverse_iterator(self.0.clone())
537+
}
538+
}
539+
507540
generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
508541
$forward_iterator,
509542
$reverse_iterator, $iterty);
@@ -540,6 +573,10 @@ macro_rules! generate_pattern_iterators {
540573
} => {}
541574
}
542575

576+
derive_pattern_clone!{
577+
clone SplitInternal
578+
with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
579+
}
543580
struct SplitInternal<'a, P: Pattern<'a>> {
544581
start: usize,
545582
end: usize,
@@ -634,6 +671,10 @@ generate_pattern_iterators! {
634671
delegate double ended;
635672
}
636673

674+
derive_pattern_clone!{
675+
clone SplitNInternal
676+
with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
677+
}
637678
struct SplitNInternal<'a, P: Pattern<'a>> {
638679
iter: SplitInternal<'a, P>,
639680
/// The number of splits remaining
@@ -676,6 +717,10 @@ generate_pattern_iterators! {
676717
delegate single ended;
677718
}
678719

720+
derive_pattern_clone!{
721+
clone MatchIndicesInternal
722+
with |s| MatchIndicesInternal(s.0.clone())
723+
}
679724
struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
680725

681726
impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
@@ -707,6 +752,10 @@ generate_pattern_iterators! {
707752
delegate double ended;
708753
}
709754

755+
derive_pattern_clone!{
756+
clone MatchesInternal
757+
with |s| MatchesInternal(s.0.clone())
758+
}
710759
struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher);
711760

712761
impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
@@ -745,6 +794,7 @@ generate_pattern_iterators! {
745794

746795
/// Return type of `str::lines()`
747796
#[stable(feature = "rust1", since = "1.0.0")]
797+
#[derive(Clone)]
748798
pub struct Lines<'a>(SplitTerminator<'a, char>);
749799

750800
#[stable(feature = "rust1", since = "1.0.0")]
@@ -772,7 +822,37 @@ impl<'a> DoubleEndedIterator for Lines<'a> {
772822

773823
/// Return type of `str::lines_any()`
774824
#[stable(feature = "rust1", since = "1.0.0")]
775-
pub struct LinesAny<'a>(Map<Lines<'a>, fn(&str) -> &str>);
825+
#[derive(Clone)]
826+
pub struct LinesAny<'a>(Map<Lines<'a>, LinesAnyMap>);
827+
828+
/// A nameable, clonable fn type
829+
#[derive(Clone)]
830+
struct LinesAnyMap;
831+
832+
impl<'a> Fn<(&'a str,)> for LinesAnyMap {
833+
#[inline]
834+
extern "rust-call" fn call(&self, (line,): (&'a str,)) -> &'a str {
835+
let l = line.len();
836+
if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
837+
else { line }
838+
}
839+
}
840+
841+
impl<'a> FnMut<(&'a str,)> for LinesAnyMap {
842+
#[inline]
843+
extern "rust-call" fn call_mut(&mut self, (line,): (&'a str,)) -> &'a str {
844+
Fn::call(&*self, (line,))
845+
}
846+
}
847+
848+
impl<'a> FnOnce<(&'a str,)> for LinesAnyMap {
849+
type Output = &'a str;
850+
851+
#[inline]
852+
extern "rust-call" fn call_once(self, (line,): (&'a str,)) -> &'a str {
853+
Fn::call(&self, (line,))
854+
}
855+
}
776856

777857
#[stable(feature = "rust1", since = "1.0.0")]
778858
impl<'a> Iterator for LinesAny<'a> {
@@ -1584,14 +1664,7 @@ impl StrExt for str {
15841664

15851665
#[inline]
15861666
fn lines_any(&self) -> LinesAny {
1587-
fn f(line: &str) -> &str {
1588-
let l = line.len();
1589-
if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
1590-
else { line }
1591-
}
1592-
1593-
let f: fn(&str) -> &str = f; // coerce to fn pointer
1594-
LinesAny(self.lines().map(f))
1667+
LinesAny(self.lines().map(LinesAnyMap))
15951668
}
15961669

15971670
#[inline]

0 commit comments

Comments
 (0)