I would like to create a callback function that has access to the argument instanceContext, but I am not sure how to fix the code.
The failing code is instanceContext.instances.len().
I have tried using lifetimes, move, Rc, Arc, Box, RefCell, but nothing seems to work.
The goal is to have multiple Instances which will have callbacks that will make it possible for one Instance to call/modify another Instance, through the InstanceContext.
During my experimentation I've been getting the following errors:
- may outlive borrowed value
- borrowed value does not live long enough
- closure is
FnOncebecause it moves the variable - this closure implements
FnOnce, notFn
use wasmtime::*;
struct InstanceContext {
id: i32,
instances: Vec<Instance>,
}
fn main() {
let store = Store::new(&Engine::default());
let mut instanceContext = InstanceContext { id: 5, instances: Vec::new() };
let _callback = createCallback(&store, &mut instanceContext);
}
fn createCallback(store: &Store, instanceContext: &mut InstanceContext) -> wasmtime::Func {
let f = Func::wrap(&store, || {
println!("Number of instances: {}", instanceContext.instances.len());
});
f
}