So my current use case is I need to:
- Create a
Vec<Box<Material>>
where Material is a trait. - Push boxed structs that impl Material into this Vec.
- Share a read only version of this vec to multiple threads.
My current approach is to use an Arc pointer for the Vec<..> and then clone it for each thread.
for example:
let mut materials: Vec<Box<Material>> = Vec::new();
// ... push stuff into materials
let materials = Arc::new(materials);
let mat_cloned = materials.clone();
// pool is a threadpool
pool.execute(move|| {
// do read stuff with mat_cloned.
}
However I get the compiler error: dyn 'materials::Material' cannot be shared between threads safely
My understanding of it is that:
Materials is a heap allocated vector of boxed pointers to various Material implementations.
I then wrap with with an Arc type which is an atomically reference counted read-only pointer.
I should be able to share this pointer with threads safely?
material::Material can't be shared safely but why not a Arc pointer to it?