-
Notifications
You must be signed in to change notification settings - Fork 24
Implement TryFrom<&str> for numerical types, bool and char #132
Copy link
Copy link
Closed as not planned
Labels
T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
Description
Proposal
Implement the trait TryFrom<&'a str> for numerical types, bool and char.
Problem statement
The given types implement the FromStr trait; but that can't be used in contexts that require generic TryFrom interface, which is the standard fallible conversion in Rust.
Motivation, use-cases
struct Dummy<T>(T);
impl<'a, T: TryFrom<&'a str>> TryFrom<&'a str> for Dummy<'a> {
type Error = T::Error;
fn try_from(s: &'a str) -> Result<Self, Error> {
Dummy(T::try_from(s)?)
}
}Then, one could just do
let dummy: Dummy<u64> = s.try_into();Solution sketches
Mimic the FromStr implementation for the given types.
Links and related work
A PR implementing this is available here.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
Type
Fields
Give feedbackNo fields configured for issues without a type.