Hello, I have two variables where the second one requires the fist one to outlive it. I need to move both of them into a thread, but the compiler is complaining that the first one doesn't live long enough. Here is the code:
use std::thread;
trait Facade : Sync { fn add(&self)->u32; }
struct RoutingNode<'a> { facade: &'a (Facade + 'a) }
impl<'a> RoutingNode<'a> {
fn new(facade: &'a Facade) -> RoutingNode<'a> {
RoutingNode { facade: facade }
}
}
fn main() {
struct MyFacade;
impl Facade for MyFacade {
fn add(&self)->u32 { 999u32 }
}
let facade = MyFacade;
let routing = RoutingNode::new(&facade);
let t = thread::spawn(move|| {
let f = facade;
let r = routing;
});
t.join();
}
<anon>:25:35: 25:41 error: `facade` does not live long enough
<anon>:25 let routing = RoutingNode::new(&facade);
^~~~~~
note: reference must be valid for the static lifetime...
<anon>:24:24: 33:2 note: ...but borrowed value is only valid for the block suffix following statement 2 at 24:23
I can see the problem is that I somehow need to tell rust explicitly that the facade will outlive routing object. Just have no clue how.
I was hoping for a solution where I wouldn't need to change the RoutingNode and Facade structures. I mean, I can change them since they are in our code base, but if they weren't, would I be stuck? What adds to the problem, is that I can't create the routing object inside the thread because I need to use it before it goes to the thread. Fortunately, I don't need to then share those objects between threads (just move it once from the main thread to the other one), so no need for Arc.