From 6c7a6c5219be516dd1a0369dc1a0e39e4c2600f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 3 Dec 2018 16:18:01 +0100 Subject: [PATCH] feat: Implement DoubleEnded/ExactSizeIterator on DocumentIter --- src/database/database_view.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/database/database_view.rs b/src/database/database_view.rs index 08d19b624..b72a24bbb 100644 --- a/src/database/database_view.rs +++ b/src/database/database_view.rs @@ -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> { database_view: &'a DatabaseView<'a>, document_ids: I, @@ -73,6 +73,10 @@ where D: DeserializeOwned, { type Item = Result>; + fn size_hint(&self) -> (usize, Option) { + self.document_ids.size_hint() + } + fn next(&mut self) -> Option { match self.document_ids.next() { 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, +{ } + +impl<'a, D, I> DoubleEndedIterator for DocumentIter<'a, D, I> +where D: DeserializeOwned, + I: DoubleEndedIterator + Iterator, +{ + fn next_back(&mut self) -> Option { + match self.document_ids.next_back() { + Some(id) => Some(self.database_view.retrieve_document(id)), + None => None + } + } +}