Implement ExactSizeIterator for DocumentIter.

This commit is contained in:
Tom Alexander
2023-10-10 15:19:42 -04:00
parent a873794068
commit ee5ed17c20
3 changed files with 39 additions and 27 deletions

View File

@@ -202,14 +202,35 @@ impl<'r, 's> AstNodeIter<'r, 's> {
}
}
multi_field_iter!(
Document<'s>,
DocumentIter,
zeroth_section,
std::option::Iter<'r, Section<'s>>,
children,
std::slice::Iter<'r, Heading<'s>>
);
pub struct DocumentIter<'r, 's> {
zeroth_section: std::option::Iter<'r, Section<'s>>,
children: std::slice::Iter<'r, Heading<'s>>,
}
impl<'r, 's> Iterator for DocumentIter<'r, 's> {
type Item = AstNode<'r, 's>;
fn next(&mut self) -> Option<Self::Item> {
self.zeroth_section
.next()
.map(Into::<AstNode>::into)
.or_else(|| self.children.next().map(Into::<AstNode>::into))
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.zeroth_section.len() + self.children.len();
(size, Some(size))
}
}
impl<'r, 's> ExactSizeIterator for DocumentIter<'r, 's> {}
impl<'r, 's> IntoIterator for &'r Document<'s> {
type Item = AstNode<'r, 's>;
type IntoIter = DocumentIter<'r, 's>;
fn into_iter(self) -> Self::IntoIter {
DocumentIter {
zeroth_section: self.zeroth_section.iter(),
children: self.children.iter(),
}
}
}
multi_field_iter!(
Heading<'s>,
HeadingIter,