Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make integer handles use non-zero types #137

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<NonZeroU32>,
}

impl XcbWindowHandle {
Expand All @@ -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,
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::ffi::c_void;
use core::num::NonZeroIsize;
use core::ptr::NonNull;

/// Raw display handle for Windows.
Expand Down Expand Up @@ -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<NonZeroIsize>,
}

impl Win32WindowHandle {
Expand All @@ -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,
}
}
}

Expand Down
Loading