jrsonnet_evaluator/
map.rs1use jrsonnet_gc::{Gc, Trace};
2use jrsonnet_interner::IStr;
3use rustc_hash::FxHashMap;
4
5use crate::LazyVal;
6
7#[derive(Trace)]
8#[trivially_drop]
9pub struct LayeredHashMapInternals {
10 parent: Option<LayeredHashMap>,
11 current: FxHashMap<IStr, LazyVal>,
12}
13
14#[derive(Trace)]
15#[trivially_drop]
16pub struct LayeredHashMap(Gc<LayeredHashMapInternals>);
17
18impl 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 }
25
26 pub fn get(&self, key: &IStr) -> Option<&LazyVal> {
27 (self.0)
28 .current
29 .get(key)
30 .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
31 }
32}
33
34impl Clone for LayeredHashMap {
35 fn clone(&self) -> Self {
36 Self(self.0.clone())
37 }
38}
39
40impl Default for LayeredHashMap {
41 fn default() -> Self {
42 Self(Gc::new(LayeredHashMapInternals {
43 parent: None,
44 current: FxHashMap::default(),
45 }))
46 }
47}