From 0a7ac18d9f4e2df30efccdaae62fd7d27d3d1e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 24 Jul 2025 14:20:03 +0200 Subject: [PATCH] refactor: Add IsPrefix = False bound on StaticEventContent bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since those APIs only support a full event type, not an event type prefix. Signed-off-by: Kévin Commaille --- crates/matrix-sdk-base/src/store/mod.rs | 10 +++++-- crates/matrix-sdk-base/src/store/traits.rs | 20 +++++++++---- crates/matrix-sdk/src/account.rs | 4 +-- crates/matrix-sdk/src/room/mod.rs | 18 ++++++++---- testing/matrix-sdk-test/src/event_factory.rs | 31 ++++++++++++-------- 5 files changed, 54 insertions(+), 29 deletions(-) diff --git a/crates/matrix-sdk-base/src/store/mod.rs b/crates/matrix-sdk-base/src/store/mod.rs index a389c059f..18140fe5a 100644 --- a/crates/matrix-sdk-base/src/store/mod.rs +++ b/crates/matrix-sdk-base/src/store/mod.rs @@ -578,7 +578,9 @@ impl StateChanges { state_key: &K, ) -> Option<&Raw>> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, C::StateKey: Borrow, K: AsRef + ?Sized, @@ -599,7 +601,7 @@ impl StateChanges { state_key: &K, ) -> Option<&Raw>> where - C: StaticEventContent + StaticStateEventContent, + C: StaticEventContent + StaticStateEventContent, C::StateKey: Borrow, K: AsRef + ?Sized, { @@ -620,7 +622,9 @@ impl StateChanges { state_key: &K, ) -> Option> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, C::PossiblyRedacted: StaticEventContent + DeserializeOwned, C::StateKey: Borrow, diff --git a/crates/matrix-sdk-base/src/store/traits.rs b/crates/matrix-sdk-base/src/store/traits.rs index 3f9f5bed4..6879327ef 100644 --- a/crates/matrix-sdk-base/src/store/traits.rs +++ b/crates/matrix-sdk-base/src/store/traits.rs @@ -788,7 +788,9 @@ pub trait StateStoreExt: StateStore { room_id: &RoomId, ) -> Result>, Self::Error> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, { Ok(self.get_state_event(room_id, C::TYPE.into(), "").await?.map(|raw| raw.cast())) @@ -805,7 +807,9 @@ pub trait StateStoreExt: StateStore { state_key: &K, ) -> Result>, Self::Error> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::StateKey: Borrow, C::Redacted: RedactedStateEventContent, K: AsRef + ?Sized + Sync, @@ -826,7 +830,9 @@ pub trait StateStoreExt: StateStore { room_id: &RoomId, ) -> Result>, Self::Error> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, { // FIXME: Could be more efficient, if we had streaming store accessor functions @@ -852,7 +858,9 @@ pub trait StateStoreExt: StateStore { state_keys: I, ) -> Result>, Self::Error> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::StateKey: Borrow, C::Redacted: RedactedStateEventContent, K: AsRef + Sized + Sync + 'a, @@ -876,7 +884,7 @@ pub trait StateStoreExt: StateStore { &self, ) -> Result>>, Self::Error> where - C: StaticEventContent + GlobalAccountDataEventContent, + C: StaticEventContent + GlobalAccountDataEventContent, { Ok(self.get_account_data_event(C::TYPE.into()).await?.map(Raw::cast_unchecked)) } @@ -893,7 +901,7 @@ pub trait StateStoreExt: StateStore { room_id: &RoomId, ) -> Result>>, Self::Error> where - C: StaticEventContent + RoomAccountDataEventContent, + C: StaticEventContent + RoomAccountDataEventContent, { Ok(self .get_room_account_data_event(room_id, C::TYPE.into()) diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index 6824b5dbf..ad38c9de5 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -722,7 +722,7 @@ impl Account { /// ``` pub async fn account_data(&self) -> Result>> where - C: GlobalAccountDataEventContent + StaticEventContent, + C: GlobalAccountDataEventContent + StaticEventContent, { get_raw_content(self.client.state_store().get_account_data_event_static::().await?) } @@ -785,7 +785,7 @@ impl Account { /// Fetch an account data event of statically-known type from the server. pub async fn fetch_account_data_static(&self) -> Result>> where - C: GlobalAccountDataEventContent + StaticEventContent, + C: GlobalAccountDataEventContent + StaticEventContent, { Ok(self.fetch_account_data(C::TYPE.into()).await?.map(Raw::cast_unchecked)) } diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 70481738c..a6d5e0ed9 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -1017,7 +1017,9 @@ impl Room { /// ``` pub async fn get_state_events_static(&self) -> Result>> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, { Ok(self.client.state_store().get_state_events_static(self.room_id()).await?) @@ -1061,7 +1063,9 @@ impl Room { state_keys: I, ) -> Result>> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::StateKey: Borrow, C::Redacted: RedactedStateEventContent, K: AsRef + Sized + Sync + 'a, @@ -1108,7 +1112,9 @@ impl Room { /// ``` pub async fn get_state_event_static(&self) -> Result>> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::Redacted: RedactedStateEventContent, { self.get_state_event_static_for_key(&EmptyStateKey).await @@ -1138,7 +1144,9 @@ impl Room { state_key: &K, ) -> Result>> where - C: StaticEventContent + StaticStateEventContent + RedactContent, + C: StaticEventContent + + StaticStateEventContent + + RedactContent, C::StateKey: Borrow, C::Redacted: RedactedStateEventContent, K: AsRef + ?Sized + Sync, @@ -1257,7 +1265,7 @@ impl Room { /// ``` pub async fn account_data_static(&self) -> Result>>> where - C: StaticEventContent + RoomAccountDataEventContent, + C: StaticEventContent + RoomAccountDataEventContent, { Ok(self.account_data(C::TYPE.into()).await?.map(Raw::cast_unchecked)) } diff --git a/testing/matrix-sdk-test/src/event_factory.rs b/testing/matrix-sdk-test/src/event_factory.rs index 48ad077b2..faab9245a 100644 --- a/testing/matrix-sdk-test/src/event_factory.rs +++ b/testing/matrix-sdk-test/src/event_factory.rs @@ -29,8 +29,9 @@ use ruma::{ TransactionId, UInt, UserId, VoipVersionId, events::{ AnyStateEvent, AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent, - AnyTimelineEvent, BundledMessageLikeRelations, Mentions, RedactedMessageLikeEventContent, - RedactedStateEventContent, StateEventContent, StaticEventContent, + AnyTimelineEvent, BundledMessageLikeRelations, False, Mentions, + RedactedMessageLikeEventContent, RedactedStateEventContent, StateEventContent, + StaticEventContent, beacon::BeaconEventContent, call::{ SessionDescription, @@ -143,7 +144,7 @@ impl Default for Unsigned { } #[derive(Debug)] -pub struct EventBuilder { +pub struct EventBuilder> { sender: Option, /// Whether the event is an ephemeral one. As such, it doesn't require a /// room id or a sender. @@ -159,7 +160,7 @@ pub struct EventBuilder { state_key: Option, } -impl EventBuilder { +impl> EventBuilder { pub fn room(mut self, room_id: &RoomId) -> Self { self.room = Some(room_id.to_owned()); self @@ -243,7 +244,7 @@ impl EventBuilder { impl EventBuilder where - E: StaticEventContent + Serialize, + E: StaticEventContent + Serialize, { #[inline(always)] fn construct_json(self, requires_room: bool) -> serde_json::Value { @@ -460,7 +461,7 @@ impl EventBuilder { } } -impl From> for Raw +impl> From> for Raw where E: Serialize, { @@ -469,7 +470,7 @@ where } } -impl From> for Raw +impl> From> for Raw where E: Serialize, { @@ -478,7 +479,7 @@ where } } -impl From> for TimelineEvent +impl> From> for TimelineEvent where E: Serialize, { @@ -487,13 +488,17 @@ where } } -impl From> for Raw { +impl + StateEventContent> From> + for Raw +{ fn from(val: EventBuilder) -> Self { Raw::new(&val.construct_json(false)).unwrap().cast_unchecked() } } -impl From> for Raw { +impl + StateEventContent> From> + for Raw +{ fn from(val: EventBuilder) -> Self { Raw::new(&val.construct_json(true)).unwrap().cast_unchecked() } @@ -531,7 +536,7 @@ impl EventFactory { } /// Create an event from any event content. - pub fn event(&self, content: E) -> EventBuilder { + pub fn event>(&self, content: E) -> EventBuilder { EventBuilder { sender: self.sender.clone(), is_ephemeral: false, @@ -701,7 +706,7 @@ impl EventFactory { /// Create a redacted event, with extra information in the unsigned section /// about the redaction itself. - pub fn redacted( + pub fn redacted + RedactedMessageLikeEventContent>( &self, redacter: &UserId, content: T, @@ -722,7 +727,7 @@ impl EventFactory { /// Create a redacted state event, with extra information in the unsigned /// section about the redaction itself. - pub fn redacted_state( + pub fn redacted_state + RedactedStateEventContent>( &self, redacter: &UserId, state_key: impl Into,