From bd02ff901fdb07e70cf60efdffc654ff47dc17ff Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 26 Apr 2021 14:21:45 +0200 Subject: [PATCH] Avoid needless copies by changing http::Request> to http::Request --- matrix_sdk/src/http_client.rs | 18 +++++++++++------- matrix_sdk/src/lib.rs | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/matrix_sdk/src/http_client.rs b/matrix_sdk/src/http_client.rs index bd2f0d84..88ccb7aa 100644 --- a/matrix_sdk/src/http_client.rs +++ b/matrix_sdk/src/http_client.rs @@ -30,7 +30,9 @@ use matrix_sdk_common::{ FromHttpResponseError, IncomingResponse, SendAccessToken, }; -use crate::{error::HttpError, Bytes, ClientConfig, OutgoingRequest, RequestConfig, Session}; +use crate::{ + error::HttpError, Bytes, BytesMut, ClientConfig, OutgoingRequest, RequestConfig, Session, +}; /// Abstraction around the http layer. The allows implementors to use different /// http libraries. @@ -70,7 +72,7 @@ pub trait HttpSend: AsyncTraitDeps { /// impl HttpSend for Client { /// async fn send_request( /// &self, - /// request: http::Request>, + /// request: http::Request, /// config: RequestConfig, /// ) -> Result, HttpError> { /// Ok(self @@ -85,7 +87,7 @@ pub trait HttpSend: AsyncTraitDeps { /// ``` async fn send_request( &self, - request: http::Request>, + request: http::Request, config: RequestConfig, ) -> Result, HttpError>; } @@ -135,7 +137,9 @@ impl HttpClient { } }; - request.try_into_http_request(&self.homeserver.to_string(), access_token)? + request + .try_into_http_request::(&self.homeserver.to_string(), access_token)? + .map(|body| body.freeze()) }; self.inner.send_request(request, config).await @@ -238,7 +242,7 @@ async fn response_to_http_response( #[cfg(any(target_arch = "wasm32"))] async fn send_request( client: &Client, - request: http::Request>, + request: http::Request, _: RequestConfig, ) -> Result, HttpError> { let request = reqwest::Request::try_from(request)?; @@ -250,7 +254,7 @@ async fn send_request( #[cfg(all(not(target_arch = "wasm32")))] async fn send_request( client: &Client, - request: http::Request>, + request: http::Request, config: RequestConfig, ) -> Result, HttpError> { let mut backoff = ExponentialBackoff::default(); @@ -312,7 +316,7 @@ async fn send_request( impl HttpSend for Client { async fn send_request( &self, - request: http::Request>, + request: http::Request, config: RequestConfig, ) -> Result, HttpError> { send_request(&self, request, config).await diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index 419ae7ca..74b2ff40 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -77,7 +77,7 @@ pub use matrix_sdk_base::{ Session, StateChanges, StoreError, }; -pub use bytes::Bytes; +pub use bytes::{Bytes, BytesMut}; pub use matrix_sdk_common::*; pub use reqwest;