use std::borrow::Borrow;
use std::cell::{Cell, UnsafeCell};
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::FromIterator;
use std::ops::Index;
use stable_deref_trait::StableDeref;
pub struct FrozenMap<K, V> {
map: UnsafeCell<HashMap<K, V>>,
in_use: Cell<bool>,
}
impl<K: Eq + Hash, V> FrozenMap<K, V> {
pub fn new() -> Self {
Self {
map: UnsafeCell::new(Default::default()),
in_use: Cell::new(false)
}
}
}
impl<K: Eq + Hash, V: StableDeref> FrozenMap<K, V> {
pub fn insert(&self, k: K, v: V) -> &V::Target {
assert!(!self.in_use.get());
self.in_use.set(true);
let ret = unsafe {
let map = self.map.get();
&*(*map).entry(k).or_insert(v)
};
self.in_use.set(false);
ret
}
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V::Target>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
assert!(!self.in_use.get());
self.in_use.set(true);
let ret = unsafe {
let map = self.map.get();
(*map).get(k).map(|x| &**x)
};
self.in_use.set(false);
ret
}
pub fn into_map(self) -> HashMap<K, V> {
self.map.into_inner()
}
}
impl<K, V> From<HashMap<K, V>> for FrozenMap<K, V> {
fn from(map: HashMap<K, V>) -> Self {
Self {
map: UnsafeCell::new(map),
in_use: Cell::new(false)
}
}
}
impl<K: Eq + Hash, V: StableDeref> Index<K> for FrozenMap<K, V> {
type Output = V::Target;
fn index(&self, idx: K) -> &V::Target {
self.get(&idx).expect("attempted to index FrozenMap with unknown key")
}
}
impl<K: Eq + Hash, V> FromIterator<(K, V)> for FrozenMap<K, V> {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (K, V)> {
let map: HashMap<_, _> = iter.into_iter().collect();
map.into()
}
}
impl<K: Eq + Hash, V:> Default for FrozenMap<K, V> {
fn default() -> Self {
FrozenMap::new()
}
}