git.delta.rocks / jrsonnet / refs/commits / 32f6ee5b9541

difftreelog

source

crates/jrsonnet-evaluator/src/map.rs1.1 KiBsourcehistory
1use gcmodule::{Cc, Trace};2use jrsonnet_interner::IStr;34use crate::{GcHashMap, Thunk, Val};56#[derive(Trace)]7#[force_tracking]8pub struct LayeredHashMapInternals {9	parent: Option<LayeredHashMap>,10	current: GcHashMap<IStr, Thunk<Val>>,11}1213#[derive(Trace)]14pub struct LayeredHashMap(Cc<LayeredHashMapInternals>);1516impl LayeredHashMap {17	pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {18		Self(Cc::new(LayeredHashMapInternals {19			parent: Some(self),20			current: new_layer,21		}))22	}2324	pub fn get(&self, key: &IStr) -> Option<&Thunk<Val>> {25		(self.0)26			.current27			.get(key)28			.or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))29	}3031	pub fn contains_key(&self, key: &IStr) -> bool {32		(self.0).current.contains_key(key)33			|| self34				.035				.parent36				.as_ref()37				.map_or(false, |p| p.contains_key(key))38	}39}4041impl Clone for LayeredHashMap {42	fn clone(&self) -> Self {43		Self(self.0.clone())44	}45}4647impl Default for LayeredHashMap {48	fn default() -> Self {49		Self(Cc::new(LayeredHashMapInternals {50			parent: None,51			current: GcHashMap::new(),52		}))53	}54}