I have a collection of my trait, and I'd like to be able to call a mutable method of the trait for each of the items in my map.
At the moment I do this sequentially and my collection looks like the following:
use std::cell::RefCell;
use std::collections::*;
use std::rc::Rc;
trait Trait {
fn foo_mut(&mut self);
}
fn main() {
let mut items: HashMap<i32, Rc<RefCell<dyn Trait>>> = HashMap::new();
// I have a separate data structure that holds Week<RefCell<dyn Trait>>
for item in items.values_mut() {
item.borrow_mut().foo_mut();
}
}
I'd like to call the trait method in parallel now, so I first changed my data structure to:
use std::collections::*;
use std::sync::{Arc, RwLock};
fn main() {
let mut items: HashMap<i32, Arc<RwLock<dyn Trait>>> = HashMap::new();
for item in items.values_mut() {
item.write().unwrap().foo_mut();
}
}
then I came across rayon, and I tried to use its parallel iterators, but the following code raises an error:
items.par_iter_mut().for_each(|(id, item)| item.write().unwrap().foo_mut());
error[E0599]: no method named `par_iter_mut` found for struct `std::collections::HashMap<i32, std::sync::Arc<std::sync::RwLock<dyn Trait>>>` in the current scope
--> src/main.rs:12:11
|
12 | items.par_iter_mut().for_each(|(id, item)| item.write().unwrap().foo_mut());
| ^^^^^^^^^^^^ help: there is an associated function with a similar name: `iter_mut`
|
= note: the method `par_iter_mut` exists but the following trait bounds were not satisfied:
`&mut std::collections::HashMap<i32, std::sync::Arc<std::sync::RwLock<dyn Trait>>>: rayon::iter::IntoParallelIterator`
which is required by `std::collections::HashMap<i32, std::sync::Arc<std::sync::RwLock<dyn Trait>>>: rayon::iter::IntoParallelRefMutIterator`
I checked the for_each documentation, and it requires Self::Item to be Send and the closure to be Send + Sync, now from what I can see Arc is already Send + Sync, but the code can be fixed by adding both these traits to mine like:
let mut items: HashMap<i32, Arc<RwLock<dyn Trait + Send + Sync>>> = HashMap::new();
Why is this necessary?