From 0cabc5c7fd1849fca9caf5668f6370e4c9e3ad4e Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 23 Jun 2017 18:51:46 -0400 Subject: [PATCH] Replace iterator struct with anonymous return iterator types. Similar to https://github.com/servo/servo/pull/17488. --- components/script/dom/htmlcollection.rs | 36 +++++-------------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/components/script/dom/htmlcollection.rs b/components/script/dom/htmlcollection.rs index 814ea766f53..6cc3dbb8dc6 100644 --- a/components/script/dom/htmlcollection.rs +++ b/components/script/dom/htmlcollection.rs @@ -224,11 +224,9 @@ impl HTMLCollection { pub fn elements_iter_after<'a>(&'a self, after: &'a Node) -> impl Iterator> + 'a { // Iterate forwards from a node. - HTMLCollectionElementsIter { - node_iter: after.following_nodes(&self.root), - root: Root::from_ref(&self.root), - filter: &self.filter, - } + after.following_nodes(&self.root) + .filter_map(Root::downcast) + .filter(move |element| self.filter.filter(&element, &self.root)) } pub fn elements_iter<'a>(&'a self) -> impl Iterator> + 'a { @@ -238,11 +236,9 @@ impl HTMLCollection { pub fn elements_iter_before<'a>(&'a self, before: &'a Node) -> impl Iterator> + 'a { // Iterate backwards from a node. - HTMLCollectionElementsIter { - node_iter: before.preceding_nodes(&self.root), - root: Root::from_ref(&self.root), - filter: &self.filter, - } + before.preceding_nodes(&self.root) + .filter_map(Root::downcast) + .filter(move |element| self.filter.filter(&element, &self.root)) } pub fn root_node(&self) -> Root { @@ -250,26 +246,6 @@ impl HTMLCollection { } } -// TODO: Make this generic, and avoid code duplication -struct HTMLCollectionElementsIter<'a, I> { - node_iter: I, - root: Root, - filter: &'a Box, -} - -impl<'a, I: Iterator>> Iterator for HTMLCollectionElementsIter<'a, I> { - type Item = Root; - - fn next(&mut self) -> Option { - let filter = &self.filter; - let root = &self.root; - self.node_iter.by_ref() - .filter_map(Root::downcast) - .filter(|element| filter.filter(&element, root)) - .next() - } -} - impl HTMLCollectionMethods for HTMLCollection { // https://dom.spec.whatwg.org/#dom-htmlcollection-length fn Length(&self) -> u32 {