diff --git a/crates/matrix-sdk-common/src/lib.rs b/crates/matrix-sdk-common/src/lib.rs index 3c2480e58..fe56eaf4f 100644 --- a/crates/matrix-sdk-common/src/lib.rs +++ b/crates/matrix-sdk-common/src/lib.rs @@ -66,3 +66,21 @@ impl SyncOutsideWasm for T {} /// implemented, while other targets will. pub trait AsyncTraitDeps: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm {} impl AsyncTraitDeps for T {} + +// TODO: Remove in favor of impl Trait once allowed in associated types +#[macro_export] +macro_rules! boxed_into_future { + () => { + $crate::boxed_into_future!(extra_bounds: ); + }; + (extra_bounds: $($extra_bounds:tt)*) => { + #[cfg(target_arch = "wasm32")] + type IntoFuture = ::std::pin::Pin<::std::boxed::Box< + dyn ::std::future::Future + $($extra_bounds)* + >>; + #[cfg(not(target_arch = "wasm32"))] + type IntoFuture = ::std::pin::Pin<::std::boxed::Box< + dyn ::std::future::Future + Send + $($extra_bounds)* + >>; + }; +} diff --git a/crates/matrix-sdk-ui/src/timeline/futures.rs b/crates/matrix-sdk-ui/src/timeline/futures.rs index b4e844ab4..b5e5f93ca 100644 --- a/crates/matrix-sdk-ui/src/timeline/futures.rs +++ b/crates/matrix-sdk-ui/src/timeline/futures.rs @@ -1,12 +1,8 @@ -use std::{ - fs, - future::{Future, IntoFuture}, - path::Path, - pin::Pin, -}; +use std::{fs, future::IntoFuture, path::Path}; use eyeball::{SharedObservable, Subscriber}; use matrix_sdk::{attachment::AttachmentConfig, TransmissionProgress}; +use matrix_sdk_base::boxed_into_future; use mime::Mime; use super::{Error, Timeline}; @@ -39,10 +35,7 @@ impl<'a> SendAttachment<'a> { impl<'a> IntoFuture for SendAttachment<'a> { type Output = Result<(), Error>; - #[cfg(target_arch = "wasm32")] - type IntoFuture = Pin + 'a>>; - #[cfg(not(target_arch = "wasm32"))] - type IntoFuture = Pin + Send + 'a>>; + boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { let Self { timeline, url, mime_type, config, send_progress } = self; diff --git a/crates/matrix-sdk/src/client/futures.rs b/crates/matrix-sdk/src/client/futures.rs index d06ffe242..02f0cd085 100644 --- a/crates/matrix-sdk/src/client/futures.rs +++ b/crates/matrix-sdk/src/client/futures.rs @@ -14,11 +14,7 @@ #![deny(unreachable_pub)] -use std::{ - fmt::Debug, - future::{Future, IntoFuture}, - pin::Pin, -}; +use std::{fmt::Debug, future::IntoFuture}; use cfg_vis::cfg_vis; use eyeball::SharedObservable; @@ -32,6 +28,7 @@ use mas_oidc_client::{ }, types::errors::ClientErrorCode, }; +use matrix_sdk_common::boxed_into_future; use ruma::api::{client::error::ErrorKind, error::FromHttpResponseError, OutgoingRequest}; #[cfg(feature = "experimental-oidc")] use tracing::error; @@ -87,10 +84,7 @@ where HttpError: From>, { type Output = HttpResult; - #[cfg(target_arch = "wasm32")] - type IntoFuture = Pin>>; - #[cfg(not(target_arch = "wasm32"))] - type IntoFuture = Pin + Send>>; + boxed_into_future!(); fn into_future(self) -> Self::IntoFuture { let Self { client, request, config, send_progress, sliding_sync_proxy_url } = self; diff --git a/crates/matrix-sdk/src/encryption/futures.rs b/crates/matrix-sdk/src/encryption/futures.rs index bba37c947..2de6d99ba 100644 --- a/crates/matrix-sdk/src/encryption/futures.rs +++ b/crates/matrix-sdk/src/encryption/futures.rs @@ -17,16 +17,13 @@ #![deny(unreachable_pub)] -use std::{ - future::{Future, IntoFuture}, - io::Read, - pin::Pin, -}; +use std::{future::IntoFuture, io::Read}; use cfg_vis::cfg_vis; use eyeball::SharedObservable; #[cfg(not(target_arch = "wasm32"))] use eyeball::Subscriber; +use matrix_sdk_common::boxed_into_future; use ruma::events::room::{EncryptedFile, EncryptedFileInit}; use crate::{Client, Result, TransmissionProgress}; @@ -73,10 +70,7 @@ where R: Read + Send + ?Sized + 'a, { type Output = Result; - #[cfg(target_arch = "wasm32")] - type IntoFuture = Pin + 'a>>; - #[cfg(not(target_arch = "wasm32"))] - type IntoFuture = Pin + Send + 'a>>; + boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { let Self { client, content_type, reader, send_progress } = self; diff --git a/crates/matrix-sdk/src/matrix_auth/login_builder.rs b/crates/matrix-sdk/src/matrix_auth/login_builder.rs index a5d893f18..95e6aa5dc 100644 --- a/crates/matrix-sdk/src/matrix_auth/login_builder.rs +++ b/crates/matrix-sdk/src/matrix_auth/login_builder.rs @@ -14,11 +14,9 @@ // limitations under the License. #![cfg_attr(not(target_arch = "wasm32"), deny(clippy::future_not_send))] -use std::{ - future::{Future, IntoFuture}, - pin::Pin, -}; +use std::future::{Future, IntoFuture}; +use matrix_sdk_common::boxed_into_future; use ruma::{ api::client::{session::login, uiaa::UserIdentifier}, assign, @@ -212,8 +210,7 @@ impl LoginBuilder { impl IntoFuture for LoginBuilder { type Output = Result; - // TODO: Use impl Trait once allowed in this position on stable - type IntoFuture = Pin>>; + boxed_into_future!(); fn into_future(self) -> Self::IntoFuture { Box::pin(self.send()) @@ -479,8 +476,7 @@ where Fut: Future> + Send + 'static, { type Output = Result; - // TODO: Use impl Trait once allowed in this position on stable - type IntoFuture = Pin>>; + boxed_into_future!(); fn into_future(self) -> Self::IntoFuture { Box::pin(self.send()) diff --git a/crates/matrix-sdk/src/room/futures.rs b/crates/matrix-sdk/src/room/futures.rs index 56b7a16a6..f40a92af3 100644 --- a/crates/matrix-sdk/src/room/futures.rs +++ b/crates/matrix-sdk/src/room/futures.rs @@ -16,14 +16,12 @@ #![deny(unreachable_pub)] +use std::future::IntoFuture; #[cfg(feature = "image-proc")] use std::io::Cursor; -use std::{ - future::{Future, IntoFuture}, - pin::Pin, -}; use eyeball::SharedObservable; +use matrix_sdk_common::boxed_into_future; use mime::Mime; use ruma::api::client::message::send_message_event; use tracing::{Instrument, Span}; @@ -81,10 +79,7 @@ impl<'a> SendAttachment<'a> { impl<'a> IntoFuture for SendAttachment<'a> { type Output = Result; - #[cfg(target_arch = "wasm32")] - type IntoFuture = Pin + 'a>>; - #[cfg(not(target_arch = "wasm32"))] - type IntoFuture = Pin + Send + 'a>>; + boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { let Self { room, body, content_type, data, config, tracing_span, send_progress } = self;