Add TryFrom<&str> to EnumString#186
Conversation
81781f2 to
55e00c9
Compare
Peternator7
left a comment
There was a problem hiding this comment.
Addition of rustversion crate makes sense. Could you update the docs as well to say this is implemented when rustc >= 1.34?
| ); | ||
|
|
||
| Ok(std::iter::FromIterator::from_iter( | ||
| vec![from_str, try_from_str].into_iter(), |
There was a problem hiding this comment.
Can we do this?
Ok(quote! {
#from_str
#try_from_str
})There was a problem hiding this comment.
of course, much nicer!
strum_tests/tests/from_str.rs
Outdated
| Black, | ||
| } | ||
|
|
||
| macro_rules! assert_from_str { |
There was a problem hiding this comment.
This probably needs to account for rustc version as well.
There was a problem hiding this comment.
Ok, I added rust version-dependent macros.
There was a problem hiding this comment.
Got rid of the macros altogether and used normal functions with appropriate trait bounds.
|
Thanks for the fast review @Peternator7, I just force-pushed some fixes. |
| ) -> TokenStream { | ||
| quote! { | ||
| #[allow(clippy::use_self)] | ||
| impl #impl_generics ::std::convert::TryFrom<&str> for #name #ty_generics #where_clause { |
There was a problem hiding this comment.
Maybe using AsRef<str> would be even more useful?
impl<T: AsRef<str>> #impl_generics ::std::convert::TryFrom<T> for #name #ty_generics #where_clause {There was a problem hiding this comment.
This runs into the following interesting compiler bug:
rust-lang/rust#50133
See e.g. here for our very specific case: rust-lang/rust#50133 (comment)
Leaving it out for now, since the workaround looks annoying to use.
The implementation simply delegates to `std::str::FromStr`. To maintain compatibility with Rust 1.32, a new dependency is introduced which allows us to emit the `TryFrom<&str>` implementation only for Rust 1.34 and above when `std::convert::TryFrom` was stabilized.
Sorry I had missed this, updated the docs now. |
|
This looks good to me. Anything you wanted to add or is this ready to merge? |
No, good to go from my side. |
|
Released as part of 0.23. Thanks for the PR. https://crates.io/crates/strum |
The implementation simply delegates to
std::str::FromStr.To maintain compatibility with Rust 1.32, a new dependency
is introduced which allows us to emit the
TryFrom<&str>implementation only for Rust 1.34 and above when
std::convert::TryFromwas stabilized.Closes #107.