pub struct Channel<'a> {
pub task_sender: Arc<Mutex<mpsc::Sender<&'a TaskAction>>>,
pub task_receiver: Arc<Mutex<mpsc::Receiver<&'a TaskAction>>>
}
impl <'a> Channel<'a> {
pub fn new() -> Channel<'a> {
let (sender, receiver) = mpsc::channel();
Channel {
task_sender: Arc::new(Mutex::new(sender)),
task_receiver: Arc::new(Mutex::new(receiver))
}
}
pub fn listen<F, E>(&mut self, handler: F, err: E)
where
F : Fn(&TaskAction) + Sync + Send + 'a,
E : Fn() + Sync + Send + 'a
{
let cloned = self.task_receiver.clone();
thread::spawn(move || {
loop {
impl <'a> Channel<'a> {
| -- lifetime `'a` defined here
...
23 | pub fn listen<F, E>(&mut self, handler: F, err: E)
| --------- `self` is a reference that is only valid in the method body
...
28 | let cloned = self.task_receiver.clone();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `self` escapes the method body here
| argument requires that `'a` must outlive `'static`
error happened becuase self will just escape and the way i wrote task_receiver.clone() is also unreasonable
im thinking about create a listen method, basically i want it like there is always a second thread that listen to certain action and react, but the way i code is defintely wrong the structure thing( new Channel{} )
any advice or ideas would be much appreciated!