modeled_types: Use FromStr instead of TryFrom
Issue number:
Closes #1061
Description of changes:
In order to clean up the code, this change replaces TryFrom<&str>
with FromStr. This change has been made for all modeled_types.
Testing done:
- [x] performed a complete build of
aws-devwith no errors - [x] performed a complete build of
metal-devwith no errors - [x] performed a complete build of
vmware-devwith no errors
Terms of contribution:
By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.
Force push: fix try_into calls that escaped local testing
Force push: fix more try_into calls that escaped local testing
Force push: clear up rustfmt errors
I'm a little curious about this motivation behind this change!
Also, assuming this all came up via compiler errors, but one nice thing about implementing TryFrom means you can use the reciprocal TryInto. Are you sure we're not calling try_into() in places that would require the TryFrom implementation?
I'm a little curious about this motivation behind this change!
Thanks for your interest in this change! The two people who initially recommended this change are no longer on the team, so I will provide what I believe is the reason for this change.
I believe the reasons for this change are discussed in the following posts:
Essentially, it is more idiomatic to convert from strings using FromStr because doing so allows you to use the default string parsing syntax (string_variable.parse()). In fact, I'm in the middle of changing all of the from_str() calls in this PR to string_variable.parse() calls. :)
Also, assuming this all came up via compiler errors, but one nice thing about implementing
TryFrommeans you can use the reciprocalTryInto. Are you sure we're not callingtry_into()in places that would require theTryFromimplementation?
I only replaced TryFrom<&str> calls that I was sure were conversions from strings, and left everything else untouched (e.g. conversions from custom types such as FriendlyVersion)
The TryFrom implementation that the macro creates for custom types in modeled_types is for the String type (permalink). That TryFrom<String> implementation actually just used to call TryFrom<&str> under the hood, and each modeled_types custom type implemented the TryFrom<&str> trait.
Now, each modeled_types custom type implements FromStr, which allows us to call .parse() from within the macro instead of TryFrom<&str>'s try_from().
Force push: changed all ::from_str() calls to .parse::<Type>() calls.
Force push: typo