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

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.