-
Notifications
You must be signed in to change notification settings - Fork 20
Implement TryFrom<&str> for numerical types, bool and char #132
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
Comments
Your motivation demonstrates how to use this, but not really why you would want to do it this way. |
|
I am skeptical that we would want to add any of these impls, especially as your last comment explicitly refers to parsing. Parsing and From conversions are semantically different things. [the difference]I've found it's easy to understand the difference in the context of something like let v: serde_json::Value = "[1, 2, 3, 4]".parse()?; The above uses On the other hand: let v: serde_json::Value = true.into();
let v: serde_json::Value = 100i32.into();
let v: serde_json::Value = "string".into(); The above uses There is a blanket impl for If the motivation for this feature request is that having a lifetime available (in contrast to |
I did not want to say the |
Why just primitive types? If your argument holds, shouldn't we add a blanket impl that makes TryFrom<&str> work on all FromStr? In general I agree with the sentiment that From is for conversion and FromStr is for parsing. |
No. |
Ah, bummer. Then yeah, I agree that this ACP's API would be confusing. |
I've wanted to figure out a more fleshed-out I don't think we should do this for that reason. |
Proposal
Implement the trait
TryFrom<&'a str>
for numerical types,bool
andchar
.Problem statement
The given types implement the
FromStr
trait; but that can't be used in contexts that require genericTryFrom
interface, which is the standard fallible conversion in Rust.Motivation, use-cases
Then, one could just do
Solution sketches
Mimic the
FromStr
implementation for the given types.Links and related work
A PR implementing this is available here.
The text was updated successfully, but these errors were encountered: