git.delta.rocks / jrsonnet / refs/commits / 3d527a7bcaaf

difftreelog

source

crates/jrsonnet-evaluator/src/map.rs1.0 KiBsourcehistory
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}