This code compiles correctly. It has some unused code warnings, but right now everything is fine.
use std::collections::BTreeMap; enum Object<'a> { Str(String), Int(i32), Float(f32), Vector(Vec<&'a Object<'a>>), Prim(fn(State) -> State) } struct State<'a> { named: BTreeMap<String, &'a Object<'a>>, stack: Vec<Object<'a>> } impl<'a> State<'a> { fn push_int(&mut self, x: i32) { self.stack.push(Object::Int(x)); } } fn main() { println!("Hello, world!"); let obj = Object::Str("this is a test".to_string()); }
An important part of this code is push_int and stack: Vec<Object<'a>> .
I'm kind of trying to create a stack-based virtual machine. I want to pass state to functions that can pick things up from the stack, manipulate stuff, and then put some stuff back on the stack; the named field will hold the named objects.
I have a suspicion that it would be better to have a stack represented as Vec<&'a Object<'a>> . As I have now, I am afraid that I will make the mistake of inefficiency. Is my guess right?
The second part of the problem is that I do not know how to make the link version vector work. Creating a new value with the correct lifetimes to push onto the stack does not work for me.
I am a bit vague about this issue, so if I were unclear, ask me questions to clear things up.
source share