From b6b88a7d786d108a8b9e4fdbc5f644253ef2a227 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 11 Dec 2022 01:07:16 -0500 Subject: [PATCH] Switch to using borrows instead of cloning the Rc during iteration. --- src/parser/list.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/parser/list.rs b/src/parser/list.rs index f65609f2..e15e8da2 100644 --- a/src/parser/list.rs +++ b/src/parser/list.rs @@ -74,20 +74,19 @@ impl List { } } -pub struct NodeIter { - position: Option>>, +pub struct NodeIter<'a, T> { + position: &'a Option>>, } -impl Iterator for NodeIter { - // TODO: This would be a lot better if it returned borrows instead of clones of Rc - type Item = Rc>; +impl<'a, T> Iterator for NodeIter<'a, T> { + type Item = &'a Rc>; fn next(&mut self) -> Option { 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(); + let next_position = &rc_node.parent; + let return_value = rc_node; (return_value, next_position) } };