@@ -5,6 +5,7 @@ use rustc_serialize::{Encodable, Encoder};
5
5
use std:: cmp:: { self , Ordering } ;
6
6
use std:: fmt;
7
7
use std:: hash:: { Hash , Hasher } ;
8
+ use std:: iter;
8
9
use std:: mem;
9
10
use std:: ops:: Deref ;
10
11
use std:: ptr;
@@ -21,6 +22,10 @@ extern "C" {
21
22
/// the same contents can exist in the same context.
22
23
/// This means we can use pointer for both
23
24
/// 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
+ ///
24
29
/// Note: `Slice` was already taken by the `Ty`.
25
30
#[ repr( C ) ]
26
31
pub struct List < T > {
@@ -61,6 +66,15 @@ impl<T: Copy> List<T> {
61
66
result
62
67
}
63
68
}
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
+ }
64
78
}
65
79
66
80
impl < T : fmt:: Debug > fmt:: Debug for List < T > {
@@ -128,12 +142,12 @@ impl<T> AsRef<[T]> for List<T> {
128
142
}
129
143
}
130
144
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 > ;
134
148
#[ inline( always) ]
135
149
fn into_iter ( self ) -> Self :: IntoIter {
136
- self [ ..] . iter ( )
150
+ self [ ..] . iter ( ) . copied ( )
137
151
}
138
152
}
139
153
0 commit comments