-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#![allow(unsafe_code)] | ||
|
||
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; | ||
|
||
/// This wrapper exist to enable safely passing a [`RawWindowHandle`] across threads. Extracting the handle | ||
/// is still an unsafe operation, so the caller must still validate that using the raw handle is safe for a given context. | ||
#[derive(Debug, Clone)] | ||
pub struct RawWindowHandleWrapper(RawWindowHandle); | ||
|
||
impl RawWindowHandleWrapper { | ||
pub(crate) fn new(handle: RawWindowHandle) -> Self { | ||
Self(handle) | ||
} | ||
|
||
/// # Safety | ||
/// This returns a [`HasRawWindowHandle`] impl, which exposes [`RawWindowHandle`]. Some platforms | ||
/// have constraints on where/how this handle can be used. For example, some platforms don't support doing window | ||
/// operations off of the main thread. The caller must ensure the [`RawWindowHandle`] is only used in valid contexts. | ||
pub unsafe fn get_handle(&self) -> HasRawWindowHandleWrapper { | ||
HasRawWindowHandleWrapper(self.0) | ||
} | ||
} | ||
|
||
// SAFE: RawWindowHandle is just a normal "raw pointer", which doesn't impl Send/Sync. However the pointer is only | ||
// exposed via an unsafe method that forces the user to make a call for a given platform. (ex: some platforms don't | ||
// support doing window operations off of the main thread). | ||
// A recommendation for this pattern (and more context) is available here: | ||
// https://github.com/rust-windowing/raw-window-handle/issues/59 | ||
unsafe impl Send for RawWindowHandleWrapper {} | ||
unsafe impl Sync for RawWindowHandleWrapper {} | ||
|
||
pub struct HasRawWindowHandleWrapper(RawWindowHandle); | ||
|
||
// SAFE: the caller has validated that this is a valid context to get RawWindowHandle | ||
unsafe impl HasRawWindowHandle for HasRawWindowHandleWrapper { | ||
fn raw_window_handle(&self) -> RawWindowHandle { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.