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

This commit is contained in:
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,
};
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<Vec<u8>>,
/// request: http::Request<Bytes>,
/// config: RequestConfig,
/// ) -> Result<http::Response<Bytes>, HttpError> {
/// Ok(self
@ -85,7 +87,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// ```
async fn send_request(
&self,
request: http::Request<Vec<u8>>,
request: http::Request<Bytes>,
config: RequestConfig,
) -> 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
@ -238,7 +242,7 @@ async fn response_to_http_response(
#[cfg(any(target_arch = "wasm32"))]
async fn send_request(
client: &Client,
request: http::Request<Vec<u8>>,
request: http::Request<Bytes>,
_: RequestConfig,
) -> Result<http::Response<Bytes>, 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<Vec<u8>>,
request: http::Request<Bytes>,
config: RequestConfig,
) -> Result<http::Response<Bytes>, 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<Vec<u8>>,
request: http::Request<Bytes>,
config: RequestConfig,
) -> Result<http::Response<Bytes>, HttpError> {
send_request(&self, request, config).await

View file

@ -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;