I followed the same train of thought. And since non-unsafe
traits that rely on the closed set of implementors do exist, we generally cannot go and implement traits for these things.
Well, the two "hypothetical" suggestions I made, either the attribute on the trait definition site, or the impl for that AutoEnum
wrapper type, require both that such things be written in the crate that defines the trait, so we wouldn't have to tackle that issue.
In that case, the #[auto_enum]
proc-macro is the marker.
I think we all agree that provided some marker, either built-in or through a (proc-)macro, that auto-generates an ad-hoc instance of (a potentially ad-hoc) enum definition, this can "easily" be achieved, provided we target a trait we already know how to make it delegate to each variant.
But I am still concerned about other traits; we should focus on a way to generalize delegations, otherwise this "feature" will be of limited usability.
- That being said, I like your approach, @taiki-e, of relying on defining custom-derives for a at least some form of extensionability

And besides unsafe
-ty of the trait, visibility / implementability of the trait, there is also the question of associated items that wouldn't be methods.
For instance, given
trait IntoConstLenIterator : IntoIterator {
const LEN: usize;
}
impl<T, const N: usize> Into... for [T; N] { const LEN: usize = N; }
Then what do we do with the following?
fn foo (cond: bool) -> impl IntoConstLenIterator<IntoIter = ...>
{
enum match cond { true => [42], false => [42, 27] }
}
There is no sensible delegation for this trait.
More generally, we should restrict ourselves to traits:
-
not having associated functions without a self
receiver (otherwise we cannot read the enum
discriminant to dispatch),
- But contrary to our current
dyn Trait
, self: Self
and, more generally, Sized
-ness, wouldn't be an issue. Neither would be generic paramers.
-
whose non-function associated items (type
s and const
s) would have to be fixed, similar to dyn Trait
.