Par_iter_mut with Rc

The following code doesn't compile when the struct contains a reference counter, and works otherwise. I understand that a trait is not satisfied for Rc<T>, but how can I specify it properly?

use rayon::prelude::*;
use std::rc::Rc;
pub trait E: Clone + Send {
    fn eval(&self) -> f64;
}
struct C<T: E> {
//  d: T,
    d: Rc<T>,
    v: f64,
}
type P<T> = Vec<C<T>>;
fn eval_pop<T: E>(mut p: P<T>) -> P<T> {
	p.par_iter_mut().for_each(|v| {v.v = v.d.eval()});
    return p;
}

If something reference-counted must be accessible from mulltiple threads (such as inside parallel iteration), it have to be Arc, not Rc.

Doesn't compile either with Rc or Arc

You will need to add Send + Sync bounds to T as well.

use rayon::prelude::*;
use std::sync::Arc;
pub trait E: Clone + Send {
    fn eval(&self) -> f64;
}
struct C<T: E> {
//  d: T,
    d: Arc<T>,
    v: f64,
}
type P<T> = Vec<C<T>>;
fn eval_pop<T: E + Send + Sync>(mut p: P<T>) -> P<T> {
	p.par_iter_mut().for_each(|v| {v.v = v.d.eval()});
    return p;
}

Unfortunately the error message here, as long as those were missing, was not all that helpful.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.