Implement iterator with far too many clones.
This commit is contained in:
parent
0bcc3d9dc6
commit
cfdcb7408a
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user