derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Add referencing derive for enums

Open Monadic-Cat opened this issue 4 years ago • 2 comments
trafficstars

I want to do this in a PR if someone else can figure out the naming.

Feature

A derive macro that emits a duplicate of the input enum type, but with members now held by reference, and a method for producing an instance of that enum from a reference to the input enum.

Like the &Option<T> -> Option<&T> provided by Option::as_ref, but on any enum. (Also, I'd like a mutable referencing variant of this, like Option::as_mut.)

Since AsRef is already a trait derived by a macro in this crate, we need a different name for this macro. We also need a way to name the new generated type, and to decide whether to try and special case this for generic enums so they don't get an extra type unnecessarily. (I'm leaning toward not doing such a special case, but it's worth thinking about.)

Monadic-Cat avatar Oct 18 '21 19:10 Monadic-Cat

Ah, since this macro will create a wholly new type, it'd be nice to have a way to specify derives to be placed on it, too.

Monadic-Cat avatar Oct 22 '21 17:10 Monadic-Cat

On one hand this seems like quite a niche derive, on the other if you have a big enough enum it's really annoying to have to do this by hand. So I think I could see this fit well in the set of static method derives of this crate. Naming wise a few options I can think of:

  1. AsRefEnum
  2. EnumAsRef
  3. AsRefStatic
  4. StaticAsRef

JelteF avatar Dec 21 '23 23:12 JelteF