turbocow 0.3.0-beta.2

Compact, clone-on-write vectors, strings, maps and sets with inline + referenced storage — a superset of ecow.
Documentation
use core::mem::MaybeUninit;

use hashbrown::HashMap;

/// A compact hash map that stores up to `N` key-value pairs inline and spills
/// to a heap-allocated [`HashMap`] when the inline capacity is exceeded.
///
/// For small maps (≤ `N` entries), all data lives on the stack with no heap
/// allocation.  Lookups use h2-accelerated linear scan.  When the map grows
/// beyond `N` entries it transparently spills to a [`hashbrown::HashMap`].
///
/// The default inline capacity is 8 entries.
///
/// # Examples
/// ```
/// use turbocow::SmallMap;
///
/// let mut map = SmallMap::<&str, i32>::new();
/// map.insert("hello", 1);
/// map.insert("world", 2);
/// assert_eq!(map.get("hello"), Some(&1));
/// assert_eq!(map.len(), 2);
/// ```
pub enum SmallMap<K, V, const N: usize = 8, S = hashbrown::DefaultHashBuilder> {
    /// Flat inline storage with h2 sidecar (up to `N` entries).
    Inline(InlineMap<K, V, N, S>),
    /// Heap-allocated hashbrown HashMap.
    Heap(HashMap<K, V, S>),
}

/// Flat inline storage for key-value pairs with h2 sidecar.
///
/// Entries and h2 bytes are stored in fixed-size arrays of capacity `N`.
/// All `N` h2 bytes are initialised to [`H2_EMPTY`](super::group::H2_EMPTY)
/// at construction time, ensuring SIMD reads are always safe.  When the map
/// reaches `N` entries it spills to a hashbrown `HashMap`.
pub struct InlineMap<K, V, const N: usize, S> {
    pub(super) entries: [MaybeUninit<(K, V)>; N],
    pub(super) h2_bytes: [u8; N],
    pub(super) len: usize,
    pub(super) hasher: S,
}

// ── Compile-time assertion ───────────────────────────────────────────────
// Intentional compile-time invariant guard; clippy flags the constant
// condition, but the assert documents the contract.
#[allow(clippy::assertions_on_constants)]
const _: () = assert!(8 > 0, "Default N must be non-zero");