From 86bf222601465f357edb5ee73e731a9bc3b0e9ea Mon Sep 17 00:00:00 2001 From: micah Date: Sat, 12 Dec 2020 01:58:02 -0500 Subject: [PATCH] PhantomData<*mut ()> in Window to ensure it is !Send --- src/macos/window.rs | 4 ++-- src/win/window.rs | 4 ++-- src/window.rs | 17 +++++++++++++++-- src/x11/window.rs | 20 ++++++++++---------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index d978da9..ad9f0b1 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -64,7 +64,7 @@ impl Window { } }; - let window_handler = Box::new(build(&mut crate::Window(&mut window))); + let window_handler = Box::new(build(&mut crate::Window::new(&mut window))); let retain_count_after_build: usize = unsafe { msg_send![window.ns_view, retainCount] @@ -190,7 +190,7 @@ impl WindowState { pub(super) fn trigger_event(&mut self, event: Event){ self.window_handler.on_event( - &mut crate::Window(&mut self.window), + &mut crate::Window::new(&mut self.window), event ); } diff --git a/src/win/window.rs b/src/win/window.rs index 73fe78b..67f2af9 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -67,7 +67,7 @@ unsafe extern "system" fn wnd_proc( let window_state_ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA) as *mut RefCell; if !window_state_ptr.is_null() { let mut window = Window { hwnd }; - let mut window = crate::Window(&mut window); + let mut window = crate::Window::new(&mut window); match msg { WM_MOUSEMOVE => { @@ -320,7 +320,7 @@ impl Window { ); // todo: manage error ^ - let handler = Box::new(build(&mut crate::Window(&mut Window { hwnd }))); + let handler = Box::new(build(&mut crate::Window::new(&mut Window { hwnd }))); let window_state = Box::new(RefCell::new(WindowState { window_class, diff --git a/src/window.rs b/src/window.rs index a88af7b..eff9a57 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; use crate::WindowHandler; @@ -18,9 +20,20 @@ impl AppRunner { } } -pub struct Window<'a>(pub(crate) &'a mut platform::Window); +pub struct Window<'a> { + window: &'a mut platform::Window, + // so that Window is !Send on all platforms + phantom: PhantomData<*mut ()>, +} impl<'a> Window<'a> { + pub(crate) fn new(window: &mut platform::Window) -> Window { + Window { + window, + phantom: PhantomData, + } + } + pub fn open( options: WindowOpenOptions, build: B @@ -35,6 +48,6 @@ impl<'a> Window<'a> { unsafe impl<'a> HasRawWindowHandle for Window<'a> { fn raw_window_handle(&self) -> RawWindowHandle { - self.0.raw_window_handle() + self.window.raw_window_handle() } } diff --git a/src/x11/window.rs b/src/x11/window.rs index bc04844..aad7ba3 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -186,7 +186,7 @@ impl Window { new_physical_size: None, }; - let mut handler = build(&mut crate::Window(&mut window)); + let mut handler = build(&mut crate::Window::new(&mut window)); let _ = tx.send(Ok(())); @@ -238,7 +238,7 @@ impl Window { let window_info = self.window_info; handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Window(WindowEvent::Resized(window_info)) ) } @@ -326,7 +326,7 @@ impl Window { if wm_delete_window == data32[0] { handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Window(WindowEvent::WillClose) ); @@ -357,7 +357,7 @@ impl Window { let logical_pos = physical_pos.to_logical(&self.window_info); handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Mouse(MouseEvent::CursorMoved { position: logical_pos, }), @@ -372,7 +372,7 @@ impl Window { match detail { 4 => { handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { x: 0.0, y: 1.0, @@ -381,7 +381,7 @@ impl Window { } 5 => { handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { x: 0.0, y: -1.0, @@ -391,7 +391,7 @@ impl Window { detail => { let button_id = mouse_id(detail); handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Mouse(MouseEvent::ButtonPressed(button_id)) ); } @@ -405,7 +405,7 @@ impl Window { if detail != 4 && detail != 5 { let button_id = mouse_id(detail); handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Mouse(MouseEvent::ButtonReleased(button_id)) ); } @@ -418,7 +418,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Keyboard(convert_key_press_event(&event)) ); } @@ -427,7 +427,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - &mut crate::Window(self), + &mut crate::Window::new(self), Event::Keyboard(convert_key_release_event(&event)) ); }