Hello,
it seems so simple, but for me today it is not. I'm using rayon::ThreadPool and two channels for each thread to process data and get the results back. I'm also thinking to write this again with the new async stuff which is available now, do you think it would be nicer to handle this case? I'm open to suggestions.
I get the following error for the reduced test case.
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:27:25
|
27 | self.thread_data.push(ThreadData {
| ^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 23:6...
--> src/main.rs:23:6
|
23 | impl<'a> Test<'a> {
| ^^
= note: ...so that the expression is assignable:
expected ThreadData<'a>
found ThreadData<'_>
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/main.rs:31:21: 37:4 receiver:std::sync::mpsc::Receiver<std::collections::HashMap<i64, (&mut std::boxed::Box<(dyn Machine + std::marker::Send + 'static)>, std::vec::Vec<&mut std::boxed::Box<(dyn Machine + std::marker::Send + 'static)>>)>>]` will meet its required lifetime bounds
--> src/main.rs:31:15
|
31 | thread_pool.spawn(move || {
| ^^^^^
I don't understand this part of the message: isn't 'a and '_ interchangeable here?
expected ThreadData<'a>
found ThreadData<'_>
= note: but, the lifetime must be valid for the static lifetime...
Mind that in my real application (that I'm trying to compile) the error is slightly different, but I think this is the same problem as seen here:
error: unsatisfied lifetime constraints
--> src/test.rs:842:4
|
329 | impl<'a> Test<'a> {
| -- lifetime `'a` defined here
...
842 | self.thread_data.push(ThreadData {is_alive,
| _____________^
843 | | computation_unit: ComputationUnit::from(barrier.elements.clone()),
844 | | running_since: now.as_millis(),
845 | | receiver,
846 | | sender: sender.clone()});
| |_____________________________________________^ argument requires that `'a` must outlive `'static`
What I can do?
This is the reduced code. If someone could make it compile, I would be very happy. Thank you!
use std;
use std::collections::HashMap;
use std::sync::mpsc::channel;
use std::sync::mpsc::Sender;
use std::vec::Vec;
fn main() {}
pub type MachineId = i64;
pub type MachinePointer<'a> = &'a mut Box<dyn Machine + Send>;
pub trait Machine {}
pub struct Test<'a> {
pub thread_data: Vec<ThreadData<'a>>,
}
pub struct ThreadData<'a> {
sender: Sender<HashMap<MachineId, (MachinePointer<'a>, Vec<MachinePointer<'a>>)>>,
}
impl<'a> Test<'a> {
fn _run(&mut self, thread_pool: &rayon::ThreadPool) {
let (sender, receiver) = channel();
self.thread_data.push(ThreadData {
sender,
});
thread_pool.spawn(move || {
loop {
let mut _incoming: Vec<HashMap<MachineId, (&mut Box<dyn Machine + Send>, Vec<&mut Box<dyn Machine + Send>>)>> = receiver.try_iter().collect();
//thread processes incoming data
//and returns the results via another channel back (not included)
}
});
//get results from thread if there are any
//or exit
//_run will be called again later to start new threads or handle results
}
}