Rayon: how implement into_par_iter() and par_iter()

Hi, I'm trying to use the into_par_iter() and par_iter() to execute some implementations inside a Vec. I defined a trait called Analyzer and created two implementations, after setup these structs in a Vec<Box> I tried to iterate in this Vec using rayon, but the application returned the follow error.

note: the method into_par_iter exists but the following trait bounds were not satisfied: [std::boxed::Box<dyn Analyzer>] : rayon::iter::IntoParallelIterator

I supposed to implement this trait for Analyzer, but I don't know how. May some one help me? Here is the code. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=884289b9716cd597d225f6bf1230cba0

I don't know why you've a flatten call, but your issue is that the things you're iterating over need to be Send such that rayon can put them on different threads to work on in parallel.

You can do this by adding Send as a supertrait for Analyzer or working with dyn Analyzer + Send.


Send is needed to iterate values with into_par_iter() or mutable references with par_iter_mut(), and Sync is needed to iterate shared references with par_iter().

1 Like

I don't need flatten xD, I tried to add Send and Sync as a supertrait for Analyzer as presented here, but I couldn't make it work. Do you have an example?

This should work:

trait Analyzer: Send + Sync {
    // ...

It works, thank you very much.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.