diff --git a/src/borrow-splitting.md b/src/borrow-splitting.md index 08b6d0d4..1fed19db 100644 --- a/src/borrow-splitting.md +++ b/src/borrow-splitting.md @@ -215,7 +215,9 @@ pub struct IterMut<'a, T: 'a>(VecDeque>); impl Tree { pub fn iter_mut(&mut self) -> IterMut { 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) } } @@ -224,42 +226,33 @@ impl Node { pub fn iter_mut(&mut self) -> NodeIterMut { NodeIterMut { elem: Some(&mut self.elem), - left: self.left.as_mut().map(|node| &mut **node), - right: self.right.as_mut().map(|node| &mut **node), + left: self.left.as_deref_mut(), + right: self.right.as_deref_mut(), } } } - impl<'a, T> Iterator for NodeIterMut<'a, T> { type Item = State<'a, T>; fn next(&mut self) -> Option { - match self.left.take() { - Some(node) => Some(State::Node(node)), - None => match self.elem.take() { - Some(elem) => Some(State::Elem(elem)), - None => match self.right.take() { - Some(node) => Some(State::Node(node)), - None => None, - } - } - } + self.left.take().map(State::Node).or_else(|| { + self.elem + .take() + .map(State::Elem) + .or_else(|| self.right.take().map(State::Node)) + }) } } impl<'a, T> DoubleEndedIterator for NodeIterMut<'a, T> { fn next_back(&mut self) -> Option { - match self.right.take() { - Some(node) => Some(State::Node(node)), - None => match self.elem.take() { - Some(elem) => Some(State::Elem(elem)), - None => match self.left.take() { - Some(node) => Some(State::Node(node)), - None => None, - } - } - } + self.right.take().map(State::Node).or_else(|| { + self.elem + .take() + .map(State::Elem) + .or_else(|| self.left.take().map(State::Node)) + }) } } @@ -267,10 +260,12 @@ impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option { 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::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> { fn next_back(&mut self) -> Option { 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::Node(node)) => self.0.push_back(node.iter_mut()), - None => if let None = self.0.pop_back() { return None }, + None => { + self.0.pop_back()?; + } } } }