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 &str {
    type Decoded<'a> = &'a str;

    const TYPE_ID: u32 = 0;

    fn to_value<'a>(&self, builder: &'a mut ValueBuilder<'a>) -> Value<'a> {
        if self.len() <= 14 {
            let mut small_string = [0u8; 14];
            small_string[..self.len()].copy_from_slice(self.as_bytes());
            Value::new(
                ValueKind::SmallString(small_string, self.len() as u8),
                builder.arena(),
            )
        } else {
            Value::new(
                ValueKind::String(builder.arena_mut().store(self.as_bytes(), MemAlign::Bits8)),
                builder.arena(),
            )
        }
    }

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

fn decode_str_kind<'a>(kind: &'a ValueKind, arena: &'a Arena) -> Option<&'a str> {
    match kind {
        ValueKind::String(index) => arena
            .get(*index)
            .map(|s| unsafe { std::str::from_utf8_unchecked(s) }),
        ValueKind::SmallString(small_string, len) => {
            Some(unsafe { std::str::from_utf8_unchecked(&small_string[..*len as usize]) })
        }
        _ => None,
    }
}