1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
use std::fmt; use std::convert; use std::slice; use std::vec; pub struct Collection<T> { items: Vec<T>, } impl<T> fmt::Debug for Collection<T> where T: fmt::Debug { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_list().entries(self.items.iter()).finish() } } impl<T> Collection<T> { pub fn new() -> Collection<T> { Collection { items: Vec::new() } } pub fn push(&mut self, item: T) { self.items.push(item) } pub fn iter<'a>(&'a self) -> slice::Iter<'a, T> { self.into_iter() } } impl<T> convert::AsRef<[T]> for Collection<T> { fn as_ref(&self) -> &[T] { &self.items } } impl<'a, T> IntoIterator for &'a Collection<T> { type IntoIter = slice::Iter<'a, T>; type Item = &'a T; fn into_iter(self) -> slice::Iter<'a, T> { self.items.iter() } } impl<T> IntoIterator for Collection<T> { type IntoIter = vec::IntoIter<T>; type Item = T; fn into_iter(self) -> vec::IntoIter<T> { self.items.into_iter() } } impl<T> Into<Vec<T>> for Collection<T> { fn into(self) -> Vec<T> { self.items } }