Avoid needless copies by changing http::Request<Vec<u8>> to http::Request<Bytes>

master
Jonas Platte 2021-04-26 14:21:45 +02:00 committed by Johannes Becker
parent 242d46c9a1
commit bd02ff901f
2 changed files with 12 additions and 8 deletions

View File

@ -30,7 +30,9 @@ use matrix_sdk_common::{
FromHttpResponseError, IncomingResponse, SendAccessToken, 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 /// Abstraction around the http layer. The allows implementors to use different
/// http libraries. /// http libraries.
@ -70,7 +72,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// impl HttpSend for Client { /// impl HttpSend for Client {
/// async fn send_request( /// async fn send_request(
/// &self, /// &self,
/// request: http::Request<Vec<u8>>, /// request: http::Request<Bytes>,
/// config: RequestConfig, /// config: RequestConfig,
/// ) -> Result<http::Response<Bytes>, HttpError> { /// ) -> Result<http::Response<Bytes>, HttpError> {
/// Ok(self /// Ok(self
@ -85,7 +87,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// ``` /// ```
async fn send_request( async fn send_request(
&self, &self,
request: http::Request<Vec<u8>>, request: http::Request<Bytes>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Bytes>, HttpError>; ) -> Result<http::Response<Bytes>, HttpError>;
} }
@ -135,7 +137,9 @@ impl HttpClient {
} }
}; };
request.try_into_http_request(&self.homeserver.to_string(), access_token)? request
.try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)?
.map(|body| body.freeze())
}; };
self.inner.send_request(request, config).await self.inner.send_request(request, config).await
@ -238,7 +242,7 @@ async fn response_to_http_response(
#[cfg(any(target_arch = "wasm32"))] #[cfg(any(target_arch = "wasm32"))]
async fn send_request( async fn send_request(
client: &Client, client: &Client,
request: http::Request<Vec<u8>>, request: http::Request<Bytes>,
_: RequestConfig, _: RequestConfig,
) -> Result<http::Response<Bytes>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
let request = reqwest::Request::try_from(request)?; let request = reqwest::Request::try_from(request)?;
@ -250,7 +254,7 @@ async fn send_request(
#[cfg(all(not(target_arch = "wasm32")))] #[cfg(all(not(target_arch = "wasm32")))]
async fn send_request( async fn send_request(
client: &Client, client: &Client,
request: http::Request<Vec<u8>>, request: http::Request<Bytes>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Bytes>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
let mut backoff = ExponentialBackoff::default(); let mut backoff = ExponentialBackoff::default();
@ -312,7 +316,7 @@ async fn send_request(
impl HttpSend for Client { impl HttpSend for Client {
async fn send_request( async fn send_request(
&self, &self,
request: http::Request<Vec<u8>>, request: http::Request<Bytes>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Bytes>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
send_request(&self, request, config).await send_request(&self, request, config).await

View File

@ -77,7 +77,7 @@ pub use matrix_sdk_base::{
Session, StateChanges, StoreError, Session, StateChanges, StoreError,
}; };
pub use bytes::Bytes; pub use bytes::{Bytes, BytesMut};
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;
pub use reqwest; pub use reqwest;