-
Notifications
You must be signed in to change notification settings - Fork 1k
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 webidl iterable support #3962
base: main
Are you sure you want to change the base?
Conversation
Oh I see I missed that the web-sys files are generated from webidl, and that the async iterable field is what should be providing the entries/keys/values methods. Given the following code am I right in thinking this isn't currently supported? wasm-bindgen/crates/webidl/src/first_pass.rs Lines 561 to 567 in fa6d2bc
|
Yes; there's an old issue #514 about it. |
Yep found that and am getting something working, looks like it's relatively simple to add, most of the plumbing is already there, so will update this PR. |
Use the doc_cfg feature to show it's behind futures-core-03-stream feature flag.
e5a2793
to
6cc1be2
Compare
Most of the plumbing was there but now (async) iterable instances generate the `entries`, `keys` and `values` methods. `forEach` is also generated for the non-async iterable.
6cc1be2
to
6047da3
Compare
FileSystemDirectoryHandle
methods
I've got something working that I've pushed, wasn't sure how to do the async Looking at how the ergonomics could be improved given we know the types yielded for these cases, some ideas in that issue so will see if something can work. |
Playing around with it a bit, I think the neatest way might be leave the exposed js methods (maybe prepend with a So for impl FileSystemDirectoryHandle
pub fn entries() -> impl Stream<Item = (JsString, FileSystemHandle)> {
JsStream::from(self.js_entries()).map(|x| {
let array = x.unwrap().unchecked_into::<Array>();
(array.at(0).unchecked_into(), array.at(1).unchecked_into())
})
}
} with something similar for regular iterables, and without the array step for Thoughts? It seems like there was a bunch of discussions regarding iterators but the direction was never really settled and a lot of those main contributors are no longer on wasm-bindgen, Probably need to read a bit more into if there are ever any cases where iterables for some of these methods can throw errors, but hand writing this for the directory case works fairly nicely with the tests I have. |
I've had a bit of a crack at this, but how you massage the types was trickier than I thought, Web IDL has types that JS doesn't so to cast to equivalent rust types you have to go via js types which doesn't always cast simply. Some API's generate enums that I also wasn't sure exactly how to deal with (see I think for now exposing these methods that just return js iterators allows users to at least use the iterable interfaces, they'll just have to deal with casting to the types they want. Maybe we want to gate it behind a feature flag if we plan on changing the API later. |
Was trying to figure out how to list files in OPFS before I realised these methods were just missing from web-sys.
Added some tests that use these and more generally test some of the file system API, only tested it with geckodriver but it should be well supported across all browsers.
Also added some flags so that the optional
stream
module should get generated in docs.rs too, wasn't obvious it existed, and it's somewhat needed to make use of theAsyncIterator
that these methods return.