I noticed that if you implement a derive proc-macro you can inspect all of the item the macro is derived to. The only thing that's not provided to the derive macro is the #[derive(Foo, Bar)]
line itself. I wonder if there is a reason for this, as there are cases where it's useful to just see which other derives are applied to a struct.
For diesel I have a particular use-case for such a feature in mind: We have a #[derive(Selectable)]
macro, which constructs a select clause for your SQL query based on the given struct definition. On it's own that's not useful as you cannot load data without using another derive (#[derive(Queryable)]
). It's almost always a mistake to use one without the other one. Ideally we would be able to detect such a mistake and emit some sort of warning from the Selectable
derive macro that users are aware of this issue. Possibly we would also be able to just generate the right Queryable
impl if the other derive is not applied.
I can imaging that this might also be useful for other crates, as you could eventually detect for example that the type also derives Serializable
and generate additional code based on that or something similar.