bumpish 0.2.0

A set of collections using bump allocations
Documentation
use super::bucket::{Bucket, BucketPtr};
use super::inner::Core;
use core::fmt::{self, Debug};

pub enum Entry<'a, K, V> {
    /// Existing slot with equivalent key.
    Occupied(OccupiedEntry<'a, K, V>),

    /// Vacant slot (no equivalent key in the map).
    Vacant(VacantEntry<'a, K, V>),
}

impl<'a, K, V> Entry<'a, K, V> {
    pub fn or_insert(self, value: V) -> &'a V {
        self.or_insert_with(|| value)
    }

    pub fn or_insert_with<F>(self, with: F) -> &'a V
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(entry) => entry.into_ref(),
            Entry::Vacant(entry) => entry.insert(with()),
        }
    }

    pub fn or_insert_with_key<F>(self, with_key: F) -> &'a V
    where
        F: FnOnce(&K) -> V,
    {
        match self {
            Entry::Occupied(entry) => entry.into_ref(),
            Entry::Vacant(entry) => {
                let value = with_key(entry.key());
                entry.insert(value)
            }
        }
    }

    pub fn key(&self) -> &K {
        match self {
            Entry::Occupied(entry) => entry.key(),
            Entry::Vacant(entry) => entry.key(),
        }
    }
}

impl<'a, K, V: Default> Entry<'a, K, V> {
    pub fn or_default(self) -> &'a V {
        self.or_insert_with(|| Default::default())
    }
}

impl<K: Debug, V: Debug> Debug for Entry<'_, K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Entry::Vacant(v) => f.debug_tuple("Entry").field(v).finish(),
            Entry::Occupied(o) => f.debug_tuple("Entry").field(o).finish(),
        }
    }
}

pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
    bucket: &'a Bucket<K, V>,
}

impl<'a, K, V> OccupiedEntry<'a, K, V> {
    pub(super) unsafe fn new(bucket_ptr: BucketPtr<K, V>) -> Self {
        Self {
            bucket: unsafe { bucket_ptr.as_ref() },
        }
    }

    #[inline]
    pub fn key(&self) -> &K {
        &self.bucket.key
    }

    #[inline]
    pub fn get(&self) -> &V {
        &self.bucket.value
    }

    #[inline]
    pub fn into_ref(self) -> &'a V {
        self.bucket.as_tuple().1
    }
}

impl<K: Debug, V: Debug> Debug for OccupiedEntry<'_, K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OccupiedEntry")
            .field("key", self.key())
            .field("value", self.get())
            .finish_non_exhaustive()
    }
}

pub struct VacantEntry<'a, K, V> {
    hash: u64,
    key: K,
    core: &'a Core<K, V>,
}

impl<'a, K, V> VacantEntry<'a, K, V> {
    pub(super) fn new(hash: u64, key: K, core: &'a Core<K, V>) -> Self {
        Self { hash, key, core }
    }

    #[inline]
    pub fn key(&self) -> &K {
        &self.key
    }

    #[inline]
    pub fn into_key(self) -> K {
        self.key
    }

    pub fn insert(self, value: V) -> &'a V {
        let bucket_ptr = self.core.insert_unique(self.hash, self.key, value);
        let bucket = unsafe { bucket_ptr.as_ref() };
        &bucket.value
    }

    pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
        let bucket_ptr = self.core.insert_unique(self.hash, self.key, value);
        unsafe { OccupiedEntry::new(bucket_ptr) }
    }
}

impl<K: Debug, V: Debug> Debug for VacantEntry<'_, K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VacantEntry")
            .field("key", self.key())
            .finish_non_exhaustive()
    }
}