I need to call a function with a lifetime specified on self from a static context like
impl <'w> World<'w> {
pub fn test_with_lifetime(&'w mut self) {
println!("test with lifetime");
}
pub fn test(&mut self) {
println!("test");
}
}
pub fn main(){
let mut world:World = World::new();
let world_rc:Rc<RefCell<World<'static>>> = Rc::new(RefCell::new(world));
let world_in_closure = Rc::clone(&world_rc);
let bx = Box::new(move ||{
if let Ok(mut borrowed_world) = world_in_closure.try_borrow_mut() {
borrowed_world.test_with_lifetime();
}
});
(bx)();
}
and if fails with 'borrowed value does not live long enough...' error.
So I have two questions:
what's difference between &self and
&'w self
in function definition? Don't they both effectively mean that object lives in the caller's context?is there a way to make it compile?