Conditionally Implement super trait, If not implemented

Let's consider this example:

trait Foo {}
trait Bar: Foo {}

// Let's say this macro generate:
// impl Foo for Qux {}
// impl Bar for Qux {}
#[derive(Foo, Bar)] 
struct Qux;

If Foo is not implemented, Is there any way to implement both trait just by adding #[derive(Bar)] ?

Are there any hacks?

There's no general way (or we'd have it for Eq and PartialEq, et cetra).

If it makes sense and you control the trait, you can

impl<T: ?Sized + Bar> Foo for T {}

But this does remove the ability for others to custom implement Foo if they also implement Bar.

2 Likes