This was briefly discussed here although with a wildcard of requested implementations Impls of TryFrom · Issue #50212 · rust-lang/rust · GitHub with the suggestion of making issues for specific implementations. It looks like the processes have changed since (?), so I'm posting here as instructed by the github issue submission templates.
Although that might not have been the meaning, my request is specific: all types currently implementing FromStr
in the current library.
FromStr
is currently used as a compatibility baseline for various text parsing libraries (for instance command line parsing, query string parsing, regex parsing). But because it doesn't expose the lifetime parameter, all non-transformative implementations must allocate.
AFAIK TryFrom
was at least in part intended as a replacement for FromStr
, but also AFAIK none of the basic types in the library implement it whereas they do implement FromStr
.
The primary objection from the original issue is that the interface is too simple whereas parsing can be very complex and need more nuanced control or be context-specific (if I understand correctly). My response is:
- Some parsing is very simple, or there's an obvious default parsing, or there's a canonical string parsing. For instance I can't think of any other reasonable representations for
f32
other than\d+(.\d*)?
and afrom_str
-like method works great - If more nuanced parsing is required, that can just be a separate non-trait (i.e. non-standardized) method. Like
u32
hasfrom_str_radix
which coexists withfrom_str
. - This seems like as much an objection to
FromStr
as toTryFrom<&str>
, and I'm not aware of significant issues with the former. From consistency standpoint, I don't believe this should be rejected on those grounds.
There might be an objection that this shouldn't be in the standard library but in a 3rd party library. Implementations of TryFrom
must be in the standard library to be implemented for base types. A new 3rd party crate could be made with a new TryFrom
trait, but it would be DOA trying to compete with TryFrom
which has the authority of the standard library.