use super::{LeafRef, SkipList};
use crate::allocator::Allocator;
use core::iter::FusedIterator;
pub struct Iter<L>(pub(super) Option<L>);
impl<L: LeafRef> Iterator for Iter<L> {
type Item = L;
fn next(&mut self) -> Option<L> {
let leaf = self.0.take();
self.0 = leaf.clone().and_then(SkipList::next);
leaf
}
}
impl<L: LeafRef> FusedIterator for Iter<L> {}
impl<L, A> IntoIterator for &SkipList<L, A>
where
L: LeafRef,
A: Allocator,
{
type Item = L;
type IntoIter = Iter<L>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct IntoIter<L, A>
where
L: LeafRef,
A: Allocator,
{
iter: Iter<L>,
_list: SkipList<L, A>,
}
impl<L, A> Iterator for IntoIter<L, A>
where
L: LeafRef,
A: Allocator,
{
type Item = L;
fn next(&mut self) -> Option<L> {
self.iter.next()
}
}
impl<L, A> FusedIterator for IntoIter<L, A>
where
L: LeafRef,
A: Allocator,
{
}
impl<L, A> IntoIterator for SkipList<L, A>
where
L: LeafRef,
A: Allocator,
{
type Item = L;
type IntoIter = IntoIter<L, A>;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
iter: Iter(self.first()),
_list: self,
}
}
}