Skip to content

Commit

Permalink
Return boxed value PnPDetectWindows:new:
Browse files Browse the repository at this point in the history
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+
  • Loading branch information
haimgel committed Oct 29, 2023
1 parent 44acc86 commit 03716a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.70.0
1.71.0
13 changes: 6 additions & 7 deletions src/platform/pnp_detect_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub struct PnPDetectWindows {
}

impl PnPDetectWindows {
pub fn new(callback: Box<dyn UsbCallback>) -> Self {
let mut pnp_detect = Self {
pub fn new(callback: Box<dyn UsbCallback>) -> Box<Self> {
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;
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 _,
)
};

Expand Down

0 comments on commit 03716a8

Please sign in to comment.