Skip to content

Commit

Permalink
chore(sdk): add a custom lifetime to SendRequest
Browse files Browse the repository at this point in the history
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…
  • Loading branch information
bnjbvr committed Oct 21, 2024
1 parent 900e06a commit c03228d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,8 @@ impl Client {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Result<String, ClientError> {
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();
Expand Down
17 changes: 10 additions & 7 deletions crates/matrix-sdk/src/client/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -44,15 +44,16 @@ use crate::{

/// `IntoFuture` returned by [`Client::send`].
#[allow(missing_debug_implementations)]
pub struct SendRequest<R> {
pub struct SendRequest<'a, R> {
pub(crate) client: Client,
pub(crate) homeserver_override: Option<String>,
pub(crate) request: R,
pub(crate) config: Option<RequestConfig>,
pub(crate) send_progress: SharedObservable<TransmissionProgress>,
pub(crate) _phantom: PhantomData<&'a ()>,
}

impl<R> SendRequest<R> {
impl<'a, R> SendRequest<'a, R> {
/// Replace the default `SharedObservable` used for tracking upload
/// progress.
///
Expand Down Expand Up @@ -85,17 +86,19 @@ impl<R> SendRequest<R> {
}
}

impl<R> IntoFuture for SendRequest<R>
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<FromHttpResponseError<R::EndpointError>>,
{
type Output = HttpResult<R::IncomingResponse>;
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(
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ impl Client {
&self,
request: Request,
config: Option<RequestConfig>,
) -> SendRequest<Request>
) -> SendRequest<'_, Request>
where
Request: OutgoingRequest + Clone + Debug,
HttpError: From<FromHttpResponseError<Request::EndpointError>>,
Expand All @@ -1425,6 +1425,7 @@ impl Client {
config,
send_progress: Default::default(),
homeserver_override: None,
_phantom: Default::default(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl fmt::Display for PersistError {
}

/// `IntoFuture` returned by [`Media::upload`].
pub type SendUploadRequest = SendRequest<media::create_content::v3::Request>;
pub type SendUploadRequest<'a> = SendRequest<'a, media::create_content::v3::Request>;

impl Media {
pub(crate) fn new(client: Client) -> Self {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Media {
/// println!("Cat URI: {}", response.content_uri);
/// # anyhow::Ok(()) };
/// ```
pub fn upload(&self, content_type: &Mime, data: Vec<u8>) -> SendUploadRequest {
pub fn upload(&self, content_type: &Mime, data: Vec<u8>) -> SendUploadRequest<'_> {
let timeout = std::cmp::max(
Duration::from_secs(data.len() as u64 / DEFAULT_UPLOAD_SPEED),
MIN_UPLOAD_REQUEST_TIMEOUT,
Expand Down

0 comments on commit c03228d

Please sign in to comment.