@@ -23,7 +23,7 @@ impl<T: ?Sized + DefaultId> Default for Id<T> {
23
23
/// Helper trait to implement [`IntoIterator`] on [`Id`].
24
24
///
25
25
/// This should be implemented in exactly the same fashion as if you were
26
- /// implementing `IntoIterator` for your type normally.
26
+ /// just implementing `IntoIterator` for your type normally.
27
27
//
28
28
// Note that [`Box<T>` gets to cheat with regards moves][box-move], so
29
29
// `boxed.into_iter()` is possible, while `id.into_iter()` is not possible
@@ -108,6 +108,23 @@ where
108
108
}
109
109
}
110
110
111
+ /// Helper trait to implement [`FromIterator`] on [`Id`].
112
+ ///
113
+ /// This should be implemented in exactly the same fashion as if you were
114
+ /// just implementing `FromIterator` for your type normally.
115
+ pub trait IdFromIterator < T > : Sized {
116
+ /// Creates an `Id` from an iterator.
117
+ fn id_from_iter < I > ( iter : I ) -> Id < Self >
118
+ where
119
+ I : IntoIterator < Item = T > ;
120
+ }
121
+
122
+ impl < T , U : IdFromIterator < T > > FromIterator < T > for Id < U > {
123
+ #[ inline]
124
+ fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> Self {
125
+ U :: id_from_iter ( iter)
126
+ }
127
+ }
111
128
112
129
#[ cfg( test) ]
113
130
mod tests {
@@ -179,12 +196,18 @@ mod tests {
179
196
}
180
197
}
181
198
182
- impl < ' a > IntoIterator for Id < Collection > {
199
+ impl IdIntoIterator for Collection {
183
200
type Item = Id < NSObject > ;
184
201
type IntoIter = IntoIter ;
185
202
186
- fn into_iter ( self ) -> Self :: IntoIter {
187
- IntoIter ( self )
203
+ fn id_into_iter ( this : Id < Self > ) -> Self :: IntoIter {
204
+ IntoIter ( this)
205
+ }
206
+ }
207
+
208
+ impl IdFromIterator < Id < NSObject > > for Collection {
209
+ fn id_from_iter < I : IntoIterator < Item = Id < NSObject > > > ( _iter : I ) -> Id < Self > {
210
+ Collection :: default_id ( )
188
211
}
189
212
}
190
213
@@ -207,4 +230,10 @@ mod tests {
207
230
208
231
for _ in obj { }
209
232
}
233
+
234
+
235
+ #[ test]
236
+ fn test_from_iter ( ) {
237
+ let _: Id < Collection > = [ NSObject :: new ( ) ] . into_iter ( ) . collect ( ) ;
238
+ }
210
239
}
0 commit comments