Skip to content

std: Enforce Unicode in fmt::Writer #20377

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

Merged
merged 1 commit into from
Jan 2, 2015
Merged

Conversation

alexcrichton
Copy link
Member

This commit is an implementation of RFC 526 which is a change to alter
the definition of the old fmt::FormatWriter. The new trait, renamed to
Writer, now only exposes one method write_str in order to guarantee that all
implementations of the formatting traits can only produce valid Unicode.

One of the primary improvements of this patch is the performance of the
.to_string() method by avoiding an almost-always redundant UTF-8 check. This
is a breaking change due to the renaming of the trait as well as the loss of the
write method, but migration paths should be relatively easy:

  • All usage of write should move to write_str. If truly binary data was
    being written in an implementation of Show, then it will need to use a
    different trait or an altogether different code path.
  • All usage of write! should continue to work as-is with no modifications.
  • All usage of Show where implementations just delegate to another should
    continue to work as-is.

[breaking-change]

Closes #20352

@rust-highfive
Copy link
Contributor

r? @erickt

(rust_highfive has picked a reviewer for you, use r? to override)

@frewsxcv
Copy link
Member

This needs a rebase

This commit is an implementation of [RFC 526][rfc] which is a change to alter
the definition of the old `fmt::FormatWriter`. The new trait, renamed to
`Writer`, now only exposes one method `write_str` in order to guarantee that all
implementations of the formatting traits can only produce valid Unicode.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md

One of the primary improvements of this patch is the performance of the
`.to_string()` method by avoiding an almost-always redundant UTF-8 check. This
is a breaking change due to the renaming of the trait as well as the loss of the
`write` method, but migration paths should be relatively easy:

* All usage of `write` should move to `write_str`. If truly binary data was
  being written in an implementation of `Show`, then it will need to use a
  different trait or an altogether different code path.

* All usage of `write!` should continue to work as-is with no modifications.

* All usage of `Show` where implementations just delegate to another should
  continue to work as-is.

[breaking-change]

Closes rust-lang#20352
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 2, 2015
@bors bors merged commit e423fcf into rust-lang:master Jan 2, 2015
@alexcrichton alexcrichton deleted the issue-20352 branch January 3, 2015 00:02
@SimonSapin
Copy link
Contributor

@alexcrichton Was it deliberate to omit the write_char method described in the RFC? (If so, that’s fine, just wondering.)

@alexcrichton
Copy link
Member Author

Oops, I completely forgot about that! I'll try to add it soon.

@SimonSapin
Copy link
Contributor

I’m personally still undecided whether it should be there, so I’ll leave the decision to you :)

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

Successfully merging this pull request may close these issues.

Tracking issue for Unicode enforcement in std::fmt (RFC 526)
7 participants