From 451777f85a40b71f2e960ca35164f720bd724800 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 29 Nov 2022 13:42:51 +0800 Subject: [PATCH 1/2] Switch from winapi to windows-sys --- Cargo.toml | 4 ++-- src/windows/localize.rs | 6 ++---- src/windows/normalize.rs | 13 +------------ tests/integration.rs | 2 +- 4 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 836ff64..ec2db4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ print_bytes = { version = "0.7", optional = true } serde = { version = "1.0", optional = true } [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = ["fileapi"] } +windows-sys = { version = "0.42", features = ["Win32_Foundation", "Win32_Storage_FileSystem"] } [dev-dependencies] bincode = "1.3" @@ -34,4 +34,4 @@ tempfile = "3.2" libc = "0.2" [features] -localization = ["winapi/shellapi"] +localization = ["windows-sys/Win32_UI_Shell", "windows-sys/Win32_UI_WindowsAndMessaging"] diff --git a/src/windows/localize.rs b/src/windows/localize.rs index 8cc3afc..cea69c7 100644 --- a/src/windows/localize.rs +++ b/src/windows/localize.rs @@ -5,9 +5,7 @@ use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; use std::path::Path; -use winapi::shared::minwindef::UINT; -use winapi::um::shellapi::SHGetFileInfoW; -use winapi::um::shellapi::SHGFI_DISPLAYNAME; +use windows_sys::Win32::UI::Shell::{SHGetFileInfoW, SHGFI_DISPLAYNAME}; pub(super) fn name(path: &Path) -> Option { let mut path: Vec<_> = path.as_os_str().encode_wide().collect(); @@ -22,7 +20,7 @@ pub(super) fn name(path: &Path) -> Option { path.as_ptr(), 0, path_info.as_mut_ptr(), - mem::size_of_val(&path_info) as UINT, + mem::size_of_val(&path_info) as _, SHGFI_DISPLAYNAME, ) }; diff --git a/src/windows/normalize.rs b/src/windows/normalize.rs index 66927cd..b8e9b26 100644 --- a/src/windows/normalize.rs +++ b/src/windows/normalize.rs @@ -1,7 +1,6 @@ use std::env; use std::ffi::OsString; use std::io; -use std::mem; use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; use std::path::Component; @@ -11,18 +10,11 @@ use std::path::Prefix; use std::path::PrefixComponent; use std::ptr; -use winapi::shared::minwindef::DWORD; -use winapi::um::fileapi::GetFullPathNameW; +use windows_sys::Win32::Storage::FileSystem::GetFullPathNameW; use super::BasePath; use super::BasePathBuf; -macro_rules! static_assert { - ( $condition:expr ) => { - const _: () = assert!($condition, "static assertion failed"); - }; -} - const SEPARATOR: u16 = b'\\' as _; pub(super) fn is_base(path: &Path) -> bool { @@ -104,9 +96,6 @@ pub(super) fn normalize_virtually( break Err(io::Error::last_os_error()); } - // This assertion should never fail. - static_assert!(mem::size_of::() <= mem::size_of::()); - let length = capacity as usize; if let Some(mut additional_capacity) = length.checked_sub(buffer.capacity()) diff --git a/tests/integration.rs b/tests/integration.rs index 229b150..242b755 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -14,7 +14,7 @@ mod common; #[cfg(windows)] #[rustfmt::skip] -use winapi::shared::winerror::ERROR_INVALID_NAME; +use windows_sys::Win32::Foundation::ERROR_INVALID_NAME; #[cfg(not(windows))] use libc::ENOENT as ERROR_INVALID_NAME; From 82e184dcc84f6fb98bea548da1204dca27b36ecc Mon Sep 17 00:00:00 2001 From: dylni <46035563+dylni@users.noreply.github.com> Date: Fri, 2 Dec 2022 21:50:49 -0500 Subject: [PATCH 2/2] Adjust "windows-sys" usage --- Cargo.toml | 3 ++- src/windows/localize.rs | 3 ++- src/windows/normalize.rs | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec2db4f..1e7afd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,11 +24,12 @@ print_bytes = { version = "0.7", optional = true } serde = { version = "1.0", optional = true } [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.42", features = ["Win32_Foundation", "Win32_Storage_FileSystem"] } +windows-sys = { version = "0.42", features = ["Win32_Storage_FileSystem"] } [dev-dependencies] bincode = "1.3" tempfile = "3.2" +windows-sys = { version = "0.42", features = ["Win32_Foundation"] } [target.'cfg(not(windows))'.dev-dependencies] libc = "0.2" diff --git a/src/windows/localize.rs b/src/windows/localize.rs index cea69c7..604d755 100644 --- a/src/windows/localize.rs +++ b/src/windows/localize.rs @@ -5,7 +5,8 @@ use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; use std::path::Path; -use windows_sys::Win32::UI::Shell::{SHGetFileInfoW, SHGFI_DISPLAYNAME}; +use windows_sys::Win32::UI::Shell::SHGetFileInfoW; +use windows_sys::Win32::UI::Shell::SHGFI_DISPLAYNAME; pub(super) fn name(path: &Path) -> Option { let mut path: Vec<_> = path.as_os_str().encode_wide().collect(); diff --git a/src/windows/normalize.rs b/src/windows/normalize.rs index b8e9b26..9f29be9 100644 --- a/src/windows/normalize.rs +++ b/src/windows/normalize.rs @@ -1,6 +1,7 @@ use std::env; use std::ffi::OsString; use std::io; +use std::mem; use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; use std::path::Component; @@ -15,6 +16,12 @@ use windows_sys::Win32::Storage::FileSystem::GetFullPathNameW; use super::BasePath; use super::BasePathBuf; +macro_rules! static_assert { + ( $condition:expr ) => { + const _: () = assert!($condition, "static assertion failed"); + }; +} + const SEPARATOR: u16 = b'\\' as _; pub(super) fn is_base(path: &Path) -> bool { @@ -96,6 +103,10 @@ pub(super) fn normalize_virtually( break Err(io::Error::last_os_error()); } + let _: u32 = capacity; + // This assertion should never fail. + static_assert!(mem::size_of::() <= mem::size_of::()); + let length = capacity as usize; if let Some(mut additional_capacity) = length.checked_sub(buffer.capacity())