Magix
Magix

Reputation: 5369

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

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()...

Upvotes: 2

Views: 189

Answers (1)

Magix
Magix

Reputation: 5369

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

Upvotes: 3

Related Questions