I have a struct in Rust representing a container for a heavy data array. I need a method to perform some calculations over this data:
pub struct MultithreadTest {
data: Vec<usize>,
}
impl MultithreadTest {
const WORKERS_COUNT: usize = 2;
pub fn new(data: Vec<usize>) -> Self {
Self { data }
}
pub fn calculate(&self) {
let data = std::sync::Arc::new(&self.data);
let (tx, rx) = std::sync::mpsc::channel();
(0..Self::WORKERS_COUNT).for_each(|thread_index| {
let data = data.clone();
let tx = tx.clone();
std::thread::spawn(move || {
// Do some stuff with data
let sum: usize = data.iter().skip(thread_index).sum();
tx.send(sum).unwrap();
});
});
let result: usize = rx.iter().take(Self::WORKERS_COUNT).sum();
println!("{:}", result);
}
}
If I try to provide self
by reference, Rust stops me:
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/lib.rs:13:40
|
13 | let data = std::sync::Arc::new(&self.data);
| ^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 12:5...
--> src/lib.rs:12:5
|
12 | / pub fn calculate(&self) {
13 | | let data = std::sync::Arc::new(&self.data);
14 | |
15 | | let (tx, rx) = std::sync::mpsc::channel();
... |
31 | | println!("{:}", result);
32 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:13:40
|
13 | let data = std::sync::Arc::new(&self.data);
| ^^^^^^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/lib.rs:21:32: 26:14 data:std::sync::Arc<&std::vec::Vec<usize>>, thread_index:usize, tx:std::sync::mpsc::Sender<usize>]` will meet its required lifetime bounds
--> src/lib.rs:21:13
|
21 | std::thread::spawn(move || {
| ^^^^^^^^^^^^^^^^^^```
I totally understand where it's coming from, but I can't understand how to solve it.
I can't clone `data` because it might be huge and I don't want to consume `self` by value.