ChainMap
This library provides a chain of HashMap
s with interior mutability of each intermediate layer. The HashMap
s are reference-counted, thus it is possible to create a tree of layers of HashMap
s and not just a single chain.
The higher maps in the tree (close to the leaves) have higher priority.
One possible use case is for the management of nested scopes.
An example from the appropriate section of the Book: 15. Scoping rules - RAII
Could be represented as
MainScope["_box2" => 5i32]
├── NestedScope["_box3" => 4i32]
├── LoopScope0[]
│ └── CreateBoxScope["_box1" => 3i32]
├── LoopScope1[]
│ └── CreateBoxScope["_box1" => 3i32]
│ ...
└── LoopScope999[]
└── CreateBoxScope["_box1" => 3i32]
Where each [ $( $key => $value ),* ]
is a level of a tree of ChainMap
s built on the previous one.
This it turn could be declared as
let mut main_scope = new;
main_scope.insert;
let mut nested_scope = main_scope.extend;
nested_scope.insert;
let mut loop_scope = Vec new;
for _ in 0..1000
The rules for which map entries are accessible from a certain level of the ChainMap
tree are exactly the same as how they would be for the corresponding scopes.