Hi -
I’d like to be able to implement custom derive for traits. For example:
#[derive(BoxTrait)]
pub trait Foo {
fn foo(&self) -> i32;
fn bar(&self) -> String;
}
would automate generating something like:
impl Foo for Box<Foo> {
fn foo(&self) -> i32 { self.as_ref().foo() }
fn bar(&self) -> String { self.as_ref().bar() }
}
to allow Box<Foo> to be used anywhere Foo is required.
Another more complex (but related) example would be BoxableTrait, which would generate a function which can take an implementation of an object-safe trait Foo and produce a boxed trait object Box<Foo>s. (More generally it would be nice if it could also take trait-constrained type parameters and associated types and normalize them into boxed trait objects as well, to make the types as uniform as possible - but I’m not sure what it would take to do that mechanically in general.)
From what I can tell, this isn’t currently possible - custom derive only works for struct and enum. Are there any proposals to generalize it to trait as well? Or would that already work in unstable?
Are there any fundamental problems I’m overlooking that would doom this idea from the start?
Thanks!