Skip to content

Commit

Permalink
Implement FusedIterator for all iterator types
Browse files Browse the repository at this point in the history
All iterator types provided by this crate traverses nodes or edges, and
they return `None` forever once the traversal is done.
For such iterators, it is recommended to implement `FusedIterator` trait
to make `iter.fuse()` efficient.
See rustdoc for `core::iter::FusedIterator`.
  • Loading branch information
lo48576 committed May 18, 2021
1 parent 806014f commit 9a21777
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ macro_rules! impl_node_iterator {
Some(node)
}
}

impl<'a, T> core::iter::FusedIterator for $name<'a, T> {}
};
}

Expand Down Expand Up @@ -124,6 +126,8 @@ impl<'a, T> Iterator for Descendants<'a, T> {
}
}

impl<'a, T> core::iter::FusedIterator for Descendants<'a, T> {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Indicator if the node is at a start or endpoint of the tree
pub enum NodeEdge {
Expand Down Expand Up @@ -194,6 +198,8 @@ impl<'a, T> Iterator for Traverse<'a, T> {
}
}

impl<'a, T> core::iter::FusedIterator for Traverse<'a, T> {}

#[derive(Clone)]
/// An iterator of the "sides" of a node visited during a depth-first pre-order traversal,
/// where nodes are visited end to start and children are visited in reverse insertion order.
Expand Down Expand Up @@ -247,3 +253,5 @@ impl<'a, T> Iterator for ReverseTraverse<'a, T> {
Some(next)
}
}

impl<'a, T> core::iter::FusedIterator for ReverseTraverse<'a, T> {}

0 comments on commit 9a21777

Please sign in to comment.