From a36375995fbb2714acf5c6dd0defeb0ef6c8a49e Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 2 Sep 2023 15:33:07 -0700 Subject: [PATCH] Review comments Signed-off-by: John Nunley --- src/borrowed.rs | 8 ++--- src/web.rs | 92 +++++++++++++++++++++---------------------------- 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/src/borrowed.rs b/src/borrowed.rs index 3c2fd91..85b3b58 100644 --- a/src/borrowed.rs +++ b/src/borrowed.rs @@ -84,7 +84,7 @@ impl HasDisplayHandle for alloc::sync::Arc { /// /// Get the underlying raw display handle with the [`HasRawDisplayHandle`] trait. #[repr(transparent)] -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Eq, Hash, Clone)] pub struct DisplayHandle<'a> { raw: RawDisplayHandle, _marker: PhantomData<&'a *const ()>, @@ -112,7 +112,7 @@ impl<'a> DisplayHandle<'a> { unsafe impl HasRawDisplayHandle for DisplayHandle<'_> { fn raw_display_handle(&self) -> Result { - Ok(self.raw.clone()) + Ok(self.raw) } } @@ -209,7 +209,7 @@ impl HasWindowHandle for alloc::sync::Arc { /// /// This handle is guaranteed to be safe and valid. Get the underlying raw window handle with the /// [`HasRawWindowHandle`] trait. -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Eq, Hash, Clone)] pub struct WindowHandle<'a> { raw: RawWindowHandle, _marker: PhantomData<&'a *const ()>, @@ -237,7 +237,7 @@ impl<'a> WindowHandle<'a> { unsafe impl HasRawWindowHandle for WindowHandle<'_> { fn raw_window_handle(&self) -> Result { - Ok(self.raw.clone()) + Ok(self.raw) } } diff --git a/src/web.rs b/src/web.rs index da65980..ea51e18 100644 --- a/src/web.rs +++ b/src/web.rs @@ -54,24 +54,11 @@ impl WebWindowHandle { /// Raw window handle for a Web canvas registered via [`wasm-bindgen`]. /// -/// ## Construction -/// ```no_run -/// # use raw_window_handle::Wbg02CanvasWindowHandle; -/// # use core::{ffi::c_void, ptr::NonNull}; -/// # fn get_canvas() -> NonNull { unimplemented!() } -/// let obj: NonNull = get_canvas(); -/// let mut window_handle = Wbg02CanvasWindowHandle::new(obj); -/// /* set fields */ -/// ``` -/// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Wbg02CanvasWindowHandle { - /// The object representing the [`HtmlCanvasElement`]. - /// - /// It is implied that this object is registered in the [`wasm-bindgen`] table and is an instance - /// of [`HtmlCanvasElement`]. The pointer is a direct reference to a [`JsValue`]. + /// The pointer to the [`JsValue`] of an [`HtmlCanvasElement`]. /// /// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen @@ -80,16 +67,30 @@ pub struct Wbg02CanvasWindowHandle { } impl Wbg02CanvasWindowHandle { - /// Create a new handle to an [`HtmlCanvasElement`]. + /// Create a new handle to a pointer to [`HtmlCanvasElement`]. /// /// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html + /// + /// ## Example + /// ```no_run + /// # use raw_window_handle::Wbg02CanvasWindowHandle; + /// # use core::{ffi::c_void, ptr::NonNull}; + /// # fn get_canvas() -> NonNull { unimplemented!() } + /// let obj: NonNull = get_canvas(); + /// let mut window_handle = Wbg02CanvasWindowHandle::new(obj); + /// /* set fields */ + /// ``` pub fn new(obj: NonNull) -> Self { Self { obj } } } -#[cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))] -/// These implementations are only available when `unstable_web_handles_wbg_02` is enabled. +#[cfg(all(target_family = "wasm", feature = "unstable-wasm-bindgen-0-2"))] +#[cfg_attr( + docsrs, + doc(cfg(all(target_family = "wasm", feature = "unstable-wasm-bindgen-0-2"))) +)] +/// These implementations are only available when `unstable-wasm-bindgen-0-2` is enabled. impl Wbg02CanvasWindowHandle { /// Create a new `Wbg02CanvasWindowHandle` from a [`wasm-bindgen`] object. /// @@ -102,11 +103,7 @@ impl Wbg02CanvasWindowHandle { /// the `Wbg02CanvasWindowHandle` lives for. /// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen - #[cfg_attr( - docsrs, - doc(cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))) - )] - pub unsafe fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self { + pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self { Self::new(NonNull::from(js_value).cast()) } @@ -121,10 +118,6 @@ impl Wbg02CanvasWindowHandle { /// underlying pointer must still be a [`wasm_bindgen`] object. /// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen - #[cfg_attr( - docsrs, - doc(cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))) - )] pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue { self.obj.cast().as_ref() } @@ -132,33 +125,30 @@ impl Wbg02CanvasWindowHandle { /// Raw window handle for a Web offscreen canvas registered via [`wasm-bindgen`]. /// -/// ## Construction -/// ```no_run -/// # use raw_window_handle::Wbg02OffscreenCanvasWindowHandle; -/// # use core::{ffi::c_void, ptr::NonNull}; -/// # fn get_offscreen_canvas() -> NonNull { unimplemented!() } -/// let obj: NonNull = get_offscreen_canvas(); -/// let mut window_handle = Wbg02OffscreenCanvasWindowHandle::new(obj); -/// /* set fields */ -/// ``` -/// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Wbg02OffscreenCanvasWindowHandle { - /// The object representing the [`OffscreenCanvas`]. - /// - /// It is implied that this object is registered in the [`wasm-bindgen`] table and is an instance - /// of [`OffscreenCanvas`]. This is a pointer to the actual [`JsValue`] object. + /// The pointer to the [`JsValue`] of an [`OffscreenElement`]. /// - /// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html + /// [`OffscreenElement`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenElement.html /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen /// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html pub obj: NonNull, } impl Wbg02OffscreenCanvasWindowHandle { - /// Create a new handle to an [`OffscreenCanvas`]. + /// Create a new handle to a pointer to an [`OffscreenCanvas`]. + /// + /// ## Construction + /// ```no_run + /// # use raw_window_handle::Wbg02OffscreenCanvasWindowHandle; + /// # use core::{ffi::c_void, ptr::NonNull}; + /// # fn get_offscreen_canvas() -> NonNull { unimplemented!() } + /// let obj: NonNull = get_offscreen_canvas(); + /// let mut window_handle = Wbg02OffscreenCanvasWindowHandle::new(obj); + /// /* set fields */ + /// ``` /// /// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html pub fn new(obj: NonNull) -> Self { @@ -166,8 +156,12 @@ impl Wbg02OffscreenCanvasWindowHandle { } } -#[cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))] -/// These implementations are only available when `unstable_web_handles_wbg_02` is enabled. +#[cfg(all(target_family = "wasm", feature = "unstable-wasm-bindgen-0-2"))] +#[cfg_attr( + docsrs, + doc(cfg(all(target_family = "wasm", feature = "unstable-wasm-bindgen-0-2"))) +)] +/// These implementations are only available when `unstable-wasm-bindgen-0-2` is enabled. impl Wbg02OffscreenCanvasWindowHandle { /// Create a new `Wbg02OffscreenCanvasWindowHandle` from a [`wasm-bindgen`] object. /// @@ -180,11 +174,7 @@ impl Wbg02OffscreenCanvasWindowHandle { /// the `Wbg02OffscreenCanvasWindowHandle` lives for. /// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen - #[cfg_attr( - docsrs, - doc(cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))) - )] - pub unsafe fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self { + pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self { Self::new(NonNull::from(js_value).cast()) } @@ -199,10 +189,6 @@ impl Wbg02OffscreenCanvasWindowHandle { /// underlying pointer must still be a [`wasm_bindgen`] object. /// /// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen - #[cfg_attr( - docsrs, - doc(cfg(all(target_family = "wasm", feature = "unstable_web_handles_wbg_02"))) - )] pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue { self.obj.cast().as_ref() }