Skip to content

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

Closed as not planned
@grego

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-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