web-sys: unnecessary &mut requirement in WebUSB functions, potentially others
several WebUSB functions that take a [u8] slice, e.g., transfer_out_with_u8_array, require it be passed as &mut. As far as I can tell this is not required since USB transfers are unidirectional.
full list of affected functions:
- control_transfer_out_with_u8_array
- isochronous_transfer_out_with_u8_array
- transfer_out_with_u8_array
other APIs might also be affected - I searched the docs for with_u8_array and found e.g. ReadableByteStreamController::enqueue_with_u8_array taking a &mut buffer which at first glance is only used for reading.
This is the case because web-sys is auto-generated from WebIDL, which doesn't include any information about whether or not buffers passed to functions are mutable.
Because of that, it conservatively assumes that all buffers are mutable, since it can't result in UB if a web API tries to mutate an immutable buffer and it's backwards-compatible to change a slice from mutable to immutable.
That change is made by adding the functions whose slices should be immutable to a big list: https://github.com/rustwasm/wasm-bindgen/blob/fa6d2bc726813c9f5370f07c64a4b60f0ac1edd5/crates/webidl/src/constants.rs#L43-L105.
@spookyvision Do you want to see if WebIDL is aware of the limitation? Solve the problem upstream?
I did a quick scan of the WebIDL spec and it doesn't seem they have a concept of (im)mutability, sadly. I might be wrong, but since the spec refers JavaScript a lot, it's probably closely modeled based on JS semantics.