TraitObject incompatible with Clone?

Is it possible to create Box of Clone-able trait object?

trait Engine : Clone {
    fn do_it(&self);

fn bar(_engine: &Box<dyn Engine>) {

5 | fn bar(_engine: &Box<dyn Engine>) {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Engine` cannot be made into an object
  = note: the trait cannot require that `Self : Sized`

Clone is Sized: pub trait Clone : Sized and this makes trait object-unsafe. I've read RFC-255 0255-object-safety - The Rust RFC Book but I can not say I understood argumentation why trait's bound Self: Sized would break object safety. I've also read proposed mitigations from this RFC, but can not see how to use them in my case.

What I really want is something like this: Rust Playground
Is there any way to achieve it?

Would something like this work? You can fiddle with BoxClone as much as you need to to make this work for you,

But if you have a finite number of engines, you should consider using an enum instead. Trait objects are really akward to use if you need to clone them. Another option is to use Arc<..> or Rc<..> instead of Box, that way you can always clone them.


A good article for explaining Object Safety is @huon's Object Safety.

While not ideal, this crate also makes cloning trait objects possible: objekt - Rust

1 Like

Yes, I thought about it and in the end I simply parameterized functions with generics.
In this particular case it is ok, but generally speaking I am concerned with code multiplication during monomorpisation in cases when there are a lot of functions to be parameterized and a lot of traits implementations.

But at least I found better explanation: Clone returns Self, which means that caller must know size at compile time. Now that's make sense. I'll read @Michael-F-Bryan link to understand better Sized issue.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.