Skip to content

Commit

Permalink
Replace iterator struct with anonymous return iterator types.
Browse files Browse the repository at this point in the history
Similar to #17488.
  • Loading branch information
frewsxcv committed Jun 23, 2017
1 parent daed0a5 commit 0cabc5c
Showing 1 changed file with 6 additions and 30 deletions.
36 changes: 6 additions & 30 deletions components/script/dom/htmlcollection.rs
Expand Up @@ -224,11 +224,9 @@ impl HTMLCollection {

pub fn elements_iter_after<'a>(&'a self, after: &'a Node) -> impl Iterator<Item=Root<Element>> + '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<Item=Root<Element>> + 'a {
Expand All @@ -238,38 +236,16 @@ impl HTMLCollection {

pub fn elements_iter_before<'a>(&'a self, before: &'a Node) -> impl Iterator<Item=Root<Element>> + '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<Node> {
Root::from_ref(&self.root)
}
}

// 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 {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length
fn Length(&self) -> u32 {
Expand Down

0 comments on commit 0cabc5c

Please sign in to comment.