Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Display implementation #20

Closed
joboet opened this issue Jun 23, 2021 · 2 comments
Closed

Add Display implementation #20

joboet opened this issue Jun 23, 2021 · 2 comments

Comments

@joboet
Copy link

joboet commented Jun 23, 2021

It would be useful to be able to print strings directly without converting them to a String, thereby avoiding the allocation.

While trying to implement this locally, I noticed that implementing Display automatically implies a ToString implementation, which conflicts with the current to_string functions as they return an error when encountering invalid code units, whereas my Display implementation performs the conversion lossily. Therefore, implementing Display would be a breaking change.

Solutions I could think of:

  • Rename the conversion methods
    Pro: makes displaying strings convenient
    Con: breaks the API and does not follow the naming guidelines
  • Add a display method similar to Path::display
    Pro: allows configuring different replacement options, like escaping invalid units
    Con: is not as straightforward and complicates usage when using proc-macros like thiserror
@yuandrew
Copy link

I agree that having a Display impl would be very useful.

I also think the default Debug impl should be swapped out for something more similar to the regular String debug implementation, instead of printing the underlying Vec as raw bytes.

@starkat99
Copy link
Owner

I've added display method to the strings. The new Display object by default does a replacement character for invalid values, with an {:#} alternate formatting option that entirely skips invalid values when printing. If you have better ideas for the alternate formatting, let me know, I couldn't think of anything else particularly useful off the top of my head.

I've also improved the Debug implementations. They now do standard escape_debug style strings on valid UTF data, just like std strings, but invalid values are displayed as \<XXXX> (XXXX being hex value), an intentionally invalid escape sequence that makes it relatively easy to see the raw invalid values.

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

No branches or pull requests

3 participants