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

From for unit enum variants #408

Closed
spearman opened this issue Aug 31, 2024 · 5 comments
Closed

From for unit enum variants #408

spearman opened this issue Aug 31, 2024 · 5 comments
Assignees

Comments

@spearman
Copy link

It seems like in the change from version 0.99 to version 1.0 that unit enum variants no longer implement from unit () by default.

For example:

#[derive(From)]
enum Myenum {
  Null,
  Bool(bool),
  Num(i32),
  Text(String)
}

Only implements From<bool>, From<i32> and From<String>, whereas before it would also implement From<()>. I tried adding the #[from] attribute to the unit variant, but then it doesn't implement From for any of the other variants, so they also need to #[from] attribute so I end up with:

#[derive(From)]
enum Myenum {
  #[from] Null,
  #[from] Bool(bool),
  #[from] Num(i32),
  #[from] Text(String)
}

Is there a better way to do this?

@JelteF
Copy link
Owner

JelteF commented Sep 5, 2024

Yeah, this these derives for () were removed by default as the feature was considered pretty useless: #106

What did you use it for?

@JelteF
Copy link
Owner

JelteF commented Sep 5, 2024

Regarding the easiest to maintain solution: Depending on how many times you need this, it be just be the easiest to implement the From implementation for () yourself manually.

@JelteF JelteF added the moreinfo label Sep 8, 2024
@sjunepark
Copy link

sjunepark commented Sep 10, 2024

Manual implementation is not hard, but in my case, I wanted to achieve something like Display

#[derive(Display)]
#[display("{_variant}")]
enum Inner {
    Y,
    K,
    N,
    E,
}

I'm not sure if allowing From to handle unit variants will be appropriate, but at least an error would have been nice. (For example, if I try to derive an AsRef on a enum, it errors.)
Applying From when there are unit variants show no errors at all, and I found out nothing was being created when using cargo expand

@spearman
Copy link
Author

Yeah, this these derives for () were removed by default as the feature was considered pretty useless: #106

What did you use it for?

I looked at where I was using it and really the only advantage is that it's usually shorter to type ().into() than to type out the full enum variant (and path to bring it into scope). On the other hand it's a bit less clear so I think I prefer giving the name of the enum. I've actually removed it from my code now so it's no longer an issue for me. You can close this if you want.

@JelteF
Copy link
Owner

JelteF commented Sep 16, 2024

@sjunepark You can use FromStr for the exact conversion you're describing.

@spearman thank you, then I'll close this issue.

@JelteF JelteF closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2024
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

4 participants