Skip to content

Commit

Permalink
winapi -> windows-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
th1000s committed Feb 1, 2024
1 parent f29f001 commit a671258
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ termcolor = "1.1.0"
textwrap = { version = "0.16.0", default-features = false }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", features = ["handleapi"] }
windows-sys = { version = "0.52", features = ["Win32_Foundation", "Win32_System_Console", "Win32_System_Threading"] }

[target.'cfg(not(target_os = "windows"))'.dependencies]
libc = "0.2.151"
Expand Down
26 changes: 13 additions & 13 deletions crates/core/ctrlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ mod ctrlc {
mod windows {
use super::*;

use winapi::shared::minwindef::{BOOL, DWORD, FALSE, TRUE};
use winapi::um::consoleapi::{SetConsoleCtrlHandler, WriteConsoleA};
use winapi::um::handleapi::DuplicateHandle;
use winapi::um::processenv::GetStdHandle;
use winapi::um::processthreadsapi::{
use windows_sys::Win32::Foundation::{
DuplicateHandle, BOOL, DUPLICATE_SAME_ACCESS, FALSE, HANDLE, TRUE,
};
use windows_sys::Win32::System::Console::{
GetStdHandle, SetConsoleCtrlHandler, WriteConsoleA, CTRL_C_EVENT,
STD_OUTPUT_HANDLE,
};
use windows_sys::Win32::System::Threading::{
GetCurrentProcess, GetCurrentThread, SuspendThread,
};
use winapi::um::winbase::STD_OUTPUT_HANDLE;
use winapi::um::wincon::CTRL_C_EVENT;
use winapi::um::winnt::{DUPLICATE_SAME_ACCESS, HANDLE};

pub type ThreadType = HANDLE;

Expand All @@ -280,7 +280,7 @@ mod ctrlc {
const ANSI_RESET: &str = "\u{1B}[00m^C";

pub(super) fn thread_self() -> ThreadId {
let mut this_thread: ThreadType = std::ptr::null_mut();
let mut this_thread: ThreadType = 0;

// SAFETY: GetCurrentThread/Process can not fail. The thread handle on a failed
// DuplicateHandle() call is not used.
Expand All @@ -303,7 +303,7 @@ mod ctrlc {
})
}

extern "system" fn on_ctrlc(event_type: DWORD) -> BOOL {
extern "system" fn on_ctrlc(event_type: u32) -> BOOL {
if event_type == CTRL_C_EVENT {
// Observed behavior: When in a Ctrl-C handler (i.e. this), resetting it so the
// next ^C is not handled by it does not work, this handler has to run to
Expand All @@ -325,16 +325,16 @@ mod ctrlc {
// SAFETY: Only a valid handle is used later.
let stdout_handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };

if stdout_handle != std::ptr::null_mut() {
let mut _bytes_written: DWORD = 0;
if stdout_handle != 0 {
let mut _bytes_written: u32 = 0;

// Short writes or other errors are ignored.
// SAFETY: correctness of `lpBuffer` and `nNumberOfCharsToWrite` is ensured by Rust.
let _ = unsafe {
WriteConsoleA(
stdout_handle,
ANSI_RESET.as_ptr() as *const _,
ANSI_RESET.len() as DWORD,
ANSI_RESET.len() as u32,
&mut _bytes_written,
std::ptr::null_mut(),
)
Expand Down

0 comments on commit a671258

Please sign in to comment.