Skip to content

Restore compatibility with windows-sys 0.52 #238

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/anstream/src/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
S: anstyle_wincon::WinconStream,
S: IsTerminal,
{
/// Returns `true` if the descriptor/handle refers to a terminal/tty.
#[inline]
pub fn is_terminal(&self) -> bool {
self.raw.is_terminal()
Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle-query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pre-release-replacements = [
]

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = ["Win32_System_Console", "Win32_Foundation"] }
windows-sys = { version = ">=0.52.0, <=0.59.*", features = ["Win32_System_Console", "Win32_Foundation"] }

[lints]
workspace = true
6 changes: 3 additions & 3 deletions crates/anstyle-query/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ mod windows_console {

fn enable_vt(handle: RawHandle) -> std::io::Result<()> {
unsafe {
let handle: HANDLE = std::mem::transmute(handle);
if handle.is_null() {
return Err(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"console is detached",
));
}
let handle: HANDLE = handle as HANDLE;

let mut dwmode: CONSOLE_MODE = 0;
if windows_sys::Win32::System::Console::GetConsoleMode(handle, &mut dwmode) == 0 {
Expand All @@ -33,7 +33,7 @@ mod windows_console {
}
}

pub fn enable_virtual_terminal_processing() -> std::io::Result<()> {
pub(crate) fn enable_virtual_terminal_processing() -> std::io::Result<()> {
let stdout = std::io::stdout();
let stdout_handle = stdout.as_raw_handle();
let stderr = std::io::stderr();
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn enable_ansi_colors() -> Option<bool> {
windows_console::enable_ansi_colors()
}

/// Raw ENABLE_VIRTUAL_TERMINAL_PROCESSING on stdout/stderr
/// Raw `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on stdout/stderr
#[cfg(windows)]
pub fn enable_virtual_terminal_processing() -> std::io::Result<()> {
windows_console::enable_virtual_terminal_processing()
Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle-wincon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ anstyle = { version = "1.0.0", path = "../anstyle" }
lexopt = "0.3.0"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.59.0", features = ["Win32_System_Console", "Win32_Foundation"] }
windows-sys = { version = ">=0.52.0, <=0.59.*", features = ["Win32_System_Console", "Win32_Foundation"] }

[lints]
workspace = true
3 changes: 2 additions & 1 deletion crates/anstyle-wincon/examples/set-wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ fn main() -> Result<(), lexopt::Error> {
let fg = args.fg.and_then(|c| c.into_ansi());
let bg = args.bg.and_then(|c| c.into_ansi());

let _ = stdout.write_colored(fg, bg, "".as_bytes());
let _ = stdout.write_colored(fg, bg, b"");

#[allow(clippy::mem_forget)]
std::mem::forget(stdout);

Ok(())
Expand Down
16 changes: 5 additions & 11 deletions crates/anstyle-wincon/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ type StdioColorInnerResult = Result<(anstyle::AnsiColor, anstyle::AnsiColor), in
/// Cached [`get_colors`] call for [`std::io::stdout`]
pub fn stdout_initial_colors() -> StdioColorResult {
static INITIAL: std::sync::OnceLock<StdioColorInnerResult> = std::sync::OnceLock::new();
INITIAL
.get_or_init(|| get_colors_(&std::io::stdout()))
.clone()
.map_err(Into::into)
(*INITIAL.get_or_init(|| get_colors_(&std::io::stdout()))).map_err(Into::into)
}

/// Cached [`get_colors`] call for [`std::io::stderr`]
pub fn stderr_initial_colors() -> StdioColorResult {
static INITIAL: std::sync::OnceLock<StdioColorInnerResult> = std::sync::OnceLock::new();
INITIAL
.get_or_init(|| get_colors_(&std::io::stderr()))
.clone()
.map_err(Into::into)
(*INITIAL.get_or_init(|| get_colors_(&std::io::stderr()))).map_err(Into::into)
Comment on lines -12 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I applied the suggestion for clippy::clone_on_copy but I was not sure what to do about the other warning about OnceLock being too new for the MSRV:

warning: current MSRV (Minimum Supported Rust Version) is `1.66.0` but this item is stable since `1.70.0`
  --> crates\anstyle-wincon\src\windows.rs:13:10
   |
13 |         .get_or_init(|| get_colors_(&std::io::stdout()))
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv
   = note: `#[warn(clippy::incompatible_msrv)]` on by default

}

/// Apply colors to future writes
Expand Down Expand Up @@ -129,10 +123,10 @@ mod inner {
handle: RawHandle,
) -> Result<CONSOLE_SCREEN_BUFFER_INFO, IoError> {
unsafe {
let handle: HANDLE = std::mem::transmute(handle);
if handle.is_null() {
return Err(IoError::BrokenPipe);
}
let handle: HANDLE = handle as HANDLE;

let mut info: CONSOLE_SCREEN_BUFFER_INFO = std::mem::zeroed();
if windows_sys::Win32::System::Console::GetConsoleScreenBufferInfo(handle, &mut info)
Expand All @@ -150,10 +144,10 @@ mod inner {
attributes: CONSOLE_CHARACTER_ATTRIBUTES,
) -> Result<(), IoError> {
unsafe {
let handle: HANDLE = std::mem::transmute(handle);
if handle.is_null() {
return Err(IoError::BrokenPipe);
}
let handle: HANDLE = handle as HANDLE;

if windows_sys::Win32::System::Console::SetConsoleTextAttribute(handle, attributes) != 0
{
Expand Down Expand Up @@ -258,7 +252,7 @@ mod inner {
for expected in COLORS {
let nibble = to_nibble(expected);
let actual = from_nibble(nibble);
assert_eq!(expected, actual, "Intermediate: {}", nibble);
assert_eq!(expected, actual, "Intermediate: {nibble}");
}
}
}