I’m not really one that asks for help, but just this once I’m going to see if it can solve my problem faster than I would eventually.
So, I have this:
pub struct BFBlock<'a> {
pub instructions: Vec<&'a mut dyn Instruction>
}
impl<'a> BFBlock<'a> {
pub fn new() -> BFBlock<'a> {
return BFBlock {
instructions: Vec::new()
};
}
pub fn add(&mut self, mut instruction: Box<dyn Instruction>) {
// argument requires that `*instruction` is borrowed for `'a`
self.instructions.push(&mut *instruction);
}
}
And in another function I have things like this:
let mut sum = Add::new(left, right);
branch.add(Box::new(sum));
I can’t make the argument for add
a reference because the struct constructed in the function doesn’t live as long as branch
I need some way to move it in a way add
can take ownership and insert it into the vector.
I started using Box since I can move the struct into the box and pass the box, but I haven’t been able to get that to work either.
Normally I would simply pass a non reference so that the argument ownership gets transferred, but since Instruction is a trait I can’t do that.