From 95460d4f090e2627706f45098f332b20e7dec4bd Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 11 Oct 2023 20:24:42 +0200 Subject: [PATCH] platform_types: Convert Windows `HANDLE` types to `isize` The `windows` crate treats these as `isize` rather than raw void pointers: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Foundation/struct.HWND.html And `raw-window-handle 0.6` recently started to do the same: https://github.com/rust-windowing/raw-window-handle/pull/136 However, the win32 documentation still states that these should be `PVOID`: https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types --- Changelog.md | 1 + ash-window/src/lib.rs | 4 ++-- ash/src/extensions/khr/external_fence_win32.rs | 8 ++++---- ash/src/extensions/khr/external_memory_win32.rs | 8 ++++---- ash/src/extensions/khr/external_semaphore_win32.rs | 8 ++++---- ash/src/vk/platform_types.rs | 12 ++++++++---- generator/src/lib.rs | 6 +++--- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Changelog.md b/Changelog.md index 55059a908..1adb937d9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Define `Display` as `c_void` instead of `*mut c_void` to match Xlib (#751) - `VK_KHR_device_group_creation`: Take borrow of `Entry` in `fn new()` (#753) - `VK_KHR_device_group_creation`: Rename `vk::Instance`-returning function from `device()` to `instance()` (#759) +- Windows `HANDLE` types (`HWND`, `HINSTANCE`, `HMONITOR`) are now defined as `isize` instead of `*const c_void` (#797) ### Removed diff --git a/ash-window/src/lib.rs b/ash-window/src/lib.rs index 885c48f37..a11991961 100644 --- a/ash-window/src/lib.rs +++ b/ash-window/src/lib.rs @@ -42,8 +42,8 @@ pub unsafe fn create_surface( match (display_handle, window_handle) { (RawDisplayHandle::Windows(_), RawWindowHandle::Win32(window)) => { let surface_desc = vk::Win32SurfaceCreateInfoKHR::default() - .hinstance(window.hinstance) - .hwnd(window.hwnd); + .hinstance(window.hinstance as isize) + .hwnd(window.hwnd as isize); let surface_fn = khr::Win32Surface::new(entry, instance); surface_fn.create_win32_surface(&surface_desc, allocation_callbacks) } diff --git a/ash/src/extensions/khr/external_fence_win32.rs b/ash/src/extensions/khr/external_fence_win32.rs index 7dde56092..1fc6c18ed 100644 --- a/ash/src/extensions/khr/external_fence_win32.rs +++ b/ash/src/extensions/khr/external_fence_win32.rs @@ -3,7 +3,7 @@ use crate::vk; use crate::{Device, Instance}; use std::ffi::CStr; use std::mem; -use std::ptr; +use std::mem::MaybeUninit; /// #[derive(Clone)] @@ -36,9 +36,9 @@ impl ExternalFenceWin32 { &self, get_info: &vk::FenceGetWin32HandleInfoKHR, ) -> VkResult { - let mut handle = ptr::null_mut(); - (self.fp.get_fence_win32_handle_khr)(self.handle, get_info, &mut handle) - .result_with_success(handle) + let mut handle = MaybeUninit::uninit(); + (self.fp.get_fence_win32_handle_khr)(self.handle, get_info, handle.as_mut_ptr()) + .assume_init_on_success(handle) } pub const NAME: &'static CStr = vk::KhrExternalFenceWin32Fn::NAME; diff --git a/ash/src/extensions/khr/external_memory_win32.rs b/ash/src/extensions/khr/external_memory_win32.rs index 53316b2cb..80d945faf 100644 --- a/ash/src/extensions/khr/external_memory_win32.rs +++ b/ash/src/extensions/khr/external_memory_win32.rs @@ -3,7 +3,7 @@ use crate::vk; use crate::{Device, Instance}; use std::ffi::CStr; use std::mem; -use std::ptr; +use std::mem::MaybeUninit; /// #[derive(Clone)] @@ -27,9 +27,9 @@ impl ExternalMemoryWin32 { &self, create_info: &vk::MemoryGetWin32HandleInfoKHR, ) -> VkResult { - let mut handle = ptr::null_mut(); - (self.fp.get_memory_win32_handle_khr)(self.handle, create_info, &mut handle) - .result_with_success(handle) + let mut handle = MaybeUninit::uninit(); + (self.fp.get_memory_win32_handle_khr)(self.handle, create_info, handle.as_mut_ptr()) + .assume_init_on_success(handle) } /// diff --git a/ash/src/extensions/khr/external_semaphore_win32.rs b/ash/src/extensions/khr/external_semaphore_win32.rs index c567b242e..8318e182b 100644 --- a/ash/src/extensions/khr/external_semaphore_win32.rs +++ b/ash/src/extensions/khr/external_semaphore_win32.rs @@ -3,7 +3,7 @@ use crate::vk; use crate::{Device, Instance}; use std::ffi::CStr; use std::mem; -use std::ptr; +use std::mem::MaybeUninit; /// #[derive(Clone)] @@ -36,9 +36,9 @@ impl ExternalSemaphoreWin32 { &self, get_info: &vk::SemaphoreGetWin32HandleInfoKHR, ) -> VkResult { - let mut handle = ptr::null_mut(); - (self.fp.get_semaphore_win32_handle_khr)(self.handle, get_info, &mut handle) - .result_with_success(handle) + let mut handle = MaybeUninit::uninit(); + (self.fp.get_semaphore_win32_handle_khr)(self.handle, get_info, handle.as_mut_ptr()) + .assume_init_on_success(handle) } pub const NAME: &'static CStr = vk::KhrExternalSemaphoreWin32Fn::NAME; diff --git a/ash/src/vk/platform_types.rs b/ash/src/vk/platform_types.rs index d1b4aa3d2..b76eb522c 100644 --- a/ash/src/vk/platform_types.rs +++ b/ash/src/vk/platform_types.rs @@ -10,12 +10,16 @@ pub type xcb_window_t = u32; pub type xcb_visualid_t = u32; pub type MirConnection = *const c_void; pub type MirSurface = *const c_void; -pub type HINSTANCE = *const c_void; -pub type HWND = *const c_void; +/// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Foundation/struct.HANDLE.html +pub type HANDLE = isize; +/// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Foundation/struct.HINSTANCE.html +pub type HINSTANCE = HANDLE; +/// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Foundation/struct.HWND.html +pub type HWND = HANDLE; +/// https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Graphics/Gdi/struct.HMONITOR.html +pub type HMONITOR = HANDLE; pub type wl_display = c_void; pub type wl_surface = c_void; -pub type HANDLE = *mut c_void; -pub type HMONITOR = HANDLE; pub type DWORD = c_ulong; pub type LPCWSTR = *const u16; pub type zx_handle_t = u32; diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 2b897afac..7db199eb9 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -305,7 +305,7 @@ pub trait ConstantExt { fn variant_ident(&self, enum_name: &str) -> Ident; fn notation(&self) -> Option<&str>; fn formatted_notation(&self) -> Option> { - static DOC_LINK: Lazy = Lazy::new(|| Regex::new(r#"<<([\w-]+)>>"#).unwrap()); + static DOC_LINK: Lazy = Lazy::new(|| Regex::new(r"<<([\w-]+)>>").unwrap()); self.notation().map(|n| { DOC_LINK.replace( n, @@ -2968,13 +2968,13 @@ pub fn write_source_code>(vk_headers_dir: &Path, src_dir: P) { let mut has_lifetimes = definitions .iter() .filter_map(get_variant!(vkxml::DefinitionsElement::Struct)) - .filter_map(|s| { + .filter(|&s| { s.elements .iter() .filter_map(get_variant!(vkxml::StructElement::Member)) .any(|x| x.reference.is_some()) - .then(|| name_to_tokens(&s.name)) }) + .map(|s| name_to_tokens(&s.name)) .collect::>(); for def in &definitions { match def {