From adb9e60fbebb329bed614a6cf86be4064133c8d4 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 13 Jun 2023 13:59:12 +0200 Subject: [PATCH] common: Add executor::JoinHandle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and simplify wasm spawn implementation. This reduces the differences on wasm vs. non-wasm. Of course it's still possible to rely on details of the different error types, but at least both implement a few common traits: `Debug`, `Display`, `Error`. --- crates/matrix-sdk-common/src/executor.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk-common/src/executor.rs b/crates/matrix-sdk-common/src/executor.rs index f0b994c56..a8eecb5cd 100644 --- a/crates/matrix-sdk-common/src/executor.rs +++ b/crates/matrix-sdk-common/src/executor.rs @@ -15,6 +15,8 @@ //! Abstraction over an executor so we can spawn tasks under WASM the same way //! we do usually. +#[cfg(target_arch = "wasm32")] +pub use std::convert::Infallible as JoinError; #[cfg(target_arch = "wasm32")] use std::{ future::Future, @@ -25,15 +27,14 @@ use std::{ #[cfg(target_arch = "wasm32")] use futures_util::{future::RemoteHandle, FutureExt}; #[cfg(not(target_arch = "wasm32"))] -pub use tokio::task::{spawn, JoinHandle}; +pub use tokio::task::{spawn, JoinError, JoinHandle}; #[cfg(target_arch = "wasm32")] pub fn spawn(future: F) -> JoinHandle where F: Future + 'static, { - let fut = future.unit_error(); - let (fut, handle) = fut.remote_handle(); + let (fut, handle) = future.remote_handle(); wasm_bindgen_futures::spawn_local(fut); JoinHandle { handle } @@ -42,14 +43,14 @@ where #[cfg(target_arch = "wasm32")] #[derive(Debug)] pub struct JoinHandle { - handle: RemoteHandle>, + handle: RemoteHandle, } #[cfg(target_arch = "wasm32")] impl Future for JoinHandle { - type Output = Result; + type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::new(&mut self.handle).poll(cx) + Pin::new(&mut self.handle).poll(cx).map(Ok) } }