Skip to content

Commit 86bf222

Browse files
committed
PhantomData<*mut ()> in Window to ensure it is !Send
1 parent 72302e9 commit 86bf222

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/macos/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Window {
6464
}
6565
};
6666

67-
let window_handler = Box::new(build(&mut crate::Window(&mut window)));
67+
let window_handler = Box::new(build(&mut crate::Window::new(&mut window)));
6868

6969
let retain_count_after_build: usize = unsafe {
7070
msg_send![window.ns_view, retainCount]
@@ -190,7 +190,7 @@ impl WindowState {
190190

191191
pub(super) fn trigger_event(&mut self, event: Event){
192192
self.window_handler.on_event(
193-
&mut crate::Window(&mut self.window),
193+
&mut crate::Window::new(&mut self.window),
194194
event
195195
);
196196
}

src/win/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ unsafe extern "system" fn wnd_proc(
6767
let window_state_ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA) as *mut RefCell<WindowState>;
6868
if !window_state_ptr.is_null() {
6969
let mut window = Window { hwnd };
70-
let mut window = crate::Window(&mut window);
70+
let mut window = crate::Window::new(&mut window);
7171

7272
match msg {
7373
WM_MOUSEMOVE => {
@@ -320,7 +320,7 @@ impl Window {
320320
);
321321
// todo: manage error ^
322322

323-
let handler = Box::new(build(&mut crate::Window(&mut Window { hwnd })));
323+
let handler = Box::new(build(&mut crate::Window::new(&mut Window { hwnd })));
324324

325325
let window_state = Box::new(RefCell::new(WindowState {
326326
window_class,

src/window.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::marker::PhantomData;
2+
13
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
24

35
use crate::WindowHandler;
@@ -18,9 +20,20 @@ impl AppRunner {
1820
}
1921
}
2022

21-
pub struct Window<'a>(pub(crate) &'a mut platform::Window);
23+
pub struct Window<'a> {
24+
window: &'a mut platform::Window,
25+
// so that Window is !Send on all platforms
26+
phantom: PhantomData<*mut ()>,
27+
}
2228

2329
impl<'a> Window<'a> {
30+
pub(crate) fn new(window: &mut platform::Window) -> Window {
31+
Window {
32+
window,
33+
phantom: PhantomData,
34+
}
35+
}
36+
2437
pub fn open<H, B>(
2538
options: WindowOpenOptions,
2639
build: B
@@ -35,6 +48,6 @@ impl<'a> Window<'a> {
3548

3649
unsafe impl<'a> HasRawWindowHandle for Window<'a> {
3750
fn raw_window_handle(&self) -> RawWindowHandle {
38-
self.0.raw_window_handle()
51+
self.window.raw_window_handle()
3952
}
4053
}

src/x11/window.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Window {
186186
new_physical_size: None,
187187
};
188188

189-
let mut handler = build(&mut crate::Window(&mut window));
189+
let mut handler = build(&mut crate::Window::new(&mut window));
190190

191191
let _ = tx.send(Ok(()));
192192

@@ -238,7 +238,7 @@ impl Window {
238238
let window_info = self.window_info;
239239

240240
handler.on_event(
241-
&mut crate::Window(self),
241+
&mut crate::Window::new(self),
242242
Event::Window(WindowEvent::Resized(window_info))
243243
)
244244
}
@@ -326,7 +326,7 @@ impl Window {
326326

327327
if wm_delete_window == data32[0] {
328328
handler.on_event(
329-
&mut crate::Window(self),
329+
&mut crate::Window::new(self),
330330
Event::Window(WindowEvent::WillClose)
331331
);
332332

@@ -357,7 +357,7 @@ impl Window {
357357
let logical_pos = physical_pos.to_logical(&self.window_info);
358358

359359
handler.on_event(
360-
&mut crate::Window(self),
360+
&mut crate::Window::new(self),
361361
Event::Mouse(MouseEvent::CursorMoved {
362362
position: logical_pos,
363363
}),
@@ -372,7 +372,7 @@ impl Window {
372372
match detail {
373373
4 => {
374374
handler.on_event(
375-
&mut crate::Window(self),
375+
&mut crate::Window::new(self),
376376
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
377377
x: 0.0,
378378
y: 1.0,
@@ -381,7 +381,7 @@ impl Window {
381381
}
382382
5 => {
383383
handler.on_event(
384-
&mut crate::Window(self),
384+
&mut crate::Window::new(self),
385385
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
386386
x: 0.0,
387387
y: -1.0,
@@ -391,7 +391,7 @@ impl Window {
391391
detail => {
392392
let button_id = mouse_id(detail);
393393
handler.on_event(
394-
&mut crate::Window(self),
394+
&mut crate::Window::new(self),
395395
Event::Mouse(MouseEvent::ButtonPressed(button_id))
396396
);
397397
}
@@ -405,7 +405,7 @@ impl Window {
405405
if detail != 4 && detail != 5 {
406406
let button_id = mouse_id(detail);
407407
handler.on_event(
408-
&mut crate::Window(self),
408+
&mut crate::Window::new(self),
409409
Event::Mouse(MouseEvent::ButtonReleased(button_id))
410410
);
411411
}
@@ -418,7 +418,7 @@ impl Window {
418418
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
419419

420420
handler.on_event(
421-
&mut crate::Window(self),
421+
&mut crate::Window::new(self),
422422
Event::Keyboard(convert_key_press_event(&event))
423423
);
424424
}
@@ -427,7 +427,7 @@ impl Window {
427427
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
428428

429429
handler.on_event(
430-
&mut crate::Window(self),
430+
&mut crate::Window::new(self),
431431
Event::Keyboard(convert_key_release_event(&event))
432432
);
433433
}

0 commit comments

Comments
 (0)