git.delta.rocks / jrsonnet / refs/commits / c214d990b22e

difftreelog

refactor drop LayeredHashMap key generic

Yaroslav Bolyukin2021-01-12parent: #970fbe7.patch.diff
in: master

2 files changed

modifiedcrates/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 {
modifiedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
after · crates/jrsonnet-evaluator/src/map.rs
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}