pub use fixed_map_derive::Key;
pub trait Key<K, V> {
type Storage: Storage<K, V>;
}
pub trait Storage<K, V>: Default {
fn insert(&mut self, key: K, value: V) -> Option<V>;
fn get(&self, key: &K) -> Option<&V>;
fn get_mut(&mut self, key: &K) -> Option<&mut V>;
fn remove(&mut self, key: &K) -> Option<V>;
fn for_each_value<F>(&self, f: F)
where
F: FnMut(&V);
}
pub struct Map<K, V>
where
K: Key<K, V>,
{
storage: K::Storage,
}
impl<K, V> Map<K, V>
where
K: Key<K, V>,
{
pub fn new() -> Map<K, V> {
Map {
storage: K::Storage::default(),
}
}
pub fn get(&self, key: &K) -> Option<&V> {
self.storage.get(key)
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.storage.get_mut(key)
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.storage.insert(key, value)
}
pub fn remove(&mut self, key: &K) -> Option<V> {
self.storage.remove(key)
}
pub fn is_empty(&self) -> bool {
let mut empty = true;
self.storage.for_each_value(|_| {
empty = false;
});
empty
}
pub fn len(&self) -> usize {
let mut len = 0;
self.storage.for_each_value(|_| {
len += 1;
});
len
}
}