From 03716a83c70c8f994f5b74bfff8b488226c16c1e Mon Sep 17 00:00:00 2001 From: Haim Gelfenbeyn Date: Sun, 29 Oct 2023 08:51:56 -0400 Subject: [PATCH] Return boxed value PnPDetectWindows:new: PnPDetectWindows structure cannot be freely copied/moved around because a pointer to self is stored in the created Window class. This code was working in Rust prior to 1.70 apparently just because Rust optimized PnPDetectWindows:new in a way that did not cause the structure to be copied; that no longer works in Rust 1.71+ --- rust-toolchain | 2 +- src/platform/pnp_detect_windows.rs | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/rust-toolchain b/rust-toolchain index 9006c0b..f474f5a 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.70.0 \ No newline at end of file +1.71.0 \ No newline at end of file diff --git a/src/platform/pnp_detect_windows.rs b/src/platform/pnp_detect_windows.rs index baad0eb..0e5dd2e 100644 --- a/src/platform/pnp_detect_windows.rs +++ b/src/platform/pnp_detect_windows.rs @@ -29,12 +29,12 @@ pub struct PnPDetectWindows { } impl PnPDetectWindows { - pub fn new(callback: Box) -> Self { - let mut pnp_detect = Self { + pub fn new(callback: Box) -> Box { + let mut pnp_detect = Box::new(Self { callback, current_devices: Self::read_device_list().unwrap_or_default(), hwnd: std::ptr::null_mut(), - }; + }); pnp_detect.create_window(); return pnp_detect; } @@ -95,8 +95,8 @@ impl PnPDetectWindows { PostQuitMessage(0); } WM_DEVICECHANGE => { - let self_ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA) as *mut Self; - let window_state: &mut Self = self_ptr.as_mut().unwrap(); + let self_ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA); + let window_state: &mut Self = &mut *(self_ptr as *mut Self); window_state.handle_hotplug_event(); } _ => return DefWindowProcW(hwnd, msg, wparam, lparam), @@ -146,8 +146,7 @@ impl PnPDetectWindows { std::ptr::null_mut(), std::ptr::null_mut(), hinstance, - self as *mut Self as *mut winapi::ctypes::c_void, - //std::ptr::null_mut(), + self as *mut _ as *mut _, ) };