rust-windowing / raw-window-handle

A common windowing interoperability library for Rust
Apache License 2.0
314 stars 49 forks source link

What is the right way of handling web handles? #151

Open notgull opened 1 year ago

notgull commented 1 year ago

I just merged #134, which adds a new web handle type based on references to a JsValue. I opted to merge it now so we can migrate bikeshedding to a new thread.

Three strategies were proposed of having wasm-bindgen-based handles without using the data attribute ID hack:

Which strategy should we use to store web handles?

cc @madsmtm @grovesNL @daxpedda

daxpedda commented 1 year ago

As I mentioned before, I'm not familiar with RWH, but option 1. seems to me to contain no more footguns then all the other handles.

Generally speaking, unlike other handles, these Web handles will only work with wasm-bindgen, so I think it makes sense to depend on wasm-bindgen. The data attribute ID is the only wasm-bindgen independent handle I can think of (or any other attribute).

grovesNL commented 1 year ago

JsValue is the least surprising option for both producers and consumers, and matched what people are already doing when they pass canvases to graphics APIs. The original goal of raw-window-handle was to make it easier to pass window handles between crates, so trying to match natural usage patterns seems like the pragmatic solution.

Using WasmAbi or pointers adds complexity and requires unsafe implementations, so it's easier for people to make mistakes. We could obviously document requirements but these may fail in subtle ways when the canvas is attempted to be used by the graphics API (e.g. use-after-free), so it adds extra friction for people getting started.

We already agreed that Copy is probably not necessary for window handles, so it shouldn't be a strong argument in favour of either option.