diff --git a/src/ext.rs b/src/ext.rs index c0161b1..d58b3f4 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -18,11 +18,11 @@ use tokio::{ use futures::future::Future; #[derive(Debug)] -pub struct GroupIter(std::iter::Fuse, Vec, usize); +pub struct GroupIter>(std::iter::Fuse, Vec, usize, PhantomData); -impl, T> Iterator for GroupIter +impl>, I: Iterator, T> Iterator for GroupIter { - type Item = Box<[T]>; + type Item = U; fn next(&mut self) -> Option { if self.1.len() == 0 { @@ -48,18 +48,39 @@ impl, T> std::iter::FusedIterator for GroupIter{} pub trait GroupIterExt: Sized { + /// Group this iterator to return a constructed `U` of every `n` items. + /// + /// # Notes + /// If there isn't `n` items left in the iterator, then the rest is returned. + fn group_into(self, n: usize) -> GroupIter + where U: From>; + + + /// Group this iterator to return a `Vec` of every `n` items. + /// + /// # Notes + /// If there isn't `n` items left in the iterator, then the rest is returned. + #[inline] fn group(self, n: usize) -> GroupIter> + { + self.group_into(n) + } + /// Group this iterator to return a boxed slice of every `n` items. /// /// # Notes /// If there isn't `n` items left in the iterator, then the rest is returned. - fn group(self, n: usize) -> GroupIter; + #[inline] fn group_into_boxed_slice(self, n: usize) -> GroupIter + { + self.group_into(n) + } } impl GroupIterExt<::IntoIter, ::Item> for T { - fn group(self, every: usize) -> GroupIter<::IntoIter, ::Item> + fn group_into(self, every: usize) -> GroupIter<::IntoIter, ::Item, U> + where U: From::Item>> { - GroupIter(self.into_iter().fuse(), Vec::with_capacity(every), every) + GroupIter(self.into_iter().fuse(), Vec::with_capacity(every), every, PhantomData) } }