Send or !Send futures for wasm-bindgen compatible library

I'm developing a library which should work with the tokio executor as well as with futures from wasm-bindgen. Futures in wasm-bindgen are !Send. Futures in tokio are Send by default.

I developed a workaround by creating Send closures which return !Send futures on web in order to have compatibility. Now my question is whether its better to have use only !Send futures or use the workaround using Send closures which return !Send futures.

Example from the project:

Using opt-in feature for using Send/!Send futures:

pub trait ScheduleMethod: 'static {
    #[cfg(not(feature = "no-thread-safe-futures"))]
    fn schedule<T>(
        &self,
        shared_thread_state: SharedThreadState,
        future_factory: impl (FnOnce(SharedThreadState) -> T) + Send + 'static,
    ) -> Result<(), Error>
    where
        T: Future<Output = ()> + Send + 'static;

    #[cfg(feature = "no-thread-safe-futures")]
    fn schedule<T>(
        &self,
        shared_thread_state: SharedThreadState,
        future_factory: impl (FnOnce(SharedThreadState) -> T) + Send + 'static,
    ) -> Result<(), Error>
    where
        T: Future<Output = ()> + 'static;
}

Tokio impl:

impl ScheduleMethod for TokioScheduleMethod {
    fn schedule<T>(
        &self,
        shared_thread_state: SharedThreadState,
        future_factory: impl FnOnce(SharedThreadState) -> T + Send + 'static,
    ) -> Result<(), Error>
    where
        T: Future<Output = ()> + 'static,
    {
        self.pool.spawn_pinned(|| {
            let unsend_data = (future_factory)(shared_thread_state);

            async move { unsend_data.await }
        });

        Ok(())
    }
}

Using non-send futures:

pub trait ScheduleMethod: 'static {
    fn schedule<T>(
        &self,
        shared_thread_state: SharedThreadState,
        future_factory: impl (FnOnce(SharedThreadState) -> T) + Send + 'static,
    ) -> Result<(), Error>
    where
        T: Future<Output = ()> + 'static;
}

Tokio impl:

impl ScheduleMethod for TokioScheduleMethod {
    fn schedule<T>(
        &self,
        shared_thread_state: SharedThreadState,
        future_factory: impl FnOnce(SharedThreadState) -> T + Send + 'static,
    ) -> Result<(), Error>
    where
        T: Future<Output = ()> + Send + 'static,
    {
        tokio::task::spawn(future_factory(shared_thread_state));
        Ok(())
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.