1use gc::{Finalize, Gc, Trace};2use jrsonnet_interner::IStr;3use rustc_hash::FxHashMap;45pub struct LayeredHashMapInternals<V: Trace + Finalize + 'static> {6 parent: Option<LayeredHashMap<V>>,7 current: FxHashMap<IStr, V>,8}910unsafe impl<V: Trace + Finalize + 'static> Trace for LayeredHashMapInternals<V> {11 gc::custom_trace!(this, {12 mark(&this.parent);13 mark(&this.current);14 });15}16impl<V: Trace + Finalize + 'static> Finalize for LayeredHashMapInternals<V> {}1718#[derive(Trace, Finalize)]19pub struct LayeredHashMap<V: Trace + Finalize + 'static>(Gc<LayeredHashMapInternals<V>>);2021impl<V: Trace + 'static> LayeredHashMap<V> {22 pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {23 Self(Gc::new(LayeredHashMapInternals {24 parent: Some(self),25 current: new_layer,26 }))27 }2829 pub fn get(&self, key: &IStr) -> Option<&V> {30 (self.0)31 .current32 .get(key)33 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))34 }35}3637impl<V: Trace + 'static> Clone for LayeredHashMap<V> {38 fn clone(&self) -> Self {39 Self(self.0.clone())40 }41}4243impl<V: Trace + 'static> Default for LayeredHashMap<V> {44 fn default() -> Self {45 Self(Gc::new(LayeredHashMapInternals {46 parent: None,47 current: FxHashMap::default(),48 }))49 }50}