I am getting the parameter type
M may not live long enough
error.
Not sure how to interpret it. On one hand, Rust does not know when thread is going to be terminated, and to be safe, considers its life time 'static. Closure which captures context variables, has life time... well, I am confused. It is marked as move
, so as long as params are Send
, lifetime should not matter, because thread is now owner of any variable being used in closure. Compiler says "so that the type [closure@... will meet its required lifetime bounds" but I have no idea what its lifetime bounds are.
use std::thread;
use std::sync::mpsc;
trait ToMessage : Send {
fn value(&self) -> Vec<u8>;
}
fn test<M>() where M: ToMessage {
let (tx, rx) = mpsc::channel();
thread::spawn(move ||{
let m: M = rx.recv().unwrap();
});
}
fn main() {}
error[E0310]: the parameter type `M` may not live long enough
--> src/main.rs:10:5
|
8 | fn test<M>() where M: ToMessage {
| - help: consider adding an explicit lifetime bound `M: 'static`...
9 | let (tx, rx) = mpsc::channel();
10 | thread::spawn(move ||{
| ^^^^^^^^^^^^^
|
note: ...so that the type `[closure@src/main.rs:10:19: 12:6 rx:std::sync::mpsc::Receiver<M>]` will meet its required lifetime bounds
--> src/main.rs:10:5
|
10 | thread::spawn(move ||{
| ^^^^^^^^^^^^^