From e12264bcb63df537ae65eb7caa5e26c9e8bbe2ab Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 4 Apr 2025 12:22:42 +0200 Subject: [PATCH] refactor(sdk): Reduce the size of `Error::WrongRoomState`. This patch boxes the error in `Error::WrongRoomState` to reduce the size of this variant (from 24 bytes to 16 bytes). --- crates/matrix-sdk/src/error.rs | 2 +- crates/matrix-sdk/src/room/mod.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index 6abd0c112..b9d9e39db 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -348,7 +348,7 @@ pub enum Error { /// specific membership state in the room, but the membership state is /// different. #[error("wrong room state: {0}")] - WrongRoomState(WrongRoomState), + WrongRoomState(Box), /// Session callbacks have been set multiple times. #[error("session callbacks have been set multiple times")] diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 49e1381ac..9fd1d8774 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -210,7 +210,10 @@ impl Room { pub async fn leave(&self) -> Result<()> { let state = self.state(); if state == RoomState::Left { - return Err(Error::WrongRoomState(WrongRoomState::new("Joined or Invited", state))); + return Err(Error::WrongRoomState(Box::new(WrongRoomState::new( + "Joined or Invited", + state, + )))); } let request = leave_room::v3::Request::new(self.inner.room_id().to_owned()); @@ -226,7 +229,10 @@ impl Room { pub async fn join(&self) -> Result<()> { let state = self.state(); if state == RoomState::Joined { - return Err(Error::WrongRoomState(WrongRoomState::new("Invited or Left", state))); + return Err(Error::WrongRoomState(Box::new(WrongRoomState::new( + "Invited or Left", + state, + )))); } let prev_room_state = self.inner.state(); @@ -2938,7 +2944,7 @@ impl Room { pub async fn invite_details(&self) -> Result { let state = self.state(); if state != RoomState::Invited { - return Err(Error::WrongRoomState(WrongRoomState::new("Invited", state))); + return Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Invited", state)))); } let invitee = self @@ -2989,7 +2995,10 @@ impl Room { let state = self.state(); match state { RoomState::Joined | RoomState::Invited | RoomState::Knocked => { - return Err(Error::WrongRoomState(WrongRoomState::new("Left / Banned", state))); + return Err(Error::WrongRoomState(Box::new(WrongRoomState::new( + "Left / Banned", + state, + )))); } RoomState::Left | RoomState::Banned => {} } @@ -3016,7 +3025,7 @@ impl Room { if state == RoomState::Joined { Ok(()) } else { - Err(Error::WrongRoomState(WrongRoomState::new("Joined", state))) + Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Joined", state)))) } } @@ -3099,7 +3108,7 @@ impl Room { ) -> Result { let state = self.state(); if state != RoomState::Joined { - return Err(Error::WrongRoomState(WrongRoomState::new("Joined", state))); + return Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Joined", state)))); } let request = report_content::v3::Request::new(