Unverified Commit 156fe6c2 authored by James Hugman's avatar James Hugman
Browse files

Make Future non Send for wasm32

parent fede6786
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -76,7 +76,6 @@
//! [`RawWaker`]: https://doc.rust-lang.org/std/task/struct.RawWaker.html

use std::{
    future::Future,
    marker::PhantomData,
    ops::Deref,
    panic,
@@ -85,14 +84,14 @@ use std::{
    task::{Context, Poll, Wake},
};

use super::{RustFutureContinuationCallback, RustFuturePoll, Scheduler};
use super::{FutureResult, RustFutureContinuationCallback, RustFuturePoll, Scheduler};
use crate::{rust_call_with_out_status, FfiDefault, LiftArgsError, LowerReturn, RustCallStatus};

/// Wraps the actual future we're polling
struct WrappedFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -106,7 +105,7 @@ where
impl<F, T, UT> WrappedFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -186,7 +185,7 @@ where
unsafe impl<F, T, UT> Send for WrappedFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -196,7 +195,7 @@ where
pub(super) struct RustFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -212,7 +211,7 @@ where
impl<F, T, UT> RustFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -267,7 +266,7 @@ where
impl<F, T, UT> Wake for RustFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
@@ -302,7 +301,7 @@ pub trait RustFutureFfi<ReturnType>: Send + Sync {
impl<F, T, UT> RustFutureFfi<T::ReturnType> for RustFuture<F, T, UT>
where
    // See rust_future_new for an explanation of these trait bounds
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    T: LowerReturn<UT> + Send + 'static,
    UT: Send + 'static,
{
+12 −1
Original line number Diff line number Diff line
@@ -30,6 +30,17 @@ pub enum RustFuturePoll {
/// to continue progress on the future.
pub type RustFutureContinuationCallback = extern "C" fn(callback_data: u64, RustFuturePoll);

/// This marker trait allows us to put different bounds on the `Future`s we support, based on
/// `#[cfg()]` configuration.
pub trait FutureResult<T, E>: Future<Output = Result<T, E>> + 'static {}

#[cfg(not(target_arch = "wasm32"))]
impl<T, F, E> FutureResult<T, E> for F where F: Future<Output = Result<T, E>> + Send + 'static {}

// `Promise`s cannot be sent to or from WebWorkers, but `JsValue` is not `Send`.
#[cfg(target_arch = "wasm32")]
impl<T, F, E> FutureResult<T, E> for F where F: Future<Output = Result<T, E>> + 'static {}

// === Public FFI API ===

/// Create a new [Handle] for a Rust future
@@ -44,7 +55,7 @@ where
    // since it will move between threads for an indeterminate amount of time as the foreign
    // executor calls polls it and the Rust executor wakes it.  It does not need to by `Sync`,
    // since we synchronize all access to the values.
    F: Future<Output = Result<T, LiftArgsError>> + Send + 'static,
    F: FutureResult<T, LiftArgsError>,
    // T is the output of the Future.  It needs to implement [LowerReturn].  Also it must be Send +
    // 'static for the same reason as F.
    T: LowerReturn<UT> + Send + 'static,