Change StringM Debug, String, and Debug representations to escaped #186
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Change StringM Debug, String, and Debug representations to escaped printable-ASCII. But keep ability to convert to/from UTF-8 Strings.
Why
The
Debug
,Display
, andFromStr
functions are intended for converting to and from a reliable String representation for the type. Today these do not render reliable String representations becauseStringM
can actually contains arbitrary byte sequences that have no representation in ASCII or UTF-8.These representations are also used in the XDR<>JSON encoding, and so there are values of StringM that are not roundtrip convertible.
It is however reasonable that someone would want to convert to and from a StringM type using Unicode/UTF-8 String values. The TryFrom/TryInto conversions with String have been retained for this, as well as some inherent functions on the StringM type. The inherent functions were renamed to make it clearer their intent.
Unfortunately this change does make the interface of StringM harder to understand. It's easy to forget that
FromStr
is for parsing strings into types, or thatDisplay
(and its auto-derived cousinToString
) are for formatting values for printing, and that both of those traits are good for converting to a reliable format. These traits are not great for conversion to and from unreliable conversions where conversion is not necessarily supported for all values.There might be a way for us to improve this in a future release and reduce the ambiguity. An option might be to remove the conversions to and from String, and require folks to go through bytes first.