Skip to content

Commit a13ea5a

Browse files
authoredJan 5, 2025··
deps: switch from winapi to windows-sys (#26)
1 parent 6984714 commit a13ea5a

File tree

4 files changed

+51
-78
lines changed

4 files changed

+51
-78
lines changed
 

‎Cargo.lock

+1-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+8-12
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ version = "0.29.0"
2929
default-features = false
3030
features = ["fs", "term"]
3131

32-
[target.'cfg(windows)'.dependencies.winapi]
33-
version = "0.3.9"
32+
[target.'cfg(windows)'.dependencies.windows-sys]
33+
version = "0.59.0"
34+
default-features = false
3435
features = [
35-
"consoleapi",
36-
"errhandlingapi",
37-
"handleapi",
38-
"impl-default",
39-
"lmapibuf",
40-
"lmserver",
41-
"lmwksta",
42-
"processenv",
43-
"winbase",
44-
"wincon",
36+
"Win32_Foundation",
37+
"Win32_System_Console",
38+
"Win32_System_SystemInformation",
39+
"Win32_System_SystemServices",
40+
"Win32_NetworkManagement_NetManagement",
4541
]

‎examples/clscli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() -> Result<(), Error> {
3535

3636
Ok(())
3737
} else {
38-
println!("Usage: cargo run --example cli -- <variant>\nWhere <variant> is one of the ClearScreen enum variants, same casing, or 'auto'.\nI recommend piping into `hexdump -C` to see what’s happening.");
38+
println!("Usage: cargo run --example clscli -- <variant>\nWhere <variant> is one of the ClearScreen enum variants, same casing, or 'auto'.\nI recommend piping into `hexdump -C` to see what’s happening.");
3939
Ok(())
4040
}
4141
}

‎src/lib.rs

+41-42
Original file line numberDiff line numberDiff line change
@@ -840,38 +840,29 @@ mod unix {
840840
mod win {
841841
use super::Error;
842842

843-
use std::{convert::TryFrom, io, mem::size_of, ptr};
844-
845-
use winapi::{
846-
shared::minwindef::{DWORD, FALSE},
847-
um::{
848-
consoleapi::{GetConsoleMode, SetConsoleMode},
849-
handleapi::INVALID_HANDLE_VALUE,
850-
lmapibuf::{NetApiBufferAllocate, NetApiBufferFree},
851-
lmserver::{NetServerGetInfo, MAJOR_VERSION_MASK, SERVER_INFO_101, SV_PLATFORM_ID_NT},
852-
lmwksta::{NetWkstaGetInfo, WKSTA_INFO_100},
853-
processenv::GetStdHandle,
854-
winbase::{VerifyVersionInfoW, STD_OUTPUT_HANDLE},
855-
wincon::{
856-
ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
857-
ENABLE_VIRTUAL_TERMINAL_PROCESSING,
858-
},
859-
winnt::{
860-
VerSetConditionMask, HANDLE, OSVERSIONINFOEXW, POSVERSIONINFOEXW, ULONGLONG,
861-
VER_GREATER_EQUAL, VER_MAJORVERSION, VER_MINORVERSION, VER_SERVICEPACKMAJOR,
862-
},
863-
},
843+
use std::{io, mem::size_of, ptr};
844+
845+
use windows_sys::Win32::Foundation::{FALSE, HANDLE, INVALID_HANDLE_VALUE};
846+
use windows_sys::Win32::NetworkManagement::NetManagement::{
847+
NetApiBufferAllocate, NetApiBufferFree, NetServerGetInfo, NetWkstaGetInfo,
848+
MAJOR_VERSION_MASK, SERVER_INFO_101, SV_PLATFORM_ID_NT, WKSTA_INFO_100,
849+
};
850+
use windows_sys::Win32::System::Console::{
851+
GetConsoleMode, GetStdHandle, SetConsoleMode, CONSOLE_MODE, ENABLE_ECHO_INPUT,
852+
ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_VIRTUAL_TERMINAL_PROCESSING,
853+
STD_OUTPUT_HANDLE,
864854
};
855+
use windows_sys::Win32::System::SystemInformation::{
856+
VerSetConditionMask, VerifyVersionInfoW, OSVERSIONINFOEXW, VER_MAJORVERSION,
857+
VER_MINORVERSION, VER_SERVICEPACKMAJOR,
858+
};
859+
use windows_sys::Win32::System::SystemServices::VER_GREATER_EQUAL;
865860

866861
#[cfg(feature = "windows-console")]
867-
use winapi::um::{
868-
wincon::{
869-
FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo,
870-
ScrollConsoleScreenBufferW, SetConsoleCursorPosition, CONSOLE_SCREEN_BUFFER_INFO,
871-
PCONSOLE_SCREEN_BUFFER_INFO,
872-
},
873-
wincontypes::{CHAR_INFO_Char, CHAR_INFO, COORD, SMALL_RECT},
874-
winnt::SHORT,
862+
use windows_sys::Win32::System::Console::{
863+
FillConsoleOutputAttribute, FillConsoleOutputCharacterW, GetConsoleScreenBufferInfo,
864+
ScrollConsoleScreenBufferW, SetConsoleCursorPosition, CHAR_INFO, CHAR_INFO_0,
865+
CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT,
875866
};
876867

877868
fn console_handle() -> Result<HANDLE, Error> {
@@ -883,7 +874,7 @@ mod win {
883874

884875
#[cfg(feature = "windows-console")]
885876
fn buffer_info(console: HANDLE) -> Result<CONSOLE_SCREEN_BUFFER_INFO, Error> {
886-
let csbi: PCONSOLE_SCREEN_BUFFER_INFO = ptr::null_mut();
877+
let csbi: *mut CONSOLE_SCREEN_BUFFER_INFO = ptr::null_mut();
887878
if unsafe { GetConsoleScreenBufferInfo(console, csbi) } == FALSE {
888879
return Err(io::Error::last_os_error().into());
889880
}
@@ -928,12 +919,13 @@ mod win {
928919
// Scroll it upwards off the top of the buffer with a magnitude of the entire height.
929920
let target = COORD {
930921
X: 0,
931-
Y: (0 - csbi.dwSize.Y) as SHORT,
922+
Y: (0 - csbi.dwSize.Y) as i16,
932923
};
933924

934925
// Fill with empty spaces with the buffer’s default text attribute.
935-
let mut space = CHAR_INFO_Char::default();
936-
unsafe { *space.AsciiChar_mut() = b' ' as i8 };
926+
let space = CHAR_INFO_0 {
927+
AsciiChar: b' ' as i8,
928+
};
937929

938930
let fill = CHAR_INFO {
939931
Char: space,
@@ -1006,7 +998,7 @@ mod win {
1006998
Ok(())
1007999
}
10081000

1009-
const ENABLE_COOKED_MODE: DWORD =
1001+
const ENABLE_COOKED_MODE: CONSOLE_MODE =
10101002
ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
10111003

10121004
pub(crate) fn cooked() -> Result<(), Error> {
@@ -1031,28 +1023,35 @@ mod win {
10311023
// proper way, requires manifesting
10321024
#[inline]
10331025
fn um_verify_version() -> bool {
1034-
let condition_mask: ULONGLONG = unsafe {
1026+
let condition_mask: u64 = unsafe {
10351027
VerSetConditionMask(
10361028
VerSetConditionMask(
1037-
VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL),
1029+
VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL as u8),
10381030
VER_MINORVERSION,
1039-
VER_GREATER_EQUAL,
1031+
VER_GREATER_EQUAL as u8,
10401032
),
10411033
VER_SERVICEPACKMAJOR,
1042-
VER_GREATER_EQUAL,
1034+
VER_GREATER_EQUAL as u8,
10431035
)
10441036
};
10451037

10461038
let mut osvi = OSVERSIONINFOEXW {
10471039
dwMinorVersion: ABRACADABRA_THRESHOLD.1 as _,
10481040
dwMajorVersion: ABRACADABRA_THRESHOLD.0 as _,
10491041
wServicePackMajor: 0,
1050-
..OSVERSIONINFOEXW::default()
1042+
dwOSVersionInfoSize: size_of::<OSVERSIONINFOEXW>() as u32,
1043+
dwBuildNumber: 0,
1044+
dwPlatformId: 0,
1045+
szCSDVersion: [0; 128],
1046+
wServicePackMinor: 0,
1047+
wSuiteMask: 0,
1048+
wProductType: 0,
1049+
wReserved: 0,
10511050
};
10521051

10531052
let ret = unsafe {
10541053
VerifyVersionInfoW(
1055-
&mut osvi as POSVERSIONINFOEXW,
1054+
&mut osvi,
10561055
VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
10571056
condition_mask,
10581057
)
@@ -1249,14 +1248,14 @@ impl<'a, T: AsRef<&'a [u8]>> From<T> for ResetScrollback<'a> {
12491248
}
12501249
}
12511250

1252-
impl<'a> AsRef<[u8]> for ResetScrollback<'a> {
1251+
impl AsRef<[u8]> for ResetScrollback<'_> {
12531252
#[inline]
12541253
fn as_ref(&self) -> &[u8] {
12551254
&self.0
12561255
}
12571256
}
12581257

1259-
impl<'a> ResetScrollback<'a> {
1258+
impl ResetScrollback<'_> {
12601259
#[inline]
12611260
fn expand(&self) -> Expansion<Self> {
12621261
#[allow(dead_code)]

0 commit comments

Comments
 (0)
Please sign in to comment.