From c03228d2cc391fa52af7b03522c327bbbbd7a198 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 21 Oct 2024 18:03:19 +0200 Subject: [PATCH] chore(sdk): add a custom lifetime to `SendRequest` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows using a `Request` type that doesn't have a static lifetime. I'm not sure that what I've done is right or wrong, but it seems to pass tests… --- bindings/matrix-sdk-ffi/src/client.rs | 3 ++- crates/matrix-sdk/src/client/futures.rs | 17 ++++++++++------- crates/matrix-sdk/src/client/mod.rs | 3 ++- crates/matrix-sdk/src/media.rs | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 1c972151e9d..3c8f7d1da6d 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -679,7 +679,8 @@ impl Client { progress_watcher: Option>, ) -> Result { let mime_type: mime::Mime = mime_type.parse().context("Parsing mime type")?; - let request = self.inner.media().upload(&mime_type, data); + let media = self.inner.media(); + let request = media.upload(&mime_type, data); if let Some(progress_watcher) = progress_watcher { let mut subscriber = request.subscribe_to_send_progress(); diff --git a/crates/matrix-sdk/src/client/futures.rs b/crates/matrix-sdk/src/client/futures.rs index 6f477a2cb58..a4586e4a44d 100644 --- a/crates/matrix-sdk/src/client/futures.rs +++ b/crates/matrix-sdk/src/client/futures.rs @@ -14,7 +14,7 @@ #![deny(unreachable_pub)] -use std::{fmt::Debug, future::IntoFuture}; +use std::{fmt::Debug, future::IntoFuture, marker::PhantomData}; use eyeball::SharedObservable; #[cfg(not(target_arch = "wasm32"))] @@ -44,15 +44,16 @@ use crate::{ /// `IntoFuture` returned by [`Client::send`]. #[allow(missing_debug_implementations)] -pub struct SendRequest { +pub struct SendRequest<'a, R> { pub(crate) client: Client, pub(crate) homeserver_override: Option, pub(crate) request: R, pub(crate) config: Option, pub(crate) send_progress: SharedObservable, + pub(crate) _phantom: PhantomData<&'a ()>, } -impl SendRequest { +impl<'a, R> SendRequest<'a, R> { /// Replace the default `SharedObservable` used for tracking upload /// progress. /// @@ -85,17 +86,19 @@ impl SendRequest { } } -impl IntoFuture for SendRequest +impl<'a, R> IntoFuture for SendRequest<'a, R> where - R: OutgoingRequest + Clone + Debug + Send + Sync + 'static, + R: OutgoingRequest + Clone + Debug + Send + Sync + 'a, R::IncomingResponse: Send + Sync, HttpError: From>, { type Output = HttpResult; - boxed_into_future!(); + + boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { - let Self { client, request, config, send_progress, homeserver_override } = self; + let Self { client, request, config, send_progress, homeserver_override, _phantom: _ } = + self; Box::pin(async move { let res = Box::pin(client.send_inner( diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index 92619b03089..6843da56371 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -1414,7 +1414,7 @@ impl Client { &self, request: Request, config: Option, - ) -> SendRequest + ) -> SendRequest<'_, Request> where Request: OutgoingRequest + Clone + Debug, HttpError: From>, @@ -1425,6 +1425,7 @@ impl Client { config, send_progress: Default::default(), homeserver_override: None, + _phantom: Default::default(), } } diff --git a/crates/matrix-sdk/src/media.rs b/crates/matrix-sdk/src/media.rs index 02fd602f0da..1e908e1269b 100644 --- a/crates/matrix-sdk/src/media.rs +++ b/crates/matrix-sdk/src/media.rs @@ -106,7 +106,7 @@ impl fmt::Display for PersistError { } /// `IntoFuture` returned by [`Media::upload`]. -pub type SendUploadRequest = SendRequest; +pub type SendUploadRequest<'a> = SendRequest<'a, media::create_content::v3::Request>; impl Media { pub(crate) fn new(client: Client) -> Self { @@ -140,7 +140,7 @@ impl Media { /// println!("Cat URI: {}", response.content_uri); /// # anyhow::Ok(()) }; /// ``` - pub fn upload(&self, content_type: &Mime, data: Vec) -> SendUploadRequest { + pub fn upload(&self, content_type: &Mime, data: Vec) -> SendUploadRequest<'_> { let timeout = std::cmp::max( Duration::from_secs(data.len() as u64 / DEFAULT_UPLOAD_SPEED), MIN_UPLOAD_REQUEST_TIMEOUT,