0
trait Trait {}

#[derive(Clone)]
struct Type {
    inner: Box<dyn Trait>
}
error[E0277]: the trait bound `dyn Trait: Clone` is not satisfied
 --> x.rs:5:5
  |
3 | #[derive(Clone)]
  |          ----- in this derive macro expansion
4 | struct Type {
5 |     inner: Box<dyn Trait>
  |     ^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `dyn Trait`
  |

Cool, I'll just specify that anything implementing Trait must also implement Clone:

trait Trait: Clone {}

#[derive(Clone)]
struct Type {
    inner: Box<dyn Trait>
}
error[E0038]: the trait `Trait` cannot be made into an object
 --> x.rs:5:16
  |
5 |     inner: Box<dyn Trait>
  |                ^^^^^^^^^ `Trait` cannot be made into an object
  |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
 --> x.rs:1:14
  |
1 | trait Trait: Clone {}
  |       -----  ^^^^^ ...because it requires `Self: Sized`
  |       |
  |       this trait cannot be made into an object...

How am I meant to be able to make a trait that can be used as the type signature of fields in structs that are cloneable?

theonlygusti
  • 11,032
  • 11
  • 64
  • 119

0 Answers0