@@ -18,7 +18,7 @@ use container::{Container, Mutable};
18
18
use cmp;
19
19
use cmp:: { Eq , TotalEq , TotalOrd , Ordering , Less , Equal , Greater } ;
20
20
use clone:: Clone ;
21
- use iterator:: { FromIterator , Iterator , IteratorUtil } ;
21
+ use iterator:: * ;
22
22
use kinds:: Copy ;
23
23
use libc:: c_void;
24
24
use num:: Zero ;
@@ -760,12 +760,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
760
760
}
761
761
#[ inline]
762
762
fn rev_iter( self ) -> VecRevIterator < ' self , T > {
763
- unsafe {
764
- let p = vec : : raw:: to_ptr( self ) ;
765
- VecRevIterator { ptr : p. offset( self . len( ) - 1 ) ,
766
- end : p. offset( -1 ) ,
767
- lifetime : cast:: transmute( p) }
768
- }
763
+ self . iter( ) . invert( )
769
764
}
770
765
771
766
/// Returns an iterator over the subslices of the vector which are
@@ -1717,13 +1712,9 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
1717
1712
}
1718
1713
}
1719
1714
1715
+ #[ inline]
1720
1716
fn mut_rev_iter ( self ) -> VecMutRevIterator < ' self , T > {
1721
- unsafe {
1722
- let p = vec:: raw :: to_mut_ptr( self ) ;
1723
- VecMutRevIterator { ptr: p. offset( self . len( ) - 1 ) ,
1724
- end: p. offset( -1 ) ,
1725
- lifetime: cast:: transmute( p) }
1726
- }
1717
+ self . mut_iter ( ) . invert ( )
1727
1718
}
1728
1719
1729
1720
/**
@@ -2083,53 +2074,61 @@ macro_rules! iterator {
2083
2074
priv lifetime: $elem // FIXME: #5922
2084
2075
}
2085
2076
};*/
2086
- ( impl $name: ident -> $elem: ty, $step: expr) => {
2087
- // could be implemented with &[T] with .slice(), but this avoids bounds checks
2077
+ ( impl $name: ident -> $elem: ty) => {
2088
2078
impl <' self , T > Iterator <$elem> for $name<' self , T > {
2089
2079
#[ inline]
2090
2080
fn next( & mut self ) -> Option <$elem> {
2081
+ // could be implemented with slices, but this avoids bounds checks
2091
2082
unsafe {
2092
2083
if self . ptr == self . end {
2093
2084
None
2094
2085
} else {
2095
2086
let old = self . ptr;
2096
- self . ptr = self . ptr. offset( $step ) ;
2087
+ self . ptr = self . ptr. offset( 1 ) ;
2097
2088
Some ( cast:: transmute( old) )
2098
2089
}
2099
2090
}
2100
2091
}
2101
2092
2102
2093
#[ inline]
2103
2094
fn size_hint( & self ) -> ( uint, Option <uint>) {
2104
- let diff = if $step > 0 {
2105
- ( self . end as uint) - ( self . ptr as uint)
2106
- } else {
2107
- ( self . ptr as uint) - ( self . end as uint)
2108
- } ;
2095
+ let diff = ( self . end as uint) - ( self . ptr as uint) ;
2109
2096
let exact = diff / size_of:: <$elem>( ) ;
2110
2097
( exact, Some ( exact) )
2111
2098
}
2112
2099
}
2113
2100
}
2114
2101
}
2115
2102
2103
+ macro_rules! double_ended_iterator {
2104
+ ( impl $name: ident -> $elem: ty) => {
2105
+ impl <' self , T > DoubleEndedIterator <$elem> for $name<' self , T > {
2106
+ #[ inline]
2107
+ fn next_back( & mut self ) -> Option <$elem> {
2108
+ // could be implemented with slices, but this avoids bounds checks
2109
+ unsafe {
2110
+ if self . end == self . ptr {
2111
+ None
2112
+ } else {
2113
+ self . end = self . end. offset( -1 ) ;
2114
+ Some ( cast:: transmute( self . end) )
2115
+ }
2116
+ }
2117
+ }
2118
+ }
2119
+ }
2120
+ }
2121
+
2116
2122
//iterator!{struct VecIterator -> *T, &'self T}
2117
2123
/// An iterator for iterating over a vector.
2118
2124
pub struct VecIterator < ' self , T > {
2119
2125
priv ptr: * T ,
2120
2126
priv end: * T ,
2121
2127
priv lifetime : & ' self T // FIXME: #5922
2122
2128
}
2123
- iterator!{ impl VecIterator -> & ' self T , 1 }
2124
-
2125
- //iterator!{struct VecRevIterator -> *T, &'self T}
2126
- /// An iterator for iterating over a vector in reverse.
2127
- pub struct VecRevIterator <' self , T > {
2128
- priv ptr: * T ,
2129
- priv end: * T ,
2130
- priv lifetime: & ' self T // FIXME: #5922
2131
- }
2132
- iterator!{ impl VecRevIterator -> & ' self T , -1 }
2129
+ iterator ! { impl VecIterator -> & ' self T }
2130
+ double_ended_iterator ! { impl VecIterator -> & ' self T }
2131
+ pub type VecRevIterator < ' self , T > = InvertIterator < & ' self T , VecIterator < ' self , T > > ;
2133
2132
2134
2133
//iterator!{struct VecMutIterator -> *mut T, &'self mut T}
2135
2134
/// An iterator for mutating the elements of a vector.
@@ -2138,16 +2137,9 @@ pub struct VecMutIterator<'self, T> {
2138
2137
priv end: * mut T ,
2139
2138
priv lifetime : & ' self mut T // FIXME: #5922
2140
2139
}
2141
- iterator!{ impl VecMutIterator -> & ' self mut T , 1 }
2142
-
2143
- //iterator!{struct VecMutRevIterator -> *mut T, &'self mut T}
2144
- /// An iterator for mutating the elements of a vector in reverse.
2145
- pub struct VecMutRevIterator <' self , T > {
2146
- priv ptr: * mut T ,
2147
- priv end: * mut T ,
2148
- priv lifetime: & ' self mut T // FIXME: #5922
2149
- }
2150
- iterator!{ impl VecMutRevIterator -> & ' self mut T , -1 }
2140
+ iterator ! { impl VecMutIterator -> & ' self mut T }
2141
+ double_ended_iterator ! { impl VecMutIterator -> & ' self mut T }
2142
+ pub type VecMutRevIterator < ' self , T > = InvertIterator < & ' self mut T , VecMutIterator < ' self , T > > ;
2151
2143
2152
2144
/// An iterator that moves out of a vector.
2153
2145
pub struct VecConsumeIterator < T > {
0 commit comments