Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasm-bindgen v0.2 handles for web_sys::HtmlCanvasElement and web_sys::OffscreenCanvas #134

Merged
merged 14 commits into from
Sep 3, 2023
16 changes: 6 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ jobs:
with:
rust-version: ${{ matrix.rust_version }}

- run: rustup target add x86_64-linux-android
- run: rustup target add wasm32-unknown-unknown

- name: Check documentation
run: cargo doc --no-deps --document-private-items

- name: Run tests
run: cargo test --verbose

- name: Run tests with std
run: cargo test --verbose --features std
- uses: taiki-e/install-action@cargo-hack

- name: Check on Android
run: cargo check --verbose --target x86_64-linux-android
- name: Run tests
run: cargo hack test --feature-powerset

- name: Check on Android with std
run: cargo check --verbose --target x86_64-linux-android --features std
- name: Run tests for wasm32-unknown-unknown
run: cargo hack check --target wasm32-unknown-unknown --feature-powerset
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ rust-version = "1.64"
alloc = []
std = ["alloc"]

# Unstable feature to allow the use of `wasm-bindgen` in the `wasm` target.
#
# This is radioactive semver-wise. Do not rely on this in stable code. Any code enabled by this
# feature can be changed or removed in a breaking way at any time. In fact, this feature could
# be removed entirely tomorrow.
#
# The point being: rely on this at your own peril.
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
unstable_web_handles = ["wasm-bindgen", "std"]
madsmtm marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(target_family = "wasm")'.dependencies.wasm-bindgen]
version = "0.2.87"
default-features = false
features = ["std"]
notgull marked this conversation as resolved.
Show resolved Hide resolved
optional = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
4 changes: 2 additions & 2 deletions src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ptr::NonNull;

/// Raw display handle for Android.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct AndroidDisplayHandle {}

impl AndroidDisplayHandle {
Expand All @@ -23,7 +23,7 @@ impl AndroidDisplayHandle {

/// Raw window handle for Android NDK.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct AndroidNdkWindowHandle {
/// A pointer to an `ANativeWindow`.
pub a_native_window: NonNull<c_void>,
Expand Down
4 changes: 2 additions & 2 deletions src/appkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ptr::NonNull;

/// Raw display handle for AppKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct AppKitDisplayHandle {}

impl AppKitDisplayHandle {
Expand All @@ -23,7 +23,7 @@ impl AppKitDisplayHandle {

/// Raw window handle for AppKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct AppKitWindowHandle {
/// A pointer to an `NSView` object.
pub ns_view: NonNull<c_void>,
Expand Down
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, Eq, Hash, Copy, Clone)]
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
#[derive(PartialEq, Clone)]
pub struct DisplayHandle<'a> {
raw: RawDisplayHandle,
_marker: PhantomData<&'a *const ()>,
Expand Down Expand Up @@ -112,13 +112,13 @@ impl<'a> DisplayHandle<'a> {

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

impl<'a> HasDisplayHandle for DisplayHandle<'a> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
Ok(*self)
Ok(self.clone())
}
}

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, Eq, Hash, Clone)]
#[derive(PartialEq, Clone)]
notgull marked this conversation as resolved.
Show resolved Hide resolved
pub struct WindowHandle<'a> {
raw: RawWindowHandle,
_marker: PhantomData<&'a *const ()>,
Expand Down
4 changes: 2 additions & 2 deletions src/haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ptr::NonNull;

/// Raw display handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct HaikuDisplayHandle {}

impl HaikuDisplayHandle {
Expand All @@ -23,7 +23,7 @@ impl HaikuDisplayHandle {

/// Raw window handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct HaikuWindowHandle {
/// A pointer to a BWindow object
pub b_window: NonNull<c_void>,
Expand Down
24 changes: 21 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::new_without_default)]

//! Interoperability library for Rust Windowing applications.
//!
Expand Down Expand Up @@ -51,7 +52,10 @@ pub use unix::{
DrmDisplayHandle, DrmWindowHandle, GbmDisplayHandle, GbmWindowHandle, WaylandDisplayHandle,
WaylandWindowHandle, XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle,
};
pub use web::{WebDisplayHandle, WebWindowHandle};
pub use web::{
Wbg02CanvasWindowHandle, Wbg02Object, Wbg02OffscreenCanvasWindowHandle, WebDisplayHandle,
WebWindowHandle,
};
pub use windows::{Win32WindowHandle, WinRtWindowHandle, WindowsDisplayHandle};

use core::fmt;
Expand Down Expand Up @@ -120,7 +124,7 @@ unsafe impl<T: HasRawWindowHandle + ?Sized> HasRawWindowHandle for alloc::sync::
/// [`RawWindowHandle::Xlib`] on macOS, it would just be weird, and probably
/// requires something like XQuartz be used).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub enum RawWindowHandle {
/// A raw window handle for UIKit (Apple's non-macOS windowing library).
///
Expand Down Expand Up @@ -189,6 +193,20 @@ pub enum RawWindowHandle {
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
Web(WebWindowHandle),
/// A raw window handle for a Web canvas registered via [`wasm-bindgen`].
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
Wgb02Canvas(Wbg02CanvasWindowHandle),
/// A raw window handle for a Web offscreen canvas registered via [`wasm-bindgen`].
///
/// ## Availability Hints
/// This variant is used on Wasm or asm.js targets when targeting the Web/HTML5.
///
/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
Wgb02OffscreenCanvas(Wbg02OffscreenCanvasWindowHandle),
/// A raw window handle for Android NDK.
///
/// ## Availability Hints
Expand Down Expand Up @@ -274,7 +292,7 @@ unsafe impl<T: HasRawDisplayHandle + ?Sized> HasRawDisplayHandle for alloc::sync
/// [`RawDisplayHandle::Xlib`] on macOS, it would just be weird, and probably
/// requires something like XQuartz be used).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub enum RawDisplayHandle {
/// A raw display handle for UIKit (Apple's non-macOS windowing library).
///
Expand Down
4 changes: 2 additions & 2 deletions src/redox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ptr::NonNull;

/// Raw display handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct OrbitalDisplayHandle {}

impl OrbitalDisplayHandle {
Expand All @@ -23,7 +23,7 @@ impl OrbitalDisplayHandle {

/// Raw window handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct OrbitalWindowHandle {
/// A pointer to an orbclient window.
// TODO(madsmtm): I think this is a file descriptor, so perhaps it should
Expand Down
4 changes: 2 additions & 2 deletions src/uikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ptr::NonNull;

/// Raw display handle for UIKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct UiKitDisplayHandle {}

impl UiKitDisplayHandle {
Expand All @@ -23,7 +23,7 @@ impl UiKitDisplayHandle {

/// Raw window handle for UIKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct UiKitWindowHandle {
/// A pointer to an `UIView` object.
pub ui_view: NonNull<c_void>,
Expand Down
20 changes: 10 additions & 10 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ptr::NonNull;

/// Raw display handle for Xlib.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct XlibDisplayHandle {
/// A pointer to an Xlib `Display`.
///
Expand Down Expand Up @@ -44,7 +44,7 @@ impl XlibDisplayHandle {

/// Raw window handle for Xlib.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct XlibWindowHandle {
/// An Xlib `Window`.
pub window: c_ulong,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl XlibWindowHandle {

/// Raw display handle for Xcb.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct XcbDisplayHandle {
/// A pointer to an X server `xcb_connection_t`.
///
Expand Down Expand Up @@ -118,7 +118,7 @@ impl XcbDisplayHandle {

/// Raw window handle for Xcb.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct XcbWindowHandle {
/// An X11 `xcb_window_t`.
pub window: NonZeroU32, // Based on xproto.h
Expand Down Expand Up @@ -152,7 +152,7 @@ impl XcbWindowHandle {

/// Raw display handle for Wayland.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct WaylandDisplayHandle {
/// A pointer to a `wl_display`.
pub display: NonNull<c_void>,
Expand Down Expand Up @@ -180,7 +180,7 @@ impl WaylandDisplayHandle {

/// Raw window handle for Wayland.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct WaylandWindowHandle {
/// A pointer to a `wl_surface`.
pub surface: NonNull<c_void>,
Expand Down Expand Up @@ -208,7 +208,7 @@ impl WaylandWindowHandle {

/// Raw display handle for the Linux Kernel Mode Set/Direct Rendering Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct DrmDisplayHandle {
/// The drm file descriptor.
// TODO: Use `std::os::fd::RawFd`?
Expand All @@ -235,7 +235,7 @@ impl DrmDisplayHandle {

/// Raw window handle for the Linux Kernel Mode Set/Direct Rendering Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct DrmWindowHandle {
/// The primary drm plane handle.
pub plane: u32,
Expand All @@ -261,7 +261,7 @@ impl DrmWindowHandle {

/// Raw display handle for the Linux Generic Buffer Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct GbmDisplayHandle {
/// The gbm device.
pub gbm_device: NonNull<c_void>,
Expand Down Expand Up @@ -289,7 +289,7 @@ impl GbmDisplayHandle {

/// Raw window handle for the Linux Generic Buffer Manager.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq)]
pub struct GbmWindowHandle {
/// The gbm surface.
pub gbm_surface: NonNull<c_void>,
Expand Down
Loading
Loading