Custom derives already exist in the form of procedural macros. And that mechanism is far more general then your proposal would allow, I'm not even sure how it would be used to derive
any interesting non-marker traits, since reflection is basically non-existent in the trait system โ how would Struct
allow getting the value of a field, or its name, generically?
My memory may be faulty, but what are these multiple ad-hoc mechanisms, besides auto traits (impl Tr for ..
) and Copy
? Leaving aside the questions of whether this proposal can even model Copy
(negative bounds seem required for that), replacing auto traits with something more complex only to capture Copy
(which needs special compiler support in many other ways, too) seems like a bad deal.
I would also like to point out that auto traits are using different rules from normal traits, specifically they are using coinductive reasoning instead of inductive reasoning. This means (a sound kind of) "infinite regress" is permitted when testing if a type implements an auto trait. For a type such as
struct List<T> { data: T, next: Option<Box<List<T>>> }
this means that it's Send
, but a trait Send2
written in the obvious way with your proposal would cause infinite recursion: to check if List<T> : Send2
, we must check whether all fields are Send2
, which will lead us to testing List<T> : Send2
again. Permitting coinductive reasoning for "normal" traits is unsound in general, so we can't just change that. We'd need another new feature that allows certain traits to opt into coinductive reasoning, but this:
- Will come with certain restrictions on the trait (it must be a pure marker trait, for starters)
- Will imply restrictions on what you can do with the trait (you can't always "mix" inductive and coinductive reasoning and expect the result to be correct)
- And thus will keep around most of the complexity of auto traits.
You may still argue that this is a better state of affairs because it's more orthogonal or something, but at the very least the proposal as currently stated isn't powerful enough.