From e41dbd63004bf6c0b7dcaef268d4c64aa9c3699d Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 16 May 2025 12:13:50 +0200 Subject: [PATCH] refactor(sdk): Reduce size of `HttpError`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch reduces the size of `HttpError` from 160 bytes to 24 bytes. --- .../src/client/builder/homeserver_config.rs | 2 +- crates/matrix-sdk/src/error.rs | 31 +++++++++++++------ .../src/widget/machine/from_widget.rs | 7 ++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk/src/client/builder/homeserver_config.rs b/crates/matrix-sdk/src/client/builder/homeserver_config.rs index e4f6f4238..41e09073b 100644 --- a/crates/matrix-sdk/src/client/builder/homeserver_config.rs +++ b/crates/matrix-sdk/src/client/builder/homeserver_config.rs @@ -174,7 +174,7 @@ async fn discover_homeserver( ) .await .map_err(|e| match e { - HttpError::Api(err) => ClientBuildError::AutoDiscovery(err), + HttpError::Api(err) => ClientBuildError::AutoDiscovery(*err), err => ClientBuildError::Http(err), })?; diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index 19c19c0da..3766d5fc3 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -100,8 +100,9 @@ pub enum HttpError { NotClientRequest, /// API response error (deserialization, or a Matrix-specific error). + // `Box` its inner value to reduce the enum size. #[error(transparent)] - Api(#[from] FromHttpResponseError), + Api(#[from] Box>), /// Error when creating an API request (e.g. serialization of /// body/headers/query parameters). @@ -121,7 +122,12 @@ impl HttpError { /// /// Otherwise, returns `None`. pub fn as_ruma_api_error(&self) -> Option<&RumaApiError> { - as_variant!(self, Self::Api(FromHttpResponseError::Server(e)) => e) + match self { + Self::Api(error) => { + as_variant!(error.as_ref(), FromHttpResponseError::Server) + }, + _ => None + } } /// Shorthand for @@ -163,14 +169,21 @@ impl HttpError { // internet, or that the remote is, so retry a few times. HttpError::Reqwest(_) => RetryKind::NetworkFailure, - HttpError::Api(FromHttpResponseError::Server(api_error)) => { - RetryKind::from_api_error(api_error) - } + HttpError::Api(error) => match error.as_ref() { + FromHttpResponseError::Server(api_error) => RetryKind::from_api_error(api_error), + _ => RetryKind::Permanent, + }, _ => RetryKind::Permanent, } } } +impl From> for HttpError { + fn from(value: FromHttpResponseError) -> Self { + Self::Api(Box::new(value)) + } +} + /// How should we behave with respect to retry behavior after an [`HttpError`] /// happened? pub(crate) enum RetryKind { @@ -537,22 +550,22 @@ pub enum RoomKeyImportError { impl From> for HttpError { fn from(err: FromHttpResponseError) -> Self { - Self::Api(err.map(RumaApiError::ClientApi)) + Self::Api(Box::new(err.map(RumaApiError::ClientApi))) } } impl From> for HttpError { fn from(err: FromHttpResponseError) -> Self { - Self::Api(err.map(|e| match e { + Self::Api(Box::new(err.map(|e| match e { UiaaResponse::AuthResponse(i) => RumaApiError::Uiaa(i), UiaaResponse::MatrixError(e) => RumaApiError::ClientApi(e), - })) + }))) } } impl From> for HttpError { fn from(err: FromHttpResponseError) -> Self { - Self::Api(err.map(RumaApiError::Other)) + Self::Api(Box::new(err.map(RumaApiError::Other))) } } diff --git a/crates/matrix-sdk/src/widget/machine/from_widget.rs b/crates/matrix-sdk/src/widget/machine/from_widget.rs index 0ff7adf97..d75d47fba 100644 --- a/crates/matrix-sdk/src/widget/machine/from_widget.rs +++ b/crates/matrix-sdk/src/widget/machine/from_widget.rs @@ -52,7 +52,12 @@ impl FromWidgetErrorResponse { /// Create a error response to send to the widget from an http error. pub(crate) fn from_http_error(error: HttpError) -> Self { let message = error.to_string(); - let matrix_api_error = as_variant!(error, HttpError::Api(ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err))) => err); + let matrix_api_error = match error { + HttpError::Api(error) => { + as_variant!(*error, ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err)) => err) + } + _ => None, + }; Self { error: FromWidgetError {