@@ -65,9 +65,10 @@ use cmp;
65
65
use cmp:: Ord ;
66
66
use mem;
67
67
use num:: { ToPrimitive , Int } ;
68
- use ops:: Add ;
68
+ use ops:: { Add , Deref } ;
69
69
use option:: { Option , Some , None } ;
70
70
use uint;
71
+
71
72
#[ deprecated = "renamed to Extend" ] pub use self :: Extend as Extendable ;
72
73
73
74
/// Conversion from an `Iterator`
@@ -1021,6 +1022,44 @@ impl<T: Clone> MinMaxResult<T> {
1021
1022
}
1022
1023
}
1023
1024
1025
+ /// A trait for iterators that contain cloneable elements
1026
+ pub trait CloneIteratorExt < A > {
1027
+ /// Creates an iterator that clones the elements it yields. Useful for converting an
1028
+ /// Iterator<&T> to an Iterator<T>.
1029
+ fn cloned ( self ) -> Cloned < Self > ;
1030
+ }
1031
+
1032
+
1033
+ impl < A : Clone , D : Deref < A > , I : Iterator < D > > CloneIteratorExt < A > for I {
1034
+ fn cloned ( self ) -> Cloned < I > {
1035
+ Cloned { it : self }
1036
+ }
1037
+ }
1038
+
1039
+ /// An iterator that clones the elements of an underlying iterator
1040
+ pub struct Cloned < I > {
1041
+ it : I ,
1042
+ }
1043
+
1044
+ impl < A : Clone , D : Deref < A > , I : Iterator < D > > Iterator < A > for Cloned < I > {
1045
+ fn next ( & mut self ) -> Option < A > {
1046
+ self . it . next ( ) . cloned ( )
1047
+ }
1048
+
1049
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
1050
+ self . it . size_hint ( )
1051
+ }
1052
+ }
1053
+
1054
+ impl < A : Clone , D : Deref < A > , I : DoubleEndedIterator < D > >
1055
+ DoubleEndedIterator < A > for Cloned < I > {
1056
+ fn next_back ( & mut self ) -> Option < A > {
1057
+ self . it . next_back ( ) . cloned ( )
1058
+ }
1059
+ }
1060
+
1061
+ impl < A : Clone , D : Deref < A > , I : ExactSize < D > > ExactSize < A > for Cloned < I > { }
1062
+
1024
1063
/// A trait for iterators that are cloneable.
1025
1064
pub trait CloneableIterator {
1026
1065
/// Repeats an iterator endlessly
0 commit comments