bumpish 0.2.0

A set of collections using bump allocations
Documentation
use super::bucket::Bucket;
use super::inner::{Core, Entries};
use crate::stk;
use crate::util::{impl_deiter, impl_iter};

pub struct Iter<'a, K, V> {
    iter: core::iter::Rev<stk::Iter<'a, Bucket<K, V>>>,
}

impl<'a, K, V> Iter<'a, K, V> {
    pub(super) fn new(entries: &'a Entries<K, V>) -> Self {
        Self {
            iter: entries.iter().rev(),
        }
    }
}

impl_iter!(Bucket::as_tuple, Iter<'a, K, V> => (&'a K, &'a V));
impl_deiter!(Bucket::as_tuple, Iter<'a, K, V>);

pub struct IterMut<'a, K, V> {
    iter: core::iter::Rev<stk::IterMut<'a, Bucket<K, V>>>,
}

impl<'a, K, V> IterMut<'a, K, V> {
    pub(super) fn new(entries: &'a mut Entries<K, V>) -> Self {
        Self {
            iter: entries.iter_mut().rev(),
        }
    }
}

impl_iter!(Bucket::as_tuple_mut, IterMut<'a, K, V> => (&'a K, &'a mut V));
impl_deiter!(Bucket::as_tuple_mut, IterMut<'a, K, V>);

pub struct Keys<'a, K, V> {
    iter: core::iter::Rev<stk::Iter<'a, Bucket<K, V>>>,
}

impl<'a, K, V> Keys<'a, K, V> {
    pub(super) fn new(entries: &'a Entries<K, V>) -> Self {
        Self {
            iter: entries.iter().rev(),
        }
    }
}

impl_iter!(Bucket::as_key, Keys<'a, K, V> => &'a K);
impl_deiter!(Bucket::as_key, Keys<'a, K, V>);

pub struct Values<'a, K, V> {
    iter: core::iter::Rev<stk::Iter<'a, Bucket<K, V>>>,
}

impl<'a, K, V> Values<'a, K, V> {
    pub(super) fn new(entries: &'a Entries<K, V>) -> Self {
        Self {
            iter: entries.iter().rev(),
        }
    }
}

impl_iter!(Bucket::as_value, Values<'a, K, V> => &'a V);
impl_deiter!(Bucket::as_value, Values<'a, K, V>);

pub struct ValuesMut<'a, K, V> {
    iter: core::iter::Rev<stk::IterMut<'a, Bucket<K, V>>>,
}

impl<'a, K, V> ValuesMut<'a, K, V> {
    pub(super) fn new(entries: &'a mut Entries<K, V>) -> Self {
        Self {
            iter: entries.iter_mut().rev(),
        }
    }
}

impl_iter!(Bucket::as_value_mut, ValuesMut<'a, K, V> => &'a mut V);
impl_deiter!(Bucket::as_value_mut, ValuesMut<'a, K, V>);

pub struct IntoIter<K, V> {
    iter: stk::IntoIter<Bucket<K, V>>,
}

impl<K, V> IntoIter<K, V> {
    pub(super) fn new(entries: Entries<K, V>) -> Self {
        Self {
            iter: entries.into_iter(),
        }
    }
}

impl_iter!(Bucket::into_tuple, IntoIter<K, V> => (K, V));

pub struct Drain<'a, K: Eq, V> {
    core: &'a mut Core<K, V>,
}

impl<'a, K: Eq, V> Drain<'a, K, V> {
    pub(super) fn new(core: &'a mut Core<K, V>) -> Self {
        Self { core }
    }
}

impl<K: Eq, V> core::iter::Iterator for Drain<'_, K, V> {
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        self.core.pop().map(Bucket::into_tuple)
    }
}

impl<K: Eq, V> core::ops::Drop for Drain<'_, K, V> {
    fn drop(&mut self) {
        while let Some(bucket) = self.core.pop() {
            drop(bucket);
        }
    }
}