@@ -5,6 +5,7 @@ use rustc_serialize::{Encodable, Encoder};
55use std:: cmp:: { self , Ordering } ;
66use std:: fmt;
77use std:: hash:: { Hash , Hasher } ;
8+ use std:: iter;
89use std:: mem;
910use std:: ops:: Deref ;
1011use std:: ptr;
@@ -21,6 +22,10 @@ extern "C" {
2122/// the same contents can exist in the same context.
2223/// This means we can use pointer for both
2324/// equality comparisons and hashing.
25+ ///
26+ /// Unlike slices, The types contained in `List` are expected to be `Copy`
27+ /// and iterating over a `List` returns `T` instead of a reference.
28+ ///
2429/// Note: `Slice` was already taken by the `Ty`.
2530#[ repr( C ) ]
2631pub struct List < T > {
@@ -61,6 +66,15 @@ impl<T: Copy> List<T> {
6166 result
6267 }
6368 }
69+
70+ // If this method didn't exist, we would use `slice.iter` due to
71+ // deref coercion.
72+ //
73+ // This would be weird, as `self.into_iter` iterates over `T` directly.
74+ #[ inline( always) ]
75+ pub fn iter ( & self ) -> <& ' _ List < T > as IntoIterator >:: IntoIter {
76+ self . into_iter ( )
77+ }
6478}
6579
6680impl < T : fmt:: Debug > fmt:: Debug for List < T > {
@@ -128,12 +142,12 @@ impl<T> AsRef<[T]> for List<T> {
128142 }
129143}
130144
131- impl < ' a , T > IntoIterator for & ' a List < T > {
132- type Item = & ' a T ;
133- type IntoIter = < & ' a [ T ] as IntoIterator >:: IntoIter ;
145+ impl < ' a , T : Copy > IntoIterator for & ' a List < T > {
146+ type Item = T ;
147+ type IntoIter = iter :: Copied < < & ' a [ T ] as IntoIterator >:: IntoIter > ;
134148 #[ inline( always) ]
135149 fn into_iter ( self ) -> Self :: IntoIter {
136- self [ ..] . iter ( )
150+ self [ ..] . iter ( ) . copied ( )
137151 }
138152}
139153
0 commit comments