varmap 0.1.1

Fast, heterogeneous key-value maps for Rust. Store mixed types (integers, strings, bytes, custom structs) under a single key namespace, with compile-time keys, runtime strings, or enum variants. Arena-backed for zero-copy reads of strings and byte slices.
Documentation
use crate::*;

impl VarMapValue for &[u8] {
    type Decoded<'a> = &'a [u8];

    const TYPE_ID: u32 = 0;

    fn to_value<'a>(&self, builder: &'a mut ValueBuilder<'a>) -> Value<'a> {
        if self.len() <= 14 {
            let mut small_bytes = [0u8; 14];

            small_bytes[..self.len()].copy_from_slice(self);

            Value::new(
                ValueKind::SmallBytes(small_bytes, self.len() as u8),
                builder.arena(),
            )
        } else {
            Value::new(
                ValueKind::Bytes(builder.arena_mut().store(self, MemAlign::Bits8)),
                builder.arena(),
            )
        }
    }

    fn from_value<'a>(value: &Value<'a>) -> Option<&'a [u8]> {
        let kind = value.borrowed_kind()?;
        decode_bytes_kind(kind, value.arena())
    }
}

fn decode_bytes_kind<'a>(kind: &'a ValueKind, arena: &'a Arena) -> Option<&'a [u8]> {
    match kind {
        ValueKind::Bytes(index) => arena.get(*index),
        ValueKind::SmallBytes(small_bytes, len) => Some(&small_bytes[..*len as usize]),
        _ => None,
    }
}