How to clone a struct which holds a trait object to a `Fn`

Viewed 92

I have the following struct :

#[derive(Clone)]
pub struct MyStruct {
  function : Box<dyn Fn(Box<dyn Any>) -> Box<dyn Any> + Send + Sync>,
  [...]
}

The clone derivation fails because the box isn't Clone but I don't have a way to make the Fn Clone, is there a way out of this ? The reason I need clone is that I sometimes pass around &[MyStruct] and need to clone to own with .to_vec()...

1 Answers

Simple answer : switch the Box (a single-owner pointer) to an Rc or Arc (shareable pointers)

Related