From c8fb8ad9ca7e13e08cc71bb56d8c67f5e99f8605 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 4 Apr 2025 11:59:23 +0200 Subject: [PATCH] refactor(sdk): Reduce the size of `Error::Http`. This patch boxes the error in `Error::Http` to reduce the size of this variant (from 160 bytes to 16 bytes). --- crates/matrix-sdk/src/error.rs | 12 +++++++++--- crates/matrix-sdk/src/widget/machine/from_widget.rs | 2 +- crates/matrix-sdk/src/widget/matrix.rs | 8 ++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index c2c2b97d6..8b8e8cd61 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -14,7 +14,7 @@ //! Error conditions. -use std::{io::Error as IoError, sync::Arc, time::Duration}; +use std::{io::Error as IoError, ops::Deref, sync::Arc, time::Duration}; use as_variant::as_variant; use http::StatusCode; @@ -261,7 +261,7 @@ impl RetryKind { pub enum Error { /// Error doing an HTTP request. #[error(transparent)] - Http(#[from] HttpError), + Http(Box), /// Queried endpoint requires authentication but was called on an anonymous /// client. @@ -431,6 +431,12 @@ impl Error { } } +impl From for Error { + fn from(error: HttpError) -> Self { + Error::Http(Box::new(error)) + } +} + /// Error for the room key importing functionality. #[cfg(feature = "e2e-encryption")] #[derive(Error, Debug)] @@ -505,7 +511,7 @@ impl From for Error { impl From for Error { fn from(e: ReqwestError) -> Self { - Error::Http(HttpError::Reqwest(e)) + Error::Http(Box::new(HttpError::Reqwest(e))) } } diff --git a/crates/matrix-sdk/src/widget/machine/from_widget.rs b/crates/matrix-sdk/src/widget/machine/from_widget.rs index ed5384631..66180a491 100644 --- a/crates/matrix-sdk/src/widget/machine/from_widget.rs +++ b/crates/matrix-sdk/src/widget/machine/from_widget.rs @@ -71,7 +71,7 @@ impl FromWidgetErrorResponse { /// Create a error response to send to the widget from a matrix sdk error. pub(crate) fn from_error(error: Error) -> Self { match error { - Error::Http(e) => FromWidgetErrorResponse::from_http_error(e), + Error::Http(e) => FromWidgetErrorResponse::from_http_error(*e), // For UnknownError's we do not want to have the `unknown error` bit in the message. // Hence we only convert the inner error to a string. Error::UnknownError(e) => FromWidgetErrorResponse::from_string(e.to_string()), diff --git a/crates/matrix-sdk/src/widget/matrix.rs b/crates/matrix-sdk/src/widget/matrix.rs index ea2546b33..0f6ccec06 100644 --- a/crates/matrix-sdk/src/widget/matrix.rs +++ b/crates/matrix-sdk/src/widget/matrix.rs @@ -55,7 +55,11 @@ impl MatrixDriver { /// Requests an OpenID token for the current user. pub(crate) async fn get_open_id(&self) -> Result { let user_id = self.room.own_user_id().to_owned(); - self.room.client.send(OpenIdRequest::new(user_id)).await.map_err(Error::Http) + self.room + .client + .send(OpenIdRequest::new(user_id)) + .await + .map_err(|error| Error::Http(Box::new(error))) } /// Reads the latest `limit` events of a given `event_type` from the room. @@ -172,7 +176,7 @@ impl MatrixDriver { action: UpdateAction, ) -> Result { let r = delayed_events::update_delayed_event::unstable::Request::new(delay_id, action); - self.room.client.send(r).await.map_err(Error::Http) + self.room.client.send(r).await.map_err(|error| Error::Http(Box::new(error))) } /// Starts forwarding new room events. Once the returned `EventReceiver`