Skip to content

Implement TryFrom<&str> for numerical types, bool and char #132

Description

@grego

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions