@@ -413,6 +413,21 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
413
413
}
414
414
}
415
415
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
+
416
431
/// This macro generates two public iterator structs
417
432
/// wrapping an private internal one that makes use of the `Pattern` API.
418
433
///
@@ -488,6 +503,15 @@ macro_rules! generate_pattern_iterators {
488
503
}
489
504
}
490
505
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
+
491
515
$( #[ $reverse_iterator_attribute] ) *
492
516
$( #[ $common_stability_attribute] ) *
493
517
pub struct $reverse_iterator<' a, P : Pattern <' a>>( $internal_iterator<' a, P >) ;
@@ -504,6 +528,15 @@ macro_rules! generate_pattern_iterators {
504
528
}
505
529
}
506
530
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
+
507
540
generate_pattern_iterators!( $( $t) * with $( #[ $common_stability_attribute] ) * ,
508
541
$forward_iterator,
509
542
$reverse_iterator, $iterty) ;
@@ -540,6 +573,10 @@ macro_rules! generate_pattern_iterators {
540
573
} => { }
541
574
}
542
575
576
+ derive_pattern_clone ! {
577
+ clone SplitInternal
578
+ with |s| SplitInternal { matcher: s. matcher. clone( ) , ..* s }
579
+ }
543
580
struct SplitInternal < ' a , P : Pattern < ' a > > {
544
581
start : usize ,
545
582
end : usize ,
@@ -634,6 +671,10 @@ generate_pattern_iterators! {
634
671
delegate double ended;
635
672
}
636
673
674
+ derive_pattern_clone ! {
675
+ clone SplitNInternal
676
+ with |s| SplitNInternal { iter: s. iter. clone( ) , ..* s }
677
+ }
637
678
struct SplitNInternal < ' a , P : Pattern < ' a > > {
638
679
iter : SplitInternal < ' a , P > ,
639
680
/// The number of splits remaining
@@ -676,6 +717,10 @@ generate_pattern_iterators! {
676
717
delegate single ended;
677
718
}
678
719
720
+ derive_pattern_clone ! {
721
+ clone MatchIndicesInternal
722
+ with |s| MatchIndicesInternal ( s. 0 . clone( ) )
723
+ }
679
724
struct MatchIndicesInternal < ' a , P : Pattern < ' a > > ( P :: Searcher ) ;
680
725
681
726
impl < ' a , P : Pattern < ' a > > MatchIndicesInternal < ' a , P > {
@@ -707,6 +752,10 @@ generate_pattern_iterators! {
707
752
delegate double ended;
708
753
}
709
754
755
+ derive_pattern_clone ! {
756
+ clone MatchesInternal
757
+ with |s| MatchesInternal ( s. 0 . clone( ) )
758
+ }
710
759
struct MatchesInternal < ' a , P : Pattern < ' a > > ( P :: Searcher ) ;
711
760
712
761
impl < ' a , P : Pattern < ' a > > MatchesInternal < ' a , P > {
@@ -745,6 +794,7 @@ generate_pattern_iterators! {
745
794
746
795
/// Return type of `str::lines()`
747
796
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
797
+ #[ derive( Clone ) ]
748
798
pub struct Lines < ' a > ( SplitTerminator < ' a , char > ) ;
749
799
750
800
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -772,7 +822,37 @@ impl<'a> DoubleEndedIterator for Lines<'a> {
772
822
773
823
/// Return type of `str::lines_any()`
774
824
#[ 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
+ }
776
856
777
857
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
778
858
impl < ' a > Iterator for LinesAny < ' a > {
@@ -1584,14 +1664,7 @@ impl StrExt for str {
1584
1664
1585
1665
#[ inline]
1586
1666
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 ) )
1595
1668
}
1596
1669
1597
1670
#[ inline]
0 commit comments