use core::iter;
use core::mem;
use crate::Node;
#[derive(Debug)]
pub struct AncestorsTraverser<T> {
next: Option<Node<T>>,
}
impl<T> Clone for AncestorsTraverser<T> {
#[inline]
fn clone(&self) -> Self {
Self {
next: self.next.clone(),
}
}
}
impl<T> AncestorsTraverser<T> {
#[inline]
#[must_use]
pub fn new(next: Option<Node<T>>) -> Self {
Self { next }
}
#[inline]
#[must_use]
pub fn peek(&self) -> Option<&Node<T>> {
self.next.as_ref()
}
}
impl<T> Iterator for AncestorsTraverser<T> {
type Item = Node<T>;
fn next(&mut self) -> Option<Self::Item> {
let next_of_next = self.next.as_ref()?.parent();
mem::replace(&mut self.next, next_of_next)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.next.as_ref().is_some() {
(1, None)
} else {
(0, Some(0))
}
}
}
impl<T> iter::FusedIterator for AncestorsTraverser<T> {}