Skip to content

Implement FusedIterator trait for the various iterators provided by this crate #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::iter::FusedIterator;
use std::ops::Range;
use std::{slice, vec};

Expand All @@ -7,6 +8,7 @@ use {Node, NodeId, NodeRef, Tree};
#[derive(Debug)]
pub struct IntoIter<T>(vec::IntoIter<Node<T>>);
impl<T> ExactSizeIterator for IntoIter<T> {}
impl<T> FusedIterator for IntoIter<T> {}
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -31,6 +33,7 @@ impl<'a, T: 'a> Clone for Values<'a, T> {
}
}
impl<'a, T: 'a> ExactSizeIterator for Values<'a, T> {}
impl<'a, T: 'a> FusedIterator for Values<'a, T> {}
impl<'a, T: 'a> Iterator for Values<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -50,6 +53,7 @@ impl<'a, T: 'a> DoubleEndedIterator for Values<'a, T> {
#[derive(Debug)]
pub struct ValuesMut<'a, T: 'a>(slice::IterMut<'a, Node<T>>);
impl<'a, T: 'a> ExactSizeIterator for ValuesMut<'a, T> {}
impl<'a, T: 'a> FusedIterator for ValuesMut<'a, T> {}
impl<'a, T: 'a> Iterator for ValuesMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -80,6 +84,7 @@ impl<'a, T: 'a> Clone for Nodes<'a, T> {
}
}
impl<'a, T: 'a> ExactSizeIterator for Nodes<'a, T> {}
impl<'a, T: 'a> FusedIterator for Nodes<'a, T> {}
impl<'a, T: 'a> Iterator for Nodes<'a, T> {
type Item = NodeRef<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -138,6 +143,7 @@ macro_rules! axis_iterators {
$i(self.0)
}
}
impl<'a, T: 'a> FusedIterator for $i<'a, T> {}
impl<'a, T: 'a> Iterator for $i<'a, T> {
type Item = NodeRef<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -181,6 +187,7 @@ impl<'a, T: 'a> Clone for Children<'a, T> {
}
}
}
impl<'a, T: 'a> FusedIterator for Children<'a, T> {}
impl<'a, T: 'a> Iterator for Children<'a, T> {
type Item = NodeRef<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -236,7 +243,7 @@ impl<'a, T: 'a> PartialEq for Edge<'a, T> {
/// Iterator which traverses a subtree.
#[derive(Debug)]
pub struct Traverse<'a, T: 'a> {
root: NodeRef<'a, T>,
root: Option<NodeRef<'a, T>>,
edge: Option<Edge<'a, T>>,
}
impl<'a, T: 'a> Clone for Traverse<'a, T> {
Expand All @@ -247,12 +254,15 @@ impl<'a, T: 'a> Clone for Traverse<'a, T> {
}
}
}
impl<'a, T: 'a> FusedIterator for Traverse<'a, T> {}
impl<'a, T: 'a> Iterator for Traverse<'a, T> {
type Item = Edge<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
match self.edge {
None => {
self.edge = Some(Edge::Open(self.root));
if let Some(root) = self.root {
self.edge = Some(Edge::Open(root));
}
}
Some(Edge::Open(node)) => {
if let Some(first_child) = node.first_child() {
Expand All @@ -262,7 +272,8 @@ impl<'a, T: 'a> Iterator for Traverse<'a, T> {
}
}
Some(Edge::Close(node)) => {
if node == self.root {
if node == self.root.unwrap() {
self.root = None;
self.edge = None;
} else if let Some(next_sibling) = node.next_sibling() {
self.edge = Some(Edge::Open(next_sibling));
Expand All @@ -283,6 +294,7 @@ impl<'a, T: 'a> Clone for Descendants<'a, T> {
Descendants(self.0.clone())
}
}
impl<'a, T: 'a> FusedIterator for Descendants<'a, T> {}
impl<'a, T: 'a> Iterator for Descendants<'a, T> {
type Item = NodeRef<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -332,7 +344,7 @@ impl<'a, T: 'a> NodeRef<'a, T> {
/// Returns an iterator which traverses the subtree starting at this node.
pub fn traverse(&self) -> Traverse<'a, T> {
Traverse {
root: *self,
root: Some(*self),
edge: None,
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ fn ancestors() {
);
}

#[test]
fn ancestors_fused() {
let tree = tree!('a' => { 'b' => { 'c' => { 'd' } } });
let d = tree
.root()
.last_child()
.unwrap()
.last_child()
.unwrap()
.last_child()
.unwrap();

let mut ancestors = d.ancestors();
assert_eq!(ancestors.by_ref().count(), 3);
assert_eq!(ancestors.next(), None);
}

#[test]
fn prev_siblings() {
let tree = tree!('a' => { 'b', 'c', 'd' });
Expand Down Expand Up @@ -101,6 +118,16 @@ fn children() {
);
}

#[test]
fn children_fused() {
let tree = tree!('a' => { 'b', 'c', 'd' });

let mut children = tree.root().children();

assert_eq!(children.by_ref().count(), 3);
assert_eq!(children.next(), None);
}

#[test]
fn children_rev() {
let tree = tree!('a' => { 'b', 'c', 'd' });
Expand Down Expand Up @@ -176,6 +203,16 @@ fn traverse() {
);
}

#[test]
fn traverse_fused() {
let tree = tree!('a' => { 'b' => { 'd', 'e' }, 'c' });

let mut traversal = tree.root().traverse();

assert_eq!(traversal.by_ref().count(), 2 * 5);
assert_eq!(traversal.next(), None);
}

#[test]
fn descendants() {
let tree = tree!('a' => { 'b' => { 'd', 'e' }, 'c' });
Expand All @@ -188,3 +225,13 @@ fn descendants() {

assert_eq!(&[&'a', &'b', &'d', &'e', &'c',], &descendants[..]);
}

#[test]
fn descendants_fused() {
let tree = tree!('a' => { 'b' => { 'd', 'e' }, 'c' });

let mut descendants = tree.root().descendants();

assert_eq!(descendants.by_ref().count(), 5);
assert_eq!(descendants.next(), None);
}
Loading