feat: Implement DoubleEnded/ExactSizeIterator on DocumentIter

This commit is contained in:
Clément Renault 2018-12-03 16:18:01 +01:00
parent 6f8dbbde9a
commit 6c7a6c5219
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

@ -60,7 +60,7 @@ impl<'a> DatabaseView<'a> {
} }
} }
// TODO impl ExactSizeIterator, DoubleEndedIterator // TODO this is just an iter::Map !!!
pub struct DocumentIter<'a, D, I> { pub struct DocumentIter<'a, D, I> {
database_view: &'a DatabaseView<'a>, database_view: &'a DatabaseView<'a>,
document_ids: I, document_ids: I,
@ -73,6 +73,10 @@ where D: DeserializeOwned,
{ {
type Item = Result<D, Box<Error>>; type Item = Result<D, Box<Error>>;
fn size_hint(&self) -> (usize, Option<usize>) {
self.document_ids.size_hint()
}
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.document_ids.next() { match self.document_ids.next() {
Some(id) => Some(self.database_view.retrieve_document(id)), Some(id) => Some(self.database_view.retrieve_document(id)),
@ -80,3 +84,20 @@ where D: DeserializeOwned,
} }
} }
} }
impl<'a, D, I> ExactSizeIterator for DocumentIter<'a, D, I>
where D: DeserializeOwned,
I: ExactSizeIterator + Iterator<Item=DocumentId>,
{ }
impl<'a, D, I> DoubleEndedIterator for DocumentIter<'a, D, I>
where D: DeserializeOwned,
I: DoubleEndedIterator + Iterator<Item=DocumentId>,
{
fn next_back(&mut self) -> Option<Self::Item> {
match self.document_ids.next_back() {
Some(id) => Some(self.database_view.retrieve_document(id)),
None => None
}
}
}