Implement iterator with far too many clones.

This commit is contained in:
Tom Alexander 2022-12-11 01:04:51 -05:00
parent 0bcc3d9dc6
commit cfdcb7408a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -73,3 +73,25 @@ impl<T: Debug> List<T> {
}
}
}
pub struct NodeIter<T> {
position: Option<Rc<Node<T>>>,
}
impl<T> Iterator for NodeIter<T> {
// TODO: This would be a lot better if it returned borrows instead of clones of Rc
type Item = Rc<Node<T>>;
fn next(&mut self) -> Option<Self::Item> {
let (return_value, next_position) = match &self.position {
None => return None,
Some(rc_node) => {
let next_position = rc_node.parent.clone();
let return_value = rc_node.clone();
(return_value, next_position)
}
};
self.position = next_position;
Some(return_value)
}
}