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

Implement std::convert::TryFrom<&str> for EnumString #107

Closed
dspicher opened this issue Aug 18, 2020 · 4 comments · Fixed by #186
Closed

Implement std::convert::TryFrom<&str> for EnumString #107

dspicher opened this issue Aug 18, 2020 · 4 comments · Fixed by #186

Comments

@dspicher
Copy link
Contributor

The EnumString macro provides std::str::FromStr.

I wonder whether it would be possible to also provide an implementation of the nearly equivalent std::convert::TryFrom<&str>?

@dspicher dspicher changed the title Implement std::convert::TryFrom<&str> for EnumString Implement std::convert::TryFrom<&str> for EnumString Aug 18, 2020
@twz123
Copy link

twz123 commented Sep 14, 2020

Related: rust-lang/api-guidelines#182

@Peternator7
Copy link
Owner

This is probably doable. Our MSRV is 1.31 and TryFrom wasn't stablized until 1.34, but I believe there are some workarounds with cfg attrs to only include features for specific versions.

@dspicher
Copy link
Contributor Author

Great, I will try to take a stab at this.

@dspicher
Copy link
Contributor Author

dspicher commented Oct 19, 2021

The implementation is simple enough, I have coded up something here:
dspicher@d5e16d4

However, the requirement to make this work with Rust 1.31 (or rather 1.32 as is stated in the current strum README) is more difficult. cfg_version is still unstable. There is the rustversion crate one could use. However, this would force all users of EnumString to take this crate up in their dependencies once the macro is expanded. Or is there any way I don't know of to circumvent this? EDIT: Solved by inlining rustversion usage into the strum crate by annotating local methods. Thx @toxeus for the hint.

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

Successfully merging a pull request may close this issue.

3 participants