raw_window_handle_ffi/handles/web_canvas.rs
1use crate::{RawWindowHandle, RawWindowHandleData};
2use std::{ffi::c_void, ptr::NonNull};
3
4/// raw_window_handle_ffi:
5/// This type is ABI-stable and FFI-compatible analogue for [`raw_window_handle::WebCanvasWindowHandle`].
6/// Can be converted to and from the referenced type.
7#[repr(C)]
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct WebCanvasWindowHandle {
10 /// A pointer to the [`JsValue`] of an [`HtmlCanvasElement`].
11 ///
12 /// Note: This uses [`c_void`] to avoid depending on `wasm-bindgen`
13 /// directly.
14 ///
15 /// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html
16 /// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html
17 //
18 // SAFETY: Not using `JsValue` is sound because `wasm-bindgen` guarantees
19 // that there's only one version of itself in any given binary, and hence
20 // we can't have a type-confusion where e.g. one library used `JsValue`
21 // from `v0.2` of `wasm-bindgen`, and another used `JsValue` from `v1.0`;
22 // the binary will simply fail to compile!
23 //
24 // Reference: TODO
25 pub obj: NonNull<c_void>,
26}
27
28unsafe impl Send for WebCanvasWindowHandle {}
29unsafe impl Sync for WebCanvasWindowHandle {}
30
31impl From<raw_window_handle::WebCanvasWindowHandle> for WebCanvasWindowHandle {
32 fn from(value: raw_window_handle::WebCanvasWindowHandle) -> Self {
33 Self { obj: value.obj }
34 }
35}
36
37impl WebCanvasWindowHandle {
38 /// Converts FFI type back to the original [`raw_window_handle::AppKitWindowHandle`].
39 ///
40 /// # Safety
41 /// Original type [`raw_window_handle::AppKitWindowHandle`] is marked as `non_exaustive`,
42 /// and therefore it is impossible to convert to it completely safely.
43 pub unsafe fn into(self) -> raw_window_handle::WebCanvasWindowHandle {
44 raw_window_handle::WebCanvasWindowHandle::new(self.obj)
45 }
46}
47
48impl From<WebCanvasWindowHandle> for RawWindowHandle {
49 fn from(val: WebCanvasWindowHandle) -> RawWindowHandle {
50 RawWindowHandle {
51 kind: crate::RawWindowHandleKind::WebCanvasWindowHandle,
52 data: RawWindowHandleData { web_canvas: val },
53 }
54 }
55}