rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.84k stars 1.08k forks source link

web-sys: Bluetooth usability issues #2381

Open chrysn opened 3 years ago

chrysn commented 3 years ago

Motivation

I'm porting Bluetooth using code over from Javascript, and am running into a few warts around how web-sys exposes them.

As I'm relatively new to web-sys usage, I don't have a clear plan forward with them, and they may or may not be related; in this issue I'd like to hash out how they are related, and what can reasonably be done within being a -sys crate.

Issues

alexcrichton commented 3 years ago

Thanks for the report! I think it should be fine for us to change the WebIDL to accomodate this perhaps?

chrysn commented 3 years ago

Reading further into WebIDL, that might easily make sense, eg. as

-  readonly attribute Bluetooth bluetooth;
+  readonly attribute Bluetooth? bluetooth;

and

-  Promise<BluetoothDevice> requestDevice(optional RequestDeviceOptions options = {});
+  Promise<BluetoothDevice> requestDevice(RequestDeviceOptions options = {});

. Is there any general guidance on which deviations from the upstream WebIDL are OK? (The Guide is silent on that.). Or would those be patches for upstream? On RequestDeviceOptions I'm a bit more clueless -- however, given it's a -sys, it may be up to other crates to create something builder-style that implements Into<RequestDeviceOptions> -- or where would something be done here?

alexcrichton commented 3 years ago

There isn't necessarily a hard-and-fast guideline here but I think it's fine in this regard since it's unstable. There's not necessarily a canonical source-of-truth of WebIDL from what I've seen since browsers often have their own tweaks or perhaps a different snapshot than us.

In any case the unstable part of this is what matters and that gives us the freedom to change this at any time, so whatever works I think should be fine!

chrysn commented 3 years ago

On the proposed patches I now have a PR open (#2385).

On the topic of how to get by the filters, I've found BluetoothLeScanFilterInit which helps over what I had to do with serde before -- but after the conversion, the information that filters is a sequence<BluetoothLEScanFilterInit> is lost, and only shows shows as JsValue. The two ways forward I see (but would need a great larger understanding of this system) are, in order of decreasing preference:

[edit: s/into_iter/iter/ due to https://github.com/rust-lang/rust/issues/66145]

alexcrichton commented 3 years ago

Ah yeah carrying over type parameters would be really nice to do and is an open issue with web-sys and its bindings. At the very least appending to the generated documentation would be great!

ChocolateLoverRaj commented 9 months ago

I would prefer a Rust-friendly filters type, instead of having to to create JavaScript objects manually with Rust.