Implement ExactSizeIterator for DocumentIter.

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

View File

@ -447,17 +447,14 @@ fn _compare_document<'b, 's>(
let mut child_status = Vec::new(); let mut child_status = Vec::new();
let mut message = None; let mut message = None;
// compare_children_iter( compare_children_iter(
// source, source,
// emacs, emacs,
// rust.zeroth_section rust.into_iter(),
// .iter() &mut child_status,
// .map(Into::<AstNode>::into) &mut this_status,
// .chain(rust.children.iter().map(Into::<AstNode>::into)), &mut message,
// &mut child_status, )?;
// &mut this_status,
// &mut message,
// )?;
for diff in compare_properties!( for diff in compare_properties!(
source, source,

View File

@ -284,13 +284,7 @@ where
Ok(()) Ok(())
} }
pub(crate) fn compare_children_iter< pub(crate) fn compare_children_iter<'b, 's, RC, RI: Iterator<Item = RC> + ExactSizeIterator>(
'b,
's,
'x,
RC: 'x,
RI: Iterator<Item = &'x RC> + ExactSizeIterator,
>(
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
rust_children: RI, rust_children: RI,
@ -299,7 +293,7 @@ pub(crate) fn compare_children_iter<
message: &mut Option<String>, message: &mut Option<String>,
) -> Result<(), Box<dyn std::error::Error>> ) -> Result<(), Box<dyn std::error::Error>>
where where
AstNode<'b, 's>: From<&'x RC>, AstNode<'b, 's>: From<RC>,
{ {
let emacs_children = emacs.as_list()?; let emacs_children = emacs.as_list()?;
let emacs_children_length = emacs_children.len() - 2; let emacs_children_length = emacs_children.len() - 2;

View File

@ -202,14 +202,35 @@ impl<'r, 's> AstNodeIter<'r, 's> {
} }
} }
multi_field_iter!( pub struct DocumentIter<'r, 's> {
Document<'s>, zeroth_section: std::option::Iter<'r, Section<'s>>,
DocumentIter, children: std::slice::Iter<'r, Heading<'s>>,
zeroth_section, }
std::option::Iter<'r, Section<'s>>, impl<'r, 's> Iterator for DocumentIter<'r, 's> {
children, type Item = AstNode<'r, 's>;
std::slice::Iter<'r, Heading<'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!( multi_field_iter!(
Heading<'s>, Heading<'s>,
HeadingIter, HeadingIter,