From d720a5c743b8e69dccaadfedeebc89dc94ecbd3e Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 30 Jul 2023 20:50:31 +0200 Subject: [PATCH 1/2] Make Win32WindowHandle handles use `NonZeroIsize` --- src/windows.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index f536677..9751dbf 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,4 +1,5 @@ use core::ffi::c_void; +use core::num::NonZeroIsize; use core::ptr::NonNull; /// Raw display handle for Windows. @@ -28,9 +29,9 @@ impl WindowsDisplayHandle { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Win32WindowHandle { /// A Win32 `HWND` handle. - pub hwnd: isize, + pub hwnd: NonZeroIsize, /// The `GWLP_HINSTANCE` associated with this type's `HWND`. - pub hinstance: isize, + pub hinstance: Option, } impl Win32WindowHandle { @@ -40,20 +41,24 @@ impl Win32WindowHandle { /// # Example /// /// ``` + /// # use core::num::NonZeroIsize; /// # use raw_window_handle::Win32WindowHandle; /// # struct HWND(isize); /// # /// let window: HWND; - /// # window = HWND(0); - /// let mut handle = Win32WindowHandle::new(window.0); + /// # window = HWND(1); + /// let mut handle = Win32WindowHandle::new(NonZeroIsize::new(window.0).unwrap()); /// // Optionally set the GWLP_HINSTANCE. /// # #[cfg(only_for_showcase)] - /// let hinstance = unsafe { GetWindowLongPtrW(window, GWLP_HINSTANCE) }; - /// # let hinstance = 0; + /// let hinstance = NonZeroIsize::new(unsafe { GetWindowLongPtrW(window, GWLP_HINSTANCE) }).unwrap(); + /// # let hinstance = None; /// handle.hinstance = hinstance; /// ``` - pub fn new(hwnd: isize) -> Self { - Self { hwnd, hinstance: 0 } + pub fn new(hwnd: NonZeroIsize) -> Self { + Self { + hwnd, + hinstance: None, + } } } From 05ba6095e5d818e8ed9b8e8df1055d39bb2ee5c8 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 30 Jul 2023 20:37:17 +0200 Subject: [PATCH 2/2] Make XcbWindowHandle use `NonZeroU32` --- src/unix.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/unix.rs b/src/unix.rs index 0dd4773..5cf7c88 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,4 +1,5 @@ use core::ffi::{c_int, c_ulong, c_void}; +use core::num::NonZeroU32; use core::ptr::NonNull; /// Raw display handle for Xlib. @@ -114,9 +115,9 @@ impl XcbDisplayHandle { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct XcbWindowHandle { /// An X11 `xcb_window_t`. - pub window: u32, // Based on xproto.h - /// An X11 `xcb_visualid_t`, or 0 if unknown. - pub visual_id: u32, + pub window: NonZeroU32, // Based on xproto.h + /// An X11 `xcb_visualid_t`. + pub visual_id: Option, } impl XcbWindowHandle { @@ -126,18 +127,19 @@ impl XcbWindowHandle { /// # Example /// /// ``` + /// # use core::num::NonZeroU32; /// # use raw_window_handle::XcbWindowHandle; /// # - /// let window: u32; - /// # window = 0; + /// let window: NonZeroU32; + /// # window = NonZeroU32::new(1).unwrap(); /// let mut handle = XcbWindowHandle::new(window); /// // Optionally set the visual ID. - /// handle.visual_id = 0; + /// handle.visual_id = None; /// ``` - pub fn new(window: u32) -> Self { + pub fn new(window: NonZeroU32) -> Self { Self { window, - visual_id: 0, + visual_id: None, } } }