Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add referencing derive for enums #174

Open
Monadic-Cat opened this issue Oct 18, 2021 · 2 comments
Open

Add referencing derive for enums #174

Monadic-Cat opened this issue Oct 18, 2021 · 2 comments

Comments

@Monadic-Cat
Copy link
Contributor

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
Copy link
Contributor Author

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.

@JelteF
Copy link
Owner

JelteF commented Dec 21, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants