When using the newtype pattern I often have lengthy derives:
extern crate derive_more;
use derive_more::*;
#[derive(Add, Sub, Mul, Div, ..., Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
struct Foo(i32);
Is there a way to shorten this to something like this:
#[derive(Num)]
struct Foo(i32);
Where Num
is a derive macro?
I found this, but it seems like one can't expand macros in attributes. This answer discusses how attributes must be attached to items, ruling this out:
#[proc_macro_derive(Num)]
pub fn num_derive(_: TokenStream) -> TokenStream {
let gen = quote! {
#[derive(Add, Sub, Mul, Div, ..., Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
};
gen.into()
}