difftreelog
refactor drop LayeredHashMap key generic
in: master
2 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -19,7 +19,7 @@
dollar: Option<ObjValue>,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- bindings: LayeredHashMap<IStr, LazyVal>,
+ bindings: LayeredHashMap<LazyVal>,
}
impl Debug for ContextInternals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth1use rustc_hash::FxHashMap;2use std::{borrow::Borrow, hash::Hash, rc::Rc};34#[derive(Default, Debug)]5struct LayeredHashMapInternals<K: Hash, V> {6 parent: Option<LayeredHashMap<K, V>>,7 current: FxHashMap<K, V>,8}910#[derive(Debug)]11pub struct LayeredHashMap<K: Hash, V>(Rc<LayeredHashMapInternals<K, V>>);1213impl<K: Hash + Eq, V> LayeredHashMap<K, V> {14 pub fn extend(self, new_layer: FxHashMap<K, V>) -> Self {15 match Rc::try_unwrap(self.0) {16 Ok(mut map) => {17 map.current.extend(new_layer);18 Self(Rc::new(map))19 }20 Err(this) => Self(Rc::new(LayeredHashMapInternals {21 parent: Some(Self(this)),22 current: new_layer,23 })),24 }25 }2627 pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>28 where29 K: Borrow<Q>,30 Q: Hash + Eq,31 {32 (self.0)33 .current34 .get(key)35 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))36 }37}3839impl<K: Hash, V> Clone for LayeredHashMap<K, V> {40 fn clone(&self) -> Self {41 Self(self.0.clone())42 }43}4445impl<K: Hash + Eq, V> Default for LayeredHashMap<K, V> {46 fn default() -> Self {47 Self(Rc::new(LayeredHashMapInternals {48 parent: None,49 current: FxHashMap::default(),50 }))51 }52}1use jrsonnet_interner::IStr;2use rustc_hash::FxHashMap;3use std::rc::Rc;45#[derive(Default, Debug)]6struct LayeredHashMapInternals<V> {7 parent: Option<LayeredHashMap<V>>,8 current: FxHashMap<IStr, V>,9}1011#[derive(Debug)]12pub struct LayeredHashMap<V>(Rc<LayeredHashMapInternals<V>>);1314impl<V> LayeredHashMap<V> {15 pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {16 match Rc::try_unwrap(self.0) {17 Ok(mut map) => {18 map.current.extend(new_layer);19 Self(Rc::new(map))20 }21 Err(this) => Self(Rc::new(LayeredHashMapInternals {22 parent: Some(Self(this)),23 current: new_layer,24 })),25 }26 }2728 pub fn get(&self, key: &IStr) -> Option<&V> {29 (self.0)30 .current31 .get(key)32 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))33 }34}3536impl<V> Clone for LayeredHashMap<V> {37 fn clone(&self) -> Self {38 Self(self.0.clone())39 }40}4142impl<V> Default for LayeredHashMap<V> {43 fn default() -> Self {44 Self(Rc::new(LayeredHashMapInternals {45 parent: None,46 current: FxHashMap::default(),47 }))48 }49}