Cleanups for tree example of splitting borrows

pull/443/head
matt rice 8 months ago
parent 6bc2415218
commit a3db3f59d9

@ -215,7 +215,9 @@ pub struct IterMut<'a, T: 'a>(VecDeque<NodeIterMut<'a, T>>);
impl<T> Tree<T> { impl<T> Tree<T> {
pub fn iter_mut(&mut self) -> IterMut<T> { pub fn iter_mut(&mut self) -> IterMut<T> {
let mut deque = VecDeque::new(); let mut deque = VecDeque::new();
self.root.as_mut().map(|root| deque.push_front(root.iter_mut())); if let Some(root) = self.root.as_mut() {
deque.push_front(root.iter_mut());
}
IterMut(deque) IterMut(deque)
} }
} }
@ -224,42 +226,33 @@ impl<T> Node<T> {
pub fn iter_mut(&mut self) -> NodeIterMut<T> { pub fn iter_mut(&mut self) -> NodeIterMut<T> {
NodeIterMut { NodeIterMut {
elem: Some(&mut self.elem), elem: Some(&mut self.elem),
left: self.left.as_mut().map(|node| &mut **node), left: self.left.as_deref_mut(),
right: self.right.as_mut().map(|node| &mut **node), right: self.right.as_deref_mut(),
} }
} }
} }
impl<'a, T> Iterator for NodeIterMut<'a, T> { impl<'a, T> Iterator for NodeIterMut<'a, T> {
type Item = State<'a, T>; type Item = State<'a, T>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.left.take() { self.left.take().map(State::Node).or_else(|| {
Some(node) => Some(State::Node(node)), self.elem
None => match self.elem.take() { .take()
Some(elem) => Some(State::Elem(elem)), .map(State::Elem)
None => match self.right.take() { .or_else(|| self.right.take().map(State::Node))
Some(node) => Some(State::Node(node)), })
None => None,
}
}
}
} }
} }
impl<'a, T> DoubleEndedIterator for NodeIterMut<'a, T> { impl<'a, T> DoubleEndedIterator for NodeIterMut<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
match self.right.take() { self.right.take().map(State::Node).or_else(|| {
Some(node) => Some(State::Node(node)), self.elem
None => match self.elem.take() { .take()
Some(elem) => Some(State::Elem(elem)), .map(State::Elem)
None => match self.left.take() { .or_else(|| self.left.take().map(State::Node))
Some(node) => Some(State::Node(node)), })
None => None,
}
}
}
} }
} }
@ -267,10 +260,12 @@ impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T; type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
match self.0.front_mut().and_then(|node_it| node_it.next()) { match self.0.front_mut().and_then(Iterator::next) {
Some(State::Elem(elem)) => return Some(elem), Some(State::Elem(elem)) => return Some(elem),
Some(State::Node(node)) => self.0.push_front(node.iter_mut()), Some(State::Node(node)) => self.0.push_front(node.iter_mut()),
None => if let None = self.0.pop_front() { return None }, None => {
self.0.pop_front()?;
}
} }
} }
} }
@ -279,10 +274,12 @@ impl<'a, T> Iterator for IterMut<'a, T> {
impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
loop { loop {
match self.0.back_mut().and_then(|node_it| node_it.next_back()) { match self.0.back_mut().and_then(DoubleEndedIterator::next_back) {
Some(State::Elem(elem)) => return Some(elem), Some(State::Elem(elem)) => return Some(elem),
Some(State::Node(node)) => self.0.push_back(node.iter_mut()), Some(State::Node(node)) => self.0.push_back(node.iter_mut()),
None => if let None = self.0.pop_back() { return None }, None => {
self.0.pop_back()?;
}
} }
} }
} }

Loading…
Cancel
Save