0

I need to use the Executor type as a trait object but I get the following error:

the trait `Executor` cannot be made into an object.
the trait cannot be made into an object because it requires `Self: Sized`

If we look into the trait definition, we can see how it actually implements Sized:

pub trait Executor<'c>: Send + Debug + Sized {
    type Database: Database;
...
}

I basically want to do something like the following:

struct CustomExecutor<'c> {
    inner: Arc<dyn Executor<'c, Database = Postgres>>,
}

I would expect this to work completely fine, can someone tell me what I'm doing wrong?

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
JasterV
  • 17
  • 5
  • 1
    It's called [object safety](https://doc.rust-lang.org/reference/items/traits.html#object-safety). If a trait has `Sized` as a super trait, it cannot be turned into trait object. Which kind of makes sense intuitively. Trait objects are dynamically sized sort of by definition so they cannot implement a `Sized` trait. But implementing that trait is all they're supposed to do. Not sure how to work around this. Although in your example, you could just use generics, but I assume your real use case is less straight-forward. – isaactfa Jun 19 '23 at 22:26

0 Answers0