Skip to content

ACP: Document how escape_debug escapes characters. #735

@tmccombs

Description

@tmccombs

Proposal

Problem statement

It is often desirable to escape a string in such a way that non-printable chracters, and some special characters like " and ' are escaped, but
most unicode printable characters can be printed as is.

Motivating examples or use cases

~~Consider https://github.com/sharkdp/fd/pull/1866/changes#diff-0d5151a0c02926abbc8a38d7c87533a96b33d24923034c0b0805d3539543b466R32-R36 .

In this case, I want to serialize some content for JSON. The json object is pretty simple, so pulling in a full serialization library, like serde-json is overkill. However, I do need to escape a string in a way that is usable in json strings.

escape_default would technically work, but it isn't ideal, because it would escape a lot of characters that I don't actually need escaped, and is thus less user-friendly if a human actually reads the resulting Json.

While this is useful for escaping in json, there are many other contexts where this kind of escaping is necessary, and could be useful.~~

Edit: this doesn't actually work for my intended use case. But I still think it would be useful to document it better.

Solution sketch

Add documentation to impl Debug for str, str::escape_debug and char::escape_debug that describes the escaping that is done by those functions, and commit to that escaping being stable going forward. This documentation may something to the effect of:

The following chars will be escaped in the following ways:

  • \n, \t, \r, \0, are escaped using the escapes used to represent them here (ex "\\n" for a newline). That is, the same backslash escape that can be used in a rust string literal.
  • ", ', and \ will be escaped by preceding the character with a "" (Which quote(s) is escaped currently depends on wither we are escaping a str or a char).
  • non-printable (unicode) characters will be escaped using the unicode escape for that code point (as with char::escape_unicode).

Alternatives

  • Instead of documenting the Debug implementation and the escape_debug functions. A new function could be added specifically to provide a stable interface. That way, the existing functions, and Debug implementation could be changed in the future, while still giving applications a garantee of the escaping behavior for a desirable escape function. However, this would create a more complicated API, and could be confusing on when you should use this new function vs. when you should use escape_debug.
  • escape_debug could be more fully specified (for both str and char), but leave the impl Debug unspecified, and open to change in the future.
  • Recommend using a third-party crate if stable and/or well specified behavior is desired. This allows flexibility in changing the output of these functions in the future. But it also seems wasteful to require adding a dependency when std already includes a suitable implementation that simply isn't guaranteed to remain suitable.
  • Recommend using escape_default. As mentioned, this often escapes all non-ascii characters, and it is often desirable not to escape printable non-ascii characters.

I personally don't have a strong preference between the proposed solution and the first two alternatives, I think any of those options would be fine.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions