Default all async support to std::future (#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 #1558
Closes #1695
This commit is contained in:
Alex Crichton
2019-09-05 11:18:36 -05:00
committed by GitHub
parent ba85275d7d
commit 3c887c40b7
33 changed files with 643 additions and 1120 deletions

View File

@ -1,4 +1,3 @@
use futures::{future, Future};
use js_sys::Promise;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
@ -37,6 +36,10 @@ pub struct Signature {
#[wasm_bindgen]
pub fn run() -> Promise {
future_to_promise(run_())
}
async fn run_() -> Result<JsValue, JsValue> {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
@ -44,36 +47,25 @@ pub fn run() -> Promise {
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();
.set("Accept", "application/vnd.github.v3+json")?;
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 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();
// `resp_value` is a `Response` object.
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();
// Send the `Branch` struct back to JS as an `Object`.
future::ok(JsValue::from_serde(&branch_info).unwrap())
});
// Convert this other `Promise` into a rust `Future`.
let json = JsFuture::from(resp.json()?).await?;
// Convert this Rust `Future` back into a JS `Promise`.
future_to_promise(future)
// 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`.
Ok(JsValue::from_serde(&branch_info).unwrap())
}