We could also change how derive attribute (or attributes in general) work and allow using funktion-like macros as derive macros?
#[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct Foo {
a: i32,
b: String,
}
Was syntax sugar for this.
struct Foo {
a: i32,
b: String,
}
derive! { struct Foo { a: i32, b: String, }, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash }
And allow function-like macros to be used derives.
macro_rules! Trivial {
( $t:item ) => {
derive! { $t, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash }
// we can't use because we end up with duplicate definition of foo
//
// #[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
// $t
}
}
#[derive(Trivial)]
struct Foo {
a: i32,
b: String,
}
One issue is that you can't have overlapping groupings without compiler magic.
(Also being able to quickly make your own derive macro just by using a function-like macro would be great, since I have a lot of code that looks like this:
struct Parens {
left: OpenParens,
expr: Expr,
right: CloseParens,
}
derive_parse_for_struct! { Parens { left, expr, right } }
There is a crate for this, but lang support would be great.
)