1use jrsonnet_gc::{Gc, Trace};2use jrsonnet_interner::IStr;3use rustc_hash::FxHashMap;45use crate::LazyVal;67#[derive(Trace)]8#[trivially_drop]9pub struct LayeredHashMapInternals {10 parent: Option<LayeredHashMap>,11 current: FxHashMap<IStr, LazyVal>,12}1314#[derive(Trace)]15#[trivially_drop]16pub struct LayeredHashMap(Gc<LayeredHashMapInternals>);1718impl LayeredHashMap {19 pub fn extend(self, new_layer: FxHashMap<IStr, LazyVal>) -> Self {20 Self(Gc::new(LayeredHashMapInternals {21 parent: Some(self),22 current: new_layer,23 }))24 }2526 pub fn get(&self, key: &IStr) -> Option<&LazyVal> {27 (self.0)28 .current29 .get(key)30 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))31 }3233 pub fn contains_key(&self, key: &IStr) -> bool {34 (self.0).current.contains_key(key)35 || self36 .037 .parent38 .as_ref()39 .map(|p| p.contains_key(key))40 .unwrap_or(false)41 }42}4344impl Clone for LayeredHashMap {45 fn clone(&self) -> Self {46 Self(self.0.clone())47 }48}4950impl Default for LayeredHashMap {51 fn default() -> Self {52 Self(Gc::new(LayeredHashMapInternals {53 parent: None,54 current: FxHashMap::default(),55 }))56 }57}