Implement ExactSizeIterator for the other node types.
This commit is contained in:
parent
b7f7876706
commit
c578bb45af
@ -202,35 +202,14 @@ impl<'r, 's> AstNodeIter<'r, 's> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DocumentIter<'r, 's> {
|
multi_field_iter!(
|
||||||
zeroth_section: std::option::Iter<'r, Section<'s>>,
|
Document<'s>,
|
||||||
children: std::slice::Iter<'r, Heading<'s>>,
|
DocumentIter,
|
||||||
}
|
zeroth_section,
|
||||||
impl<'r, 's> Iterator for DocumentIter<'r, 's> {
|
std::option::Iter<'r, Section<'s>>,
|
||||||
type Item = AstNode<'r, 's>;
|
children,
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
std::slice::Iter<'r, Heading<'s>>
|
||||||
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!(
|
multi_field_iter!(
|
||||||
Heading<'s>,
|
Heading<'s>,
|
||||||
HeadingIter,
|
HeadingIter,
|
||||||
|
@ -11,8 +11,15 @@ macro_rules! children_iter {
|
|||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.next.next().map(Into::<AstNode>::into)
|
self.next.next().map(Into::<AstNode>::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
let size = self.next.len();
|
||||||
|
(size, Some(size))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'r, 's> ExactSizeIterator for $itertype<'r, 's> {}
|
||||||
|
|
||||||
impl<'r, 's> IntoIterator for &'r $astnodetype {
|
impl<'r, 's> IntoIterator for &'r $astnodetype {
|
||||||
type Item = AstNode<'r, 's>;
|
type Item = AstNode<'r, 's>;
|
||||||
|
|
||||||
@ -42,8 +49,14 @@ macro_rules! empty_iter {
|
|||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
(0, Some(0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'r, 's> ExactSizeIterator for $itertype<'r, 's> {}
|
||||||
|
|
||||||
impl<'r, 's> IntoIterator for &'r $astnodetype {
|
impl<'r, 's> IntoIterator for &'r $astnodetype {
|
||||||
type Item = AstNode<'r, 's>;
|
type Item = AstNode<'r, 's>;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user