Switch to using borrows instead of cloning the Rc during iteration.

This commit is contained in:
Tom Alexander 2022-12-11 01:07:16 -05:00
parent cfdcb7408a
commit b6b88a7d78
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 6 additions and 7 deletions

View File

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