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::{Object, Promise};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
@ -11,22 +10,22 @@ extern "C" {
fn new_event() -> Promise;
}
#[wasm_bindgen_test(async)]
fn event() -> impl Future<Item = (), Error = JsValue> {
JsFuture::from(new_event()).map(Event::from).map(|event| {
// All DOM interfaces should inherit from `Object`.
assert!(event.is_instance_of::<Object>());
let _: &Object = event.as_ref();
#[wasm_bindgen_test]
async fn event() {
let result = JsFuture::from(new_event()).await.unwrap();
let event = Event::from(result);
// All DOM interfaces should inherit from `Object`.
assert!(event.is_instance_of::<Object>());
let _: &Object = event.as_ref();
// These should match `new Event`.
assert!(event.bubbles());
assert!(event.cancelable());
assert!(event.composed());
// These should match `new Event`.
assert!(event.bubbles());
assert!(event.cancelable());
assert!(event.composed());
// The default behavior not initially prevented, but after
// we call `prevent_default` it better be.
assert!(!event.default_prevented());
event.prevent_default();
assert!(event.default_prevented());
})
// The default behavior not initially prevented, but after
// we call `prevent_default` it better be.
assert!(!event.default_prevented());
event.prevent_default();
assert!(event.default_prevented());
}

View File

@ -1,12 +1,5 @@
#![cfg(target_arch = "wasm32")]
extern crate futures;
extern crate js_sys;
extern crate wasm_bindgen;
extern crate wasm_bindgen_futures;
extern crate wasm_bindgen_test;
extern crate web_sys;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);

View File

@ -1,8 +1,3 @@
extern crate futures;
extern crate js_sys;
extern crate wasm_bindgen_futures;
use futures::Future;
use js_sys::{ArrayBuffer, DataView};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
@ -23,20 +18,19 @@ fn test_response_from_js() {
assert_eq!(response.status(), 501);
}
#[wasm_bindgen_test(async)]
fn test_response_from_bytes() -> impl Future<Item = (), Error = JsValue> {
#[wasm_bindgen_test]
async fn test_response_from_bytes() {
let mut bytes: [u8; 3] = [1, 3, 5];
let response = Response::new_with_opt_u8_array(Some(&mut bytes)).unwrap();
assert!(response.ok());
assert_eq!(response.status(), 200);
let buf_promise = response.array_buffer().unwrap();
JsFuture::from(buf_promise).map(move |buf_val| {
assert!(buf_val.is_instance_of::<ArrayBuffer>());
let array_buf: ArrayBuffer = buf_val.dyn_into().unwrap();
let data_view = DataView::new(&array_buf, 0, bytes.len());
for (i, byte) in bytes.iter().enumerate() {
assert_eq!(&data_view.get_uint8(i), byte);
}
})
let buf_val = JsFuture::from(buf_promise).await.unwrap();
assert!(buf_val.is_instance_of::<ArrayBuffer>());
let array_buf: ArrayBuffer = buf_val.dyn_into().unwrap();
let data_view = DataView::new(&array_buf, 0, bytes.len());
for (i, byte) in bytes.iter().enumerate() {
assert_eq!(&data_view.get_uint8(i), byte);
}
}

View File

@ -2,11 +2,6 @@ use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use futures::{
future::{ok, IntoFuture},
Future,
};
use web_sys::{
RtcPeerConnection, RtcRtpTransceiver, RtcRtpTransceiverDirection, RtcRtpTransceiverInit,
RtcSessionDescriptionInit,
@ -20,10 +15,10 @@ extern "C" {
fn is_unified_avail() -> bool;
}
#[wasm_bindgen_test(async)]
fn rtc_rtp_transceiver_direction() -> Box<dyn Future<Item = (), Error = JsValue>> {
#[wasm_bindgen_test]
async fn rtc_rtp_transceiver_direction() {
if !is_unified_avail() {
return Box::new(Ok(()).into_future());
return;
}
let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new();
@ -39,54 +34,39 @@ fn rtc_rtp_transceiver_direction() -> Box<dyn Future<Item = (), Error = JsValue>
let pc2: RtcPeerConnection = RtcPeerConnection::new().unwrap();
let r = exchange_sdps(pc1, pc2).and_then(move |(_, p2)| {
assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
assert_eq!(
tr1.current_direction(),
Some(RtcRtpTransceiverDirection::Sendonly)
);
let (_, p2) = exchange_sdps(pc1, pc2).await;
assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly);
assert_eq!(
tr1.current_direction(),
Some(RtcRtpTransceiverDirection::Sendonly)
);
let tr2: RtcRtpTransceiver = js_sys::try_iter(&p2.get_transceivers())
.unwrap()
.unwrap()
.next()
.unwrap()
.unwrap()
.unchecked_into();
let tr2: RtcRtpTransceiver = js_sys::try_iter(&p2.get_transceivers())
.unwrap()
.unwrap()
.next()
.unwrap()
.unwrap()
.unchecked_into();
assert_eq!(tr2.direction(), RtcRtpTransceiverDirection::Recvonly);
assert_eq!(
tr2.current_direction(),
Some(RtcRtpTransceiverDirection::Recvonly)
);
Ok(())
});
Box::new(r)
assert_eq!(tr2.direction(), RtcRtpTransceiverDirection::Recvonly);
assert_eq!(
tr2.current_direction(),
Some(RtcRtpTransceiverDirection::Recvonly)
);
}
fn exchange_sdps(
async fn exchange_sdps(
p1: RtcPeerConnection,
p2: RtcPeerConnection,
) -> impl Future<Item = (RtcPeerConnection, RtcPeerConnection), Error = JsValue> {
JsFuture::from(p1.create_offer())
.and_then(move |offer| {
let offer = offer.unchecked_into::<RtcSessionDescriptionInit>();
JsFuture::from(p1.set_local_description(&offer)).join4(
JsFuture::from(p2.set_remote_description(&offer)),
Ok(p1),
Ok(p2),
)
})
.and_then(|(_, _, p1, p2)| JsFuture::from(p2.create_answer()).join3(Ok(p1), Ok(p2)))
.and_then(|(answer, p1, p2)| {
let answer = answer.unchecked_into::<RtcSessionDescriptionInit>();
JsFuture::from(p2.set_local_description(&answer)).join4(
JsFuture::from(p1.set_remote_description(&answer)),
Ok(p1),
Ok(p2),
)
})
.and_then(|(_, _, p1, p2)| Ok((p1, p2)))
) -> (RtcPeerConnection, RtcPeerConnection) {
let offer = JsFuture::from(p1.create_offer()).await.unwrap();
let offer = offer.unchecked_into::<RtcSessionDescriptionInit>();
JsFuture::from(p1.set_local_description(&offer)).await.unwrap();
JsFuture::from(p2.set_remote_description(&offer)).await.unwrap();
let answer = JsFuture::from(p2.create_answer()).await.unwrap();
let answer = answer.unchecked_into::<RtcSessionDescriptionInit>();
JsFuture::from(p2.set_local_description(&answer)).await.unwrap();
JsFuture::from(p1.set_remote_description(&answer)).await.unwrap();
(p1, p2)
}

View File

@ -11,7 +11,6 @@
//! @see https://github.com/rustwasm/wasm-bindgen/issues/1005
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};
#[wasm_bindgen(module = "/tests/wasm/element.js")]