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

Windows: Use WriteFile to write to a UTF-8 console #134622

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

ChrisDenton
Copy link
Member

@ChrisDenton ChrisDenton commented Dec 21, 2024

If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling WriteConsole.

@rustbot
Copy link
Collaborator

rustbot commented Dec 21, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 21, 2024
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

I don't really have enough context to solidly say "yes" but the code changes/comments seem reasonable. r=me unless you feel we need someone more familiar with Windows etc.

/// Returns true if the attached console's code page is currently UTF-8.
#[cfg(not(target_vendor = "win7"))]
fn is_utf8_console() -> bool {
unsafe { c::GetConsoleOutputCP() == c::CP_UTF8 }
Copy link
Member

Choose a reason for hiding this comment

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

Is this cheap enough to call on every write? I guess we're already calling GetConsoleMode on every write...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I don't love it but it is cheap. Especially when compared to the previous code where if is_console is true then it will be doing a full UTF-8 to UTF-16 conversion using a small buffer (necessitating multiple calls to write).

fn write(handle_id: u32, data: &[u8], incomplete_utf8: &mut IncompleteUtf8) -> io::Result<usize> {
if data.is_empty() {
return Ok(0);
}

let handle = get_handle(handle_id)?;
if !is_console(handle) {
if !is_console(handle) || is_utf8_console() {
Copy link
Member

Choose a reason for hiding this comment

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

Should we be worried about race conditions at all with changes in the mode setting? My sense is probably no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not. It is possible that the code page could be changed after is_utf8_console and before the write. But changing the console code page is like changing any global state (e.g. an environment variable or the current directory). That is, it's the sort of thing you maybe set once on startup and don't touch again (unless you're very careful).

@ChrisDenton
Copy link
Member Author

@bors r=Mark-Simulacrum

@bors
Copy link
Contributor

bors commented Dec 27, 2024

📌 Commit 1e3ecd5 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 27, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 27, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#134606 (ptr::copy: fix docs for the overlapping case)
 - rust-lang#134622 (Windows: Use WriteFile to write to a UTF-8 console)
 - rust-lang#134759 (compiletest: Remove the `-test` suffix from normalize directives)
 - rust-lang#134787 (Spruce up the docs of several queries related to the type/trait system and const eval)
 - rust-lang#134806 (rustdoc: use shorter paths as preferred canonical paths)
 - rust-lang#134815 (Sort triples by name in platform_support.md)
 - rust-lang#134816 (tools: fix build failure caused by PR rust-lang#134420)
 - rust-lang#134819 (Fix mistake in windows file open)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7bbbfc6 into rust-lang:master Dec 27, 2024
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 27, 2024
Rollup merge of rust-lang#134622 - ChrisDenton:write-file-utf8, r=Mark-Simulacrum

Windows: Use WriteFile to write to a UTF-8 console

If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling `WriteConsole`.
poliorcetics pushed a commit to poliorcetics/rust that referenced this pull request Dec 28, 2024
…k-Simulacrum

Windows: Use WriteFile to write to a UTF-8 console

If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling `WriteConsole`.
poliorcetics pushed a commit to poliorcetics/rust that referenced this pull request Dec 28, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#134606 (ptr::copy: fix docs for the overlapping case)
 - rust-lang#134622 (Windows: Use WriteFile to write to a UTF-8 console)
 - rust-lang#134759 (compiletest: Remove the `-test` suffix from normalize directives)
 - rust-lang#134787 (Spruce up the docs of several queries related to the type/trait system and const eval)
 - rust-lang#134806 (rustdoc: use shorter paths as preferred canonical paths)
 - rust-lang#134815 (Sort triples by name in platform_support.md)
 - rust-lang#134816 (tools: fix build failure caused by PR rust-lang#134420)
 - rust-lang#134819 (Fix mistake in windows file open)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library 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