How to work around missing coercion to super trait object?

I'm a bit confused with how trait objects implement the underlying trait. I'm trying to work around the coercion to super trait object that's currently unavailable in stable, and I thought that implementing a third trait to generic types would work, but it seems like there's something missing, like an impl Trait for dyn Trait:

Is there a reason that this doesn't work? Are there any suggestions to work around coercion to the super trait object?

Thanks in advance,


Type parameters are implicitly Sized, and for a good reason – you usually want them to be sized. So the two blanket impls of Action<()> and Action<bool> for T apply only to Sized types – which dyn Trait trait objects aren't.

If you opt out of sized-ness, then your code compiles and runs.

Do note, however, that this means that there's no coercion from subtrait to supertrait. That's simply not a thing in Rust. In order to create a trait object from a value, the value must be Sized, so you simply can't convert one trait object to another. If you already have a trait object of type dyn Subtrait, and you want to obtain a value of type dyn Supertrait from it, then you will have to explicitly provide a conversion method in your subtrait. (This is commonly done with Any in practice in order to support downcasting.)


@H2CO3 already explained why your example didn't work. You may find some other answers in this tour, such as


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.