-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Update wasm-bindgen-futures to only support std::future
#1695
Comments
Strongly in favor of doing the migration to std futures first, and handling the threaded impl in a follow up release. I've been messing around with wasm futures over the last few days, and it would've saved me quite a bit of debugging had std futures support been the default. edit: heh, maybe I spoke too soon. By "getting the threaded implementation up to par" does that mean solving the "cannot send between threads" restrictions when using |
@yoshuawuyts oh sorry nah this is unrelated to Rust futures and moreso about how we implement things on WebAssembly. The threads here is when the The actual transition will be quite simple actually, it's already done for futures 0.1. Just need to port it to 0.3 :) |
To clarify, this work is required to enable the new async/await syntax, correct? What would this example that @Pauan gave, look like with the new async/await syntax: fn on_click() {
state.merge(Action::LoadData);
spawn_local(indexeddb::open("my_db", 1)
.then(|db| db.start_transaction())
.then(|(db, trans)| db.object_store("people"))
.then(|store| store.get(0))
.then(|record| {
state.merge(Action::LoadSucceeded {
name: record.name,
age: record.age
});
}));
} fn on_click() {
state.merge(Action::LoadData);
spawn_local(async {
let db = await!(indexeddb::open("my_db", 1))?;
let trans = await!(db.start_transaction())?;
let store = await!(db.object_store("people"))?;
let record = await!(store.get(0))?;
state.merge(Action::LoadSucceeded {
name: record.name,
age: record.age
});
});
} |
@ctaggart wasm-bindgen already supports std Future (you just have to enable the This issue is about changing it so it's the default, so you no longer have to enable As for your example, it would look like this: fn on_click() {
state.merge(Action::LoadData);
spawn_local(unwrap_future(async {
let db = indexeddb::open("my_db", 1).await?;
let trans = db.start_transaction().await?;
let store = db.object_store("people").await?;
let record = store.get(0).await?;
state.merge(Action::LoadSucceeded {
name: record.name,
age: record.age
});
Ok(())
}));
} ...except the |
@ctaggart Note that the compatibility layer from the futures library also works perfectly fine in WASM. So you can really use both, with async-await syntax. |
Thanks! Yes, I'm attempting to do a fetch in wasm, but I'm getting confused how to use async/await with it. I enabled the
Will it eventually be possible to await a let request_promise = window.fetch_with_request(&request);
let future = JsFuture::from(request_promise)
.and_then(|resp_value| {
// `resp_value` is a `Response` object.
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();
resp.json()
})
.and_then(|json_value: Promise| {
// Convert this other `Promise` into a rust `Future`.
JsFuture::from(json_value)
})
.and_then(|json| {
// Use serde to parse the JSON into a struct.
let branch_info: Branch = json.into_serde().unwrap();
// Send the `Branch` struct back to JS as an `Object`.
future::ok(JsValue::from_serde(&branch_info).unwrap())
});
// Convert this Rust `Future` back into a JS `Promise`.
future_to_promise(future) I tried this, but got the error above and many more: async {
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
let resp: Response = resp_value.dyn_into().unwrap();
let json = JsFuture::from(resp.json()).await?;
let branch: Branch = json.into_serde().unwrap();
future_to_promise(future::ok(JsValue::from_serde(&branch_info).unwrap()))
} |
@ctaggart AFAICT |
@ctaggart You have to import |
I ended up getting this to compile, but wasm_bindgen fails with:
pub async fn run(&mut self) -> Promise {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let request = Request::new_with_str_and_init(
"https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master",
&opts,
)
.unwrap();
request
.headers()
.set("Accept", "application/vnd.github.v3+json")
.unwrap();
let window = web_sys::window().unwrap();
let request_promise = window.fetch_with_request(&request);
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await;
let resp: Response = resp_value.unwrap().dyn_into().unwrap();
let json = JsFuture::from(resp.json().unwrap()).await.unwrap();
let branch: Branch = json.into_serde().unwrap();
wasm_bindgen_futures::future_to_promise(futures::future::ok(JsValue::from_serde(&branch).unwrap()))
} May be I need to adjust the use wasm_bindgen::prelude::*;
use js_sys::Promise;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::futures_0_3::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response}; |
@ctaggart I'm sorry to see you're still struggling. Do you want to call this function from JavaScript? If so I think it shouldn't be an #[wasm_bindgen]
pub fn run(&mut self) -> Promise {
let fut = async
{
// ... the rest of the code
JsValue::from_serde(&branch).unwrap()
}
wasm_bindgen_futures::future_to_promise(fut)
} ps: this kind of question is better suited for the rust user forum than for this GH issue I think. |
@ctaggart #[wasm_bindgen]
impl Foo {
pub fn run(&mut self) -> Promise {
future_to_promise(self.run_async())
}
}
impl Foo {
async fn run_async(&mut self) -> Result<Branch, JsValue> {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let request = Request::new_with_str_and_init(
"https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master",
&opts,
)?;
request
.headers()
.set("Accept", "application/vnd.github.v3+json")?;
let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
let resp: Response = resp_value.dyn_into().unwrap();
let json = JsFuture::from(resp.json()?).await?;
let branch: Branch = json.into_serde().unwrap();
Ok(JsValue::from_serde(&branch).unwrap())
}
} In addition, you are using When you're using std Futures, you shouldn't be using |
I think that we may want to defer this switch until |
I feel it's likely we'll have a stable out before 1.39, though I'd hope it'd happen sometime in the next 6 weeks when 1.39 goes into beta. Though I personally don't think it's worth waiting on that since the |
Hm ok thanks for the info, although it's not so much that we're waiting for interfaces or something like that but moreso we have dependencies on at least the channels implemented there and so that code needs to live somewhere... |
As much as I want to switch to Futures 0.3 ASAP, |
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes rustwasm#1558 Closes rustwasm#1695
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes rustwasm#1558 Closes rustwasm#1695
I've posted an initial version of this to #1741 |
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes rustwasm#1558 Closes rustwasm#1695
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes rustwasm#1558 Closes rustwasm#1695
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes rustwasm#1558 Closes rustwasm#1695
This commit defaults all crates in-tree to use `std::future` by default and none of them support the crates.io `futures` 0.1 crate any more. This is a breaking change for `wasm-bindgen-futures` and `wasm-bindgen-test` so they've both received a major version bump to reflect the new defaults. Historical versions of these crates should continue to work if necessary, but they won't receive any more maintenance after this is merged. The movement here liberally uses `async`/`await` to remove the need for using any combinators on the `Future` trait. As a result many of the crates now rely on a much more recent version of the compiler, especially to run tests. The `wasm-bindgen-futures` crate was updated to remove all of its futures-related dependencies and purely use `std::future`, hopefully improving its compatibility by not having any version compat considerations over time. The implementations of the executors here are relatively simple and only delve slightly into the `RawWaker` business since there are no other stable APIs in `std::task` for wrapping these. This commit also adds support for: #[wasm_bindgen_test] async fn foo() { // ... } where previously you needed to pass `(async)` now that's inferred because it's an `async fn`. Closes #1558 Closes #1695
In the discussion from our WG meeting yesterday it's high time that we should probably release a new breaking version of the
wasm-bindgen-futures
crate which drops support for thefutures 0.1
crate and instead only supportsstd::future::Future
.I think the only major work item here is getting the threaded implementation up to par, but that can always happen in a follow-up release!
The text was updated successfully, but these errors were encountered: