| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! Sealed [`Key`] and [`KeyQuery`] traits routing [`HashMap<K, V>`] and | |
| //! [`HashSet<T>`] statically onto either [`RawU64Map`] (`KeyRepr::U64`) or | |
| //! [`RawBytesMap`] (`KeyRepr::Bytes`) with zero indirect `BLR` comparators and | |
| //! zero monomorphized probe loops. | |
| use core::fmt; | |
| use vx_alloc::alloc::AllocError; | |
| use vx_alloc::string::String; | |
| use vx_alloc::vec::Vec; | |
| use crate::collections::raw_bytes_map::RawBytesMap; | |
| use crate::collections::raw_table::RawTable; | |
| use crate::collections::raw_u64_map::RawU64Map; | |
| /// Static representation discriminant selecting the non-generic backing core | |
| /// for a [`Key`] type. | |
| #[derive(Copy, Clone, PartialEq, Eq)] | |
| pub enum KeyRepr { | |
| /// Inline 64-bit integer key backed by [`RawU64Map`]. | |
| U64, | |
| /// Heap-owned byte-slice key backed by [`RawBytesMap`]. | |
| Bytes, | |
| } | |
| impl fmt::Debug for KeyRepr { | |
| #[inline(always)] | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| match self { | |
| Self::U64 => f.write_str("KeyRepr::U64"), | |
| Self::Bytes => f.write_str("KeyRepr::Bytes"), | |
| } | |
| } | |
| } | |
| pub(crate) mod sealed { | |
| use super::*; | |
| pub trait Sealed: Sized { | |
| /// Insert `self` into `table` with value bytes at `val_src`. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Same preconditions as `RawU64Map::try_insert_raw` / `RawBytesMap::try_insert_raw`. | |
| unsafe fn raw_insert( | |
| table: &mut RawTable, | |
| key: Self, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError>; | |
| /// Clear all entries in `table` (retaining capacity), freeing key buffers if any. | |
| fn raw_clear(table: &mut RawTable); | |
| /// Free all key buffers (if any) and the bucket array of `table`. | |
| fn raw_free_all(table: &mut RawTable); | |
| } | |
| pub trait SealedQuery<K: super::Key> { | |
| /// Look up `self` inside `table`, returning a raw pointer to the stored | |
| /// value bytes or null if absent. | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8; | |
| /// Insert `self` (by borrowed reference) into `table` with value bytes at `val_src`. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Same preconditions as `RawU64Map::try_insert_raw` / `RawBytesMap::try_insert_raw`. | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError>; | |
| /// Remove `self` from `table`, moving its value bytes into `out_val`. | |
| /// | |
| /// # Safety | |
| /// | |
| /// Same preconditions as `RawU64Map::remove_raw` / `RawBytesMap::remove_raw`. | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool; | |
| } | |
| } | |
| /// Sealed trait implemented by all key types supported by [`HashMap`] and | |
| /// [`HashSet`]. | |
| /// | |
| /// Sealing ensures every map operation statically dispatches to one of the two | |
| /// verified non-generic cores ([`RawU64Map`] or [`RawBytesMap`]) with zero | |
| /// `BLR` comparator calls and zero monomorphized probe loops. | |
| pub trait Key: sealed::Sealed { | |
| /// Backing core representation for this key type. | |
| const REPR: KeyRepr; | |
| } | |
| /// Sealed query trait enabling borrowed lookups (`&str` on `HashMap<String, V>`, | |
| /// `&[u8]` on `HashMap<Vec<u8>, V>`, `&u64` on `HashMap<u64, V>`, etc.). | |
| pub trait KeyQuery<K: Key>: sealed::SealedQuery<K> {} | |
| impl<K: Key, Q: ?Sized + sealed::SealedQuery<K>> KeyQuery<K> for Q {} | |
| // ============================================================================ | |
| // KeyRepr::U64 Implementations (Integer Keys -> RawU64Map) | |
| // ============================================================================ | |
| macro_rules! impl_u64_key { | |
| ($($ty:ty => $to_u64:expr),+ $(,)?) => { | |
| $( | |
| impl sealed::Sealed for $ty { | |
| #[inline(always)] | |
| unsafe fn raw_insert( | |
| table: &mut RawTable, | |
| key: Self, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| let k: u64 = ($to_u64)(key); | |
| // SAFETY: `table` was initialized with `U64_HEADER_SIZE`; forwarded from caller. | |
| unsafe { RawU64Map::from_table_mut(table).try_insert_raw(k, val_src, old_val_out) } | |
| } | |
| #[inline(always)] | |
| fn raw_clear(table: &mut RawTable) { | |
| RawU64Map::from_table_mut(table).clear(); | |
| } | |
| #[inline(always)] | |
| fn raw_free_all(table: &mut RawTable) { | |
| RawU64Map::from_table_mut(table).free_all(); | |
| } | |
| } | |
| impl Key for $ty { | |
| const REPR: KeyRepr = KeyRepr::U64; | |
| } | |
| impl sealed::SealedQuery<$ty> for $ty { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| let k: u64 = ($to_u64)(*self); | |
| RawU64Map::from_table_ref(table).get_ptr(k) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| let k: u64 = ($to_u64)(*self); | |
| // SAFETY: Forwarded from caller. | |
| unsafe { | |
| RawU64Map::from_table_mut(table).try_insert_raw(k, val_src, old_val_out) | |
| } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| let k: u64 = ($to_u64)(*self); | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawU64Map::from_table_mut(table).remove_raw(k, out_val) } | |
| } | |
| } | |
| )+ | |
| }; | |
| } | |
| impl_u64_key!( | |
| u64 => |k: u64| k, | |
| u32 => |k: u32| k as u64, | |
| u16 => |k: u16| k as u64, | |
| u8 => |k: u8| k as u64, | |
| usize => |k: usize| k as u64, | |
| i64 => |k: i64| k as u64, | |
| i32 => |k: i32| (k as u32) as u64, | |
| i16 => |k: i16| (k as u16) as u64, | |
| i8 => |k: i8| (k as u8) as u64, | |
| isize => |k: isize| k as u64, | |
| ); | |
| // ============================================================================ | |
| // KeyRepr::Bytes Implementations (String / &str / Vec<u8> / &[u8] -> RawBytesMap) | |
| // ============================================================================ | |
| macro_rules! impl_bytes_key { | |
| ($ty:ty, |$k:ident| $as_bytes:expr) => { | |
| impl sealed::Sealed for $ty { | |
| #[inline(always)] | |
| unsafe fn raw_insert( | |
| table: &mut RawTable, | |
| $k: Self, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| let bytes: &[u8] = $as_bytes; | |
| // SAFETY: `table` was initialized with `BYTES_HEADER_SIZE`; forwarded from caller. | |
| unsafe { | |
| RawBytesMap::from_table_mut(table).try_insert_raw(bytes, val_src, old_val_out) | |
| } | |
| } | |
| #[inline(always)] | |
| fn raw_clear(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).clear(); | |
| } | |
| #[inline(always)] | |
| fn raw_free_all(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).free_all(); | |
| } | |
| } | |
| impl Key for $ty { | |
| const REPR: KeyRepr = KeyRepr::Bytes; | |
| } | |
| }; | |
| } | |
| macro_rules! impl_bytes_query { | |
| ($kty:ty, $qty:ty, |$q:ident| $as_bytes:expr) => { | |
| impl sealed::SealedQuery<$kty> for $qty { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| let $q = self; | |
| RawBytesMap::from_table_ref(table).get_ptr($as_bytes) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| let $q = self; | |
| // SAFETY: Forwarded from caller. | |
| unsafe { | |
| RawBytesMap::from_table_mut(table).try_insert_raw( | |
| $as_bytes, | |
| val_src, | |
| old_val_out, | |
| ) | |
| } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| let $q = self; | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).remove_raw($as_bytes, out_val) } | |
| } | |
| } | |
| }; | |
| } | |
| impl_bytes_key!(String, |k| k.as_bytes()); | |
| impl_bytes_query!(String, str, |q| q.as_bytes()); | |
| impl_bytes_query!(String, String, |q| q.as_bytes()); | |
| impl_bytes_key!(Vec<u8>, |k| k.as_slice()); | |
| impl_bytes_query!(Vec<u8>, [u8], |q| q); | |
| impl_bytes_query!(Vec<u8>, Vec<u8>, |q| q.as_slice()); | |
| impl<'a> sealed::Sealed for &'a str { | |
| #[inline(always)] | |
| unsafe fn raw_insert( | |
| table: &mut RawTable, | |
| key: Self, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: `table` was initialized with `BYTES_HEADER_SIZE`; forwarded from caller. | |
| unsafe { | |
| RawBytesMap::from_table_mut(table).try_insert_raw(key.as_bytes(), val_src, old_val_out) | |
| } | |
| } | |
| #[inline(always)] | |
| fn raw_clear(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).clear(); | |
| } | |
| #[inline(always)] | |
| fn raw_free_all(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).free_all(); | |
| } | |
| } | |
| impl<'a> Key for &'a str { | |
| const REPR: KeyRepr = KeyRepr::Bytes; | |
| } | |
| impl<'a> sealed::SealedQuery<&'a str> for str { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| RawBytesMap::from_table_ref(table).get_ptr(self.as_bytes()) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { | |
| RawBytesMap::from_table_mut(table).try_insert_raw(self.as_bytes(), val_src, old_val_out) | |
| } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).remove_raw(self.as_bytes(), out_val) } | |
| } | |
| } | |
| impl<'a, 'b> sealed::SealedQuery<&'a str> for &'b str { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| RawBytesMap::from_table_ref(table).get_ptr(self.as_bytes()) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { | |
| RawBytesMap::from_table_mut(table).try_insert_raw(self.as_bytes(), val_src, old_val_out) | |
| } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).remove_raw(self.as_bytes(), out_val) } | |
| } | |
| } | |
| impl<'a> sealed::Sealed for &'a [u8] { | |
| #[inline(always)] | |
| unsafe fn raw_insert( | |
| table: &mut RawTable, | |
| key: Self, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: `table` was initialized with `BYTES_HEADER_SIZE`; forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).try_insert_raw(key, val_src, old_val_out) } | |
| } | |
| #[inline(always)] | |
| fn raw_clear(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).clear(); | |
| } | |
| #[inline(always)] | |
| fn raw_free_all(table: &mut RawTable) { | |
| RawBytesMap::from_table_mut(table).free_all(); | |
| } | |
| } | |
| impl<'a> Key for &'a [u8] { | |
| const REPR: KeyRepr = KeyRepr::Bytes; | |
| } | |
| impl<'a> sealed::SealedQuery<&'a [u8]> for [u8] { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| RawBytesMap::from_table_ref(table).get_ptr(self) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).try_insert_raw(self, val_src, old_val_out) } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).remove_raw(self, out_val) } | |
| } | |
| } | |
| impl<'a, 'b> sealed::SealedQuery<&'a [u8]> for &'b [u8] { | |
| #[inline(always)] | |
| fn raw_get_ptr(&self, table: &RawTable) -> *const u8 { | |
| RawBytesMap::from_table_ref(table).get_ptr(self) | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_insert_ref( | |
| &self, | |
| table: &mut RawTable, | |
| val_src: *const u8, | |
| old_val_out: *mut u8, | |
| ) -> Result<bool, AllocError> { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).try_insert_raw(self, val_src, old_val_out) } | |
| } | |
| #[inline(always)] | |
| unsafe fn raw_remove(&self, table: &mut RawTable, out_val: *mut u8) -> bool { | |
| // SAFETY: Forwarded from caller. | |
| unsafe { RawBytesMap::from_table_mut(table).remove_raw(self, out_val) } | |
| } | |
| } |