mirror of https://github.com/smol-rs/vec-arena
Implement `FromIterator` (#6)
This commit is contained in:
parent
96ef84d50f
commit
aa516a8799
23
src/lib.rs
23
src/lib.rs
|
@ -628,6 +628,17 @@ impl<T> IntoIterator for Arena<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> iter::FromIterator<T> for Arena<T> {
|
||||
fn from_iter<U: IntoIterator<Item=T>>(iter: U) -> Arena<T> {
|
||||
let iter = iter.into_iter();
|
||||
let mut arena = Arena::with_capacity(iter.size_hint().0);
|
||||
for i in iter {
|
||||
arena.insert(i);
|
||||
}
|
||||
arena
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for IntoIter<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "IntoIter {{ ... }}")
|
||||
|
@ -906,4 +917,16 @@ mod tests {
|
|||
assert_eq!(*it.next().unwrap().1, 40 + d);
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_iter() {
|
||||
let arena: Arena<_> = [10, 20, 30, 40].iter().cloned().collect();
|
||||
|
||||
let mut it = arena.iter();
|
||||
assert_eq!(it.next(), Some((0, &10)));
|
||||
assert_eq!(it.next(), Some((1, &20)));
|
||||
assert_eq!(it.next(), Some((2, &30)));
|
||||
assert_eq!(it.next(), Some((3, &40)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue