I have hit a speedbump in a project I am working on.
Basically, I have an Arc<T>
cloned object that gets (move
) captured in a closure, and that closure gets moved to another thread with thread::spawn
. The compiler complains that T
needs to be 'static
but I don't see why that is necessary, as Arc<T>
should be managing the lifetime. I don't want to add the 'static
constraint to T
all throughout my code.
I have created a dummy example below. I have extracted the various functions to highlight the constraints placed on the callback and T
type.
I think I might be misunderstanding the role of Arc
, could someone please explain why the 'static
lifetime is required for T
?
use std::sync::Arc;
use std::thread;
fn main() {
let my_struct = Arc::new(MyStruct);
let arc_clone = Arc::clone(&my_struct);
call_spawn_thread_code(arc_clone);
}
fn call_spawn_thread_code<T>(t: Arc<T>)
where
T: MyTrait + Send + Sync, // + 'static <-- will fix the issue but means T must be 'static, which propogates
{
spawn_thread_code(move |string| {
t.foo();
println!("{}", string);
});
}
fn spawn_thread_code<F>(cb: F)
where
F: Fn(&str) + Send + 'static,
{
let jh = thread::spawn(move || cb("Hello, world!"));
jh.join().unwrap();
}
struct MyStruct;
impl MyTrait for MyStruct {}
trait MyTrait {
fn foo(&self) {
println!("bar");
}
}