Make IntoFuture implementations a little easier to read and write

This commit is contained in:
Jonas Platte
2023-10-30 16:37:09 +01:00
committed by Jonas Platte
parent 18b714116e
commit 4b702da8e8
6 changed files with 34 additions and 44 deletions

View File

@@ -66,3 +66,21 @@ impl<T> SyncOutsideWasm for T {}
/// implemented, while other targets will.
pub trait AsyncTraitDeps: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm {}
impl<T: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm> 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<Output = Self::Output> + $($extra_bounds)*
>>;
#[cfg(not(target_arch = "wasm32"))]
type IntoFuture = ::std::pin::Pin<::std::boxed::Box<
dyn ::std::future::Future<Output = Self::Output> + Send + $($extra_bounds)*
>>;
};
}

View File

@@ -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<Box<dyn Future<Output = Self::Output> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
boxed_into_future!(extra_bounds: 'a);
fn into_future(self) -> Self::IntoFuture {
let Self { timeline, url, mime_type, config, send_progress } = self;

View File

@@ -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<FromHttpResponseError<R::EndpointError>>,
{
type Output = HttpResult<R::IncomingResponse>;
#[cfg(target_arch = "wasm32")]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
#[cfg(not(target_arch = "wasm32"))]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
boxed_into_future!();
fn into_future(self) -> Self::IntoFuture {
let Self { client, request, config, send_progress, sliding_sync_proxy_url } = self;

View File

@@ -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<EncryptedFile>;
#[cfg(target_arch = "wasm32")]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
boxed_into_future!(extra_bounds: 'a);
fn into_future(self) -> Self::IntoFuture {
let Self { client, content_type, reader, send_progress } = self;

View File

@@ -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<login::v3::Response>;
// TODO: Use impl Trait once allowed in this position on stable
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
boxed_into_future!();
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())
@@ -479,8 +476,7 @@ where
Fut: Future<Output = Result<()>> + Send + 'static,
{
type Output = Result<login::v3::Response>;
// TODO: Use impl Trait once allowed in this position on stable
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
boxed_into_future!();
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())

View File

@@ -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<send_message_event::v3::Response>;
#[cfg(target_arch = "wasm32")]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 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;