Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Sep 2, 2023
1 parent 62c92c2 commit a363759
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::sync::Arc<H> {
///
/// 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 ()>,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'a> DisplayHandle<'a> {

unsafe impl HasRawDisplayHandle for DisplayHandle<'_> {
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError> {
Ok(self.raw.clone())
Ok(self.raw)
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::sync::Arc<H> {
///
/// 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 ()>,
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<'a> WindowHandle<'a> {

unsafe impl HasRawWindowHandle for WindowHandle<'_> {
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError> {
Ok(self.raw.clone())
Ok(self.raw)
}
}

Expand Down
92 changes: 39 additions & 53 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<c_void> { unimplemented!() }
/// let obj: NonNull<c_void> = 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
Expand All @@ -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<c_void> { unimplemented!() }
/// let obj: NonNull<c_void> = get_canvas();
/// let mut window_handle = Wbg02CanvasWindowHandle::new(obj);
/// /* set fields */
/// ```
pub fn new(obj: NonNull<c_void>) -> 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.
///
Expand All @@ -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())
}

Expand All @@ -121,53 +118,50 @@ 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()
}
}

/// 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<c_void> { unimplemented!() }
/// let obj: NonNull<c_void> = 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<c_void>,
}

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<c_void> { unimplemented!() }
/// let obj: NonNull<c_void> = 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<c_void>) -> 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 Wbg02OffscreenCanvasWindowHandle {
/// Create a new `Wbg02OffscreenCanvasWindowHandle` from a [`wasm-bindgen`] object.
///
Expand All @@ -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())
}

Expand All @@ -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()
}
Expand Down

0 comments on commit a363759

Please sign in to comment.