I'm trying to implement a "scope" in Rust in which it can contain values. It can optionally have a parent scope which it can look up if the parent contains the value but it doesn't.
The scope needs to be passed around to many other functions. Say I have the following code:
use std::collections::HashMap;
struct Value {
value: String,
}
struct Scope {
values: HashMap<String, Value>,
parent: Option</* ??? */>,
}
impl Scope {
fn new() -> Scope {
Scope {
values: HashMap::new(),
parent: None,
}
}
fn new_with_parent(scope: /* ??? */) -> Scope {
Scope {
values: HashMap::new(),
parent: Some(scope),
}
}
pub fn add_value(&mut self, name: String, val: Value) -> () {
self.values.insert(name, val);
}
pub fn get_parent_value(&mut self, name: String) -> Value {
match self.parent {
None => Value {
value: "None".to_string(),
},
Some(parent) => panic!()
}
}
}
fn main() {
let mut scope = Scope::new();
scope.add_value(
String::from("test"),
Value {
value: "test".to_string(),
},
);
test(&mut scope);
}
fn test(scope: &mut Scope) -> Value {
let mut child = Scope::new_with_parent(&scope);
child.get_parent_value(String::from("test"))
}
This is a minimal example of what I'm trying to do, I just can't figure out which type to use and how to use it for this.
I'm not sure if this is the correct way of doing such a thing in Rust, but I can't think of anything else as I'm quite new to this.
I've tried using Pins using Box::pin, using *const, RefCell but I couldn't figure out a way to make any of them work as I usually would get a "shared reference" error
You need to specify the life time of the reference to the parent. Something like this:
Output:
Rust Playground
Note: better semantics for
get_value()would probably be to see if the value exists inself.valuesand if not try the parent; this would continue until no more parents exist.