Skip to content

Add a write_char method to std::fmt::Formatter. #24689

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 2 commits into from
Jun 10, 2015

Conversation

SimonSapin
Copy link
Contributor

This is the logical next step after #24661, but I’m less sure about this one.

r? @alexcrichton

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@alexcrichton
Copy link
Member

Hm can you clarify why you'd want this as an inherent method? It seems like a similar win is gained from implementing fmt::Write. The main reason fmt::Write isn't leveraged for write_fmt is that it would require an import of a somewhat obscure trait in lots of code, but I think that write_char is pretty rare by comparison.

@SimonSapin
Copy link
Contributor Author

I only made it an inherent method because write_str was as well, and this seemed like the smaller change. Note that Formatter currently does not implement any trait.

I don’t have a strong opinion on how to do this, or if it should be done at all.

@alexcrichton
Copy link
Member

I think it would be safe to provide an implementation of the trait for Formatter, but I think we may want to hold off adding a method for now.

@alexcrichton alexcrichton added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. I-needs-decision Issue: In need of a decision. labels May 26, 2015
@alexcrichton
Copy link
Member

@SimonSapin could you try adding this method through an implementation of the Write trait on the Formatter struct directly? I think it should have the implementation anyway and seems like a conservative way to expose the functionality.

@alexcrichton alexcrichton removed the I-needs-decision Issue: In need of a decision. label Jun 10, 2015
This is the logical next step after rust-lang#24661, but I’m less sure about this one.
@SimonSapin SimonSapin force-pushed the formatter-write-char branch from b3c46d0 to 10fbce3 Compare June 10, 2015 19:42
@SimonSapin
Copy link
Contributor Author

Done. (Now the trait has to be in scope to use that method, of course.)

@@ -892,6 +892,20 @@ impl<'a> Formatter<'a> {
}
}

impl<'a> Write for Formatter<'a> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also tag this with #[stable(since = "1.2.0", ...)]?

@alexcrichton
Copy link
Member

r=me with a stability tag, thanks @SimonSapin!

@SimonSapin SimonSapin force-pushed the formatter-write-char branch from 10fbce3 to 63da18b Compare June 10, 2015 20:06
@alexcrichton
Copy link
Member

@bors: r+ 63da18b

bors added a commit that referenced this pull request Jun 10, 2015
This is the logical next step after #24661, but I’m less sure about this one.

r? @alexcrichton
@bors
Copy link
Collaborator

bors commented Jun 10, 2015

⌛ Testing commit 63da18b with merge db0c1cb...

@bors bors merged commit 63da18b into rust-lang:master Jun 10, 2015
@SimonSapin SimonSapin deleted the formatter-write-char branch June 10, 2015 22:57
let mut utf8 = [0; 4];
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
let s: &str = unsafe { mem::transmute(&utf8[..amt]) };
Display::fmt(s, f)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, my mistake. Feel free to reverse this chunk if there isn’t a better solution.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jul 1, 2015
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625
bors added a commit that referenced this pull request Jul 1, 2015
This recently regressed in #24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes #26625
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jul 8, 2015
This recently regressed in rust-lang#24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes rust-lang#26625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants