diff --git a/src/gl/win.rs b/src/gl/win.rs index 655a807..f4cca6c 100644 --- a/src/gl/win.rs +++ b/src/gl/win.rs @@ -84,10 +84,6 @@ impl GlContext { return Err(GlError::InvalidWindowHandle); }; - if handle.hwnd.is_null() { - return Err(GlError::InvalidWindowHandle); - } - // Create temporary window and context to load function pointers let class_name_str = format!("raw-gl-context-window-{}", uuid::Uuid::new_v4().to_simple()); @@ -195,7 +191,7 @@ impl GlContext { // Create actual context - let hwnd = handle.hwnd as HWND; + let hwnd = handle.hwnd.get() as HWND; let hdc = GetDC(hwnd); diff --git a/src/win/window.rs b/src/win/window.rs index ac7824b..918123a 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -22,6 +22,7 @@ use winapi::um::winuser::{ use std::cell::{Cell, Ref, RefCell, RefMut}; use std::collections::VecDeque; use std::ffi::{c_void, OsStr}; +use std::num::NonZeroIsize; use std::os::windows::ffi::OsStrExt; use std::ptr::null_mut; use std::rc::Rc; @@ -86,15 +87,12 @@ impl WindowHandle { } unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - if let Some(hwnd) = self.hwnd { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = hwnd as *mut c_void; - - RawWindowHandle::Win32(handle) - } else { - RawWindowHandle::Win32(Win32WindowHandle::empty()) - } + fn raw_window_handle(&self) -> Result { + self.hwnd + .and_then(|hwnd| NonZeroIsize::new(hwnd as isize)) + .ok_or(raw_window_handle::HandleError::Unavailable) + .map(|v| Win32WindowHandle::new(v)) + .map(|v| RawWindowHandle::Win32(v)) } } @@ -591,7 +589,7 @@ impl Window<'_> { B: Send + 'static, { let parent = match parent.raw_window_handle() { - RawWindowHandle::Win32(h) => h.hwnd as HWND, + Ok(RawWindowHandle::Win32(h)) => h.hwnd.get() as HWND, h => panic!("unsupported parent handle {:?}", h), }; @@ -688,9 +686,11 @@ impl Window<'_> { #[cfg(feature = "opengl")] let gl_context: Option = options.gl_config.map(|gl_config| { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = hwnd as *mut c_void; - let handle = RawWindowHandle::Win32(handle); + let handle = NonZeroIsize::new(hwnd as isize) + .ok_or(raw_window_handle::HandleError::Unavailable) + .map(|v| Win32WindowHandle::new(v)) + .map(|v| RawWindowHandle::Win32(v)) + .unwrap(); GlContext::create(&handle, gl_config).expect("Could not create OpenGL context") }); @@ -827,17 +827,17 @@ impl Window<'_> { } unsafe impl HasRawWindowHandle for Window<'_> { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = self.state.hwnd as *mut c_void; - - RawWindowHandle::Win32(handle) + fn raw_window_handle(&self) -> Result { + NonZeroIsize::new(self.state.hwnd as isize) + .ok_or(raw_window_handle::HandleError::Unavailable) + .map(|v| Win32WindowHandle::new(v)) + .map(|v| RawWindowHandle::Win32(v)) } } unsafe impl HasRawDisplayHandle for Window<'_> { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) + fn raw_display_handle(&self) -> Result { + Ok(RawDisplayHandle::Windows(WindowsDisplayHandle::new())) } }