//! This module defines a `DynMap` -- a container for heterogeneous maps. //! //! This means that `DynMap` stores a bunch of hash maps inside, and those maps //! can be of different types. //! //! It is used like this: //! //! ``` //! // keys define submaps of a `DynMap` //! const STRING_TO_U32: Key = Key::new(); //! const U32_TO_VEC: Key> = Key::new(); //! //! // Note: concrete type, no type params! //! let mut map = DynMap::new(); //! //! // To access a specific map, index the `DynMap` by `Key`: //! map[STRING_TO_U32].insert("hello".to_string(), 92); //! let value = map[U32_TO_VEC].get(92); //! assert!(value.is_none()); //! ``` //! //! This is a work of fiction. Any similarities to Kotlin's `BindingContext` are //! a coincidence. pub mod keys; use std::{ hash::Hash, marker::PhantomData, ops::{Index, IndexMut}, }; use rustc_hash::FxHashMap; use stdx::anymap::Map; pub struct Key { _phantom: PhantomData<(K, V, P)>, } impl Key { pub(crate) const fn new() -> Key { Key { _phantom: PhantomData } } } impl Copy for Key {} impl Clone for Key { fn clone(&self) -> Key { *self } } pub trait Policy { type K; type V; fn insert(map: &mut DynMap, key: Self::K, value: Self::V); fn get<'a>(map: &'a DynMap, key: &Self::K) -> Option<&'a Self::V>; fn is_empty(map: &DynMap) -> bool; } impl Policy for (K, V) { type K = K; type V = V; fn insert(map: &mut DynMap, key: K, value: V) { map.map.entry::>().or_insert_with(Default::default).insert(key, value); } fn get<'a>(map: &'a DynMap, key: &K) -> Option<&'a V> { map.map.get::>()?.get(key) } fn is_empty(map: &DynMap) -> bool { map.map.get::>().map_or(true, |it| it.is_empty()) } } pub struct DynMap { pub(crate) map: Map, } impl Default for DynMap { fn default() -> Self { DynMap { map: Map::new() } } } #[repr(transparent)] pub struct KeyMap { map: DynMap, _phantom: PhantomData, } impl KeyMap> { pub fn insert(&mut self, key: P::K, value: P::V) { P::insert(&mut self.map, key, value) } pub fn get(&self, key: &P::K) -> Option<&P::V> { P::get(&self.map, key) } pub fn is_empty(&self) -> bool { P::is_empty(&self.map) } } impl Index> for DynMap { type Output = KeyMap>; fn index(&self, _key: Key) -> &Self::Output { // Safe due to `#[repr(transparent)]`. unsafe { std::mem::transmute::<&DynMap, &KeyMap>>(self) } } } impl IndexMut> for DynMap { fn index_mut(&mut self, _key: Key) -> &mut Self::Output { // Safe due to `#[repr(transparent)]`. unsafe { std::mem::transmute::<&mut DynMap, &mut KeyMap>>(self) } } }