Auto merge of #17503 - frewsxcv:frewsxcv-return-node-list-2, r=mbrubeck

Replace iterator struct with anonymous return iterator types.

Similar to https://github.com/servo/servo/pull/17488.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17503)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-06-23 19:01:33 -07:00 committed by GitHub
commit 1d045e5c3b

View file

@ -224,11 +224,9 @@ impl HTMLCollection {
pub fn elements_iter_after<'a>(&'a self, after: &'a Node) -> impl Iterator<Item=Root<Element>> + 'a { pub fn elements_iter_after<'a>(&'a self, after: &'a Node) -> impl Iterator<Item=Root<Element>> + 'a {
// Iterate forwards from a node. // Iterate forwards from a node.
HTMLCollectionElementsIter { after.following_nodes(&self.root)
node_iter: after.following_nodes(&self.root), .filter_map(Root::downcast)
root: Root::from_ref(&self.root), .filter(move |element| self.filter.filter(&element, &self.root))
filter: &self.filter,
}
} }
pub fn elements_iter<'a>(&'a self) -> impl Iterator<Item=Root<Element>> + 'a { pub fn elements_iter<'a>(&'a self) -> impl Iterator<Item=Root<Element>> + 'a {
@ -238,11 +236,9 @@ impl HTMLCollection {
pub fn elements_iter_before<'a>(&'a self, before: &'a Node) -> impl Iterator<Item=Root<Element>> + 'a { pub fn elements_iter_before<'a>(&'a self, before: &'a Node) -> impl Iterator<Item=Root<Element>> + 'a {
// Iterate backwards from a node. // Iterate backwards from a node.
HTMLCollectionElementsIter { before.preceding_nodes(&self.root)
node_iter: before.preceding_nodes(&self.root), .filter_map(Root::downcast)
root: Root::from_ref(&self.root), .filter(move |element| self.filter.filter(&element, &self.root))
filter: &self.filter,
}
} }
pub fn root_node(&self) -> Root<Node> { pub fn root_node(&self) -> Root<Node> {
@ -250,26 +246,6 @@ impl HTMLCollection {
} }
} }
// TODO: Make this generic, and avoid code duplication
struct HTMLCollectionElementsIter<'a, I> {
node_iter: I,
root: Root<Node>,
filter: &'a Box<CollectionFilter>,
}
impl<'a, I: Iterator<Item=Root<Node>>> Iterator for HTMLCollectionElementsIter<'a, I> {
type Item = Root<Element>;
fn next(&mut self) -> Option<Self::Item> {
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 { impl HTMLCollectionMethods for HTMLCollection {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length // https://dom.spec.whatwg.org/#dom-htmlcollection-length
fn Length(&self) -> u32 { fn Length(&self) -> u32 {