diff --git a/crates/ruma-client-api/src/config/set_global_account_data.rs b/crates/ruma-client-api/src/config/set_global_account_data.rs index 067805e5..4596d003 100644 --- a/crates/ruma-client-api/src/config/set_global_account_data.rs +++ b/crates/ruma-client-api/src/config/set_global_account_data.rs @@ -7,7 +7,10 @@ pub mod v3 { use ruma_common::{ api::ruma_api, - events::{AnyGlobalAccountDataEventContent, EventContent, GlobalAccountDataEventType}, + events::{ + AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent, + GlobalAccountDataEventType, + }, serde::Raw, UserId, }; @@ -60,7 +63,7 @@ pub mod v3 { /// `T`s [`Serialize`][serde::Serialize] implementation can fail. pub fn new(data: &'a T, user_id: &'a UserId) -> serde_json::Result where - T: EventContent, + T: GlobalAccountDataEventContent, { Ok(Self { user_id, diff --git a/crates/ruma-client-api/src/config/set_room_account_data.rs b/crates/ruma-client-api/src/config/set_room_account_data.rs index 53a11f53..4c68b9db 100644 --- a/crates/ruma-client-api/src/config/set_room_account_data.rs +++ b/crates/ruma-client-api/src/config/set_room_account_data.rs @@ -7,7 +7,9 @@ pub mod v3 { use ruma_common::{ api::ruma_api, - events::{AnyRoomAccountDataEventContent, EventContent, RoomAccountDataEventType}, + events::{ + AnyRoomAccountDataEventContent, RoomAccountDataEventContent, RoomAccountDataEventType, + }, serde::Raw, RoomId, UserId, }; @@ -68,7 +70,7 @@ pub mod v3 { user_id: &'a UserId, ) -> serde_json::Result where - T: EventContent, + T: RoomAccountDataEventContent, { Ok(Self { data: Raw::from_json(to_raw_json_value(data)?), diff --git a/crates/ruma-client-api/src/message/send_message_event.rs b/crates/ruma-client-api/src/message/send_message_event.rs index f5c12303..8ff5b9c3 100644 --- a/crates/ruma-client-api/src/message/send_message_event.rs +++ b/crates/ruma-client-api/src/message/send_message_event.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::{ api::ruma_api, - events::{AnyMessageLikeEventContent, EventContent, MessageLikeEventType}, + events::{AnyMessageLikeEventContent, MessageLikeEventContent, MessageLikeEventType}, serde::Raw, EventId, RoomId, TransactionId, }; @@ -68,7 +68,7 @@ pub mod v3 { content: &'a T, ) -> serde_json::Result where - T: EventContent, + T: MessageLikeEventContent, { Ok(Self { room_id, diff --git a/crates/ruma-client-api/src/state/send_state_event.rs b/crates/ruma-client-api/src/state/send_state_event.rs index 6b8ec66b..18699eac 100644 --- a/crates/ruma-client-api/src/state/send_state_event.rs +++ b/crates/ruma-client-api/src/state/send_state_event.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::{ api::ruma_api, - events::{AnyStateEventContent, EventContent, StateEventType}, + events::{AnyStateEventContent, StateEventContent, StateEventType}, serde::{Incoming, Raw}, EventId, RoomId, }; @@ -66,7 +66,7 @@ pub mod v3 { content: &'a T, ) -> serde_json::Result where - T: EventContent, + T: StateEventContent, { Ok(Self { room_id, diff --git a/crates/ruma-common/src/events/content.rs b/crates/ruma-common/src/events/content.rs index 997cb5f7..f767ac55 100644 --- a/crates/ruma-common/src/events/content.rs +++ b/crates/ruma-common/src/events/content.rs @@ -5,6 +5,11 @@ use serde_json::value::RawValue as RawJsonValue; use crate::serde::Raw; +use super::{ + EphemeralRoomEventType, GlobalAccountDataEventType, MessageLikeEventType, + RoomAccountDataEventType, StateEventType, ToDeviceEventType, +}; + /// The base trait that all event content types implement. /// /// Use [`macros::EventContent`] to derive this traits. It is not meant to be implemented manually. @@ -128,3 +133,46 @@ pub enum EventKind { /// Presence event kind. Presence, } + +macro_rules! trait_aliases { + // need to use `,` instead of `+` because (1) path can't be followed by `+` + // and (2) `+` can't be used as a separator since it's a repetition operator + ($( + $( #[doc = $docs:literal] )* + trait $id:ident = $( $def:path ),+; + )*) => { + $( + $( #[doc = $docs] )* + pub trait $id: $($def+)+ {} + impl $id for T {} + )* + } +} + +trait_aliases! { + /// An alias for `EventContent`. + trait GlobalAccountDataEventContent = EventContent; + + /// An alias for `EventContent`. + trait RoomAccountDataEventContent = EventContent; + + /// An alias for `EventContent`. + trait EphemeralRoomEventContent = EventContent; + + /// An alias for `EventContent`. + trait MessageLikeEventContent = EventContent; + + /// An alias for `EventContent + RedactedEventContent`. + trait RedactedMessageLikeEventContent = + EventContent, RedactedEventContent; + + /// An alias for `EventContent`. + trait StateEventContent = EventContent; + + /// An alias for `EventContent + RedactedEventContent`. + trait RedactedStateEventContent = + EventContent, RedactedEventContent; + + /// An alias for `EventContent`. + trait ToDeviceEventContent = EventContent; +} diff --git a/crates/ruma-common/src/events/kinds.rs b/crates/ruma-common/src/events/kinds.rs index de7ac3a4..007a7213 100644 --- a/crates/ruma-common/src/events/kinds.rs +++ b/crates/ruma-common/src/events/kinds.rs @@ -4,29 +4,30 @@ use ruma_macros::Event; use serde::{Deserialize, Serialize}; use super::{ - EphemeralRoomEventType, EventContent, GlobalAccountDataEventType, MessageLikeEventType, - MessageLikeUnsigned, RedactedEventContent, RedactedUnsigned, RoomAccountDataEventType, - StateEventType, StateUnsigned, ToDeviceEventType, + EphemeralRoomEventContent, GlobalAccountDataEventContent, MessageLikeEventContent, + MessageLikeEventType, MessageLikeUnsigned, RedactedMessageLikeEventContent, + RedactedStateEventContent, RedactedUnsigned, RoomAccountDataEventContent, StateEventContent, + StateEventType, StateUnsigned, ToDeviceEventContent, }; use crate::{EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId}; /// A global account data event. #[derive(Clone, Debug, Event)] -pub struct GlobalAccountDataEvent> { +pub struct GlobalAccountDataEvent { /// Data specific to the event type. pub content: C, } /// A room account data event. #[derive(Clone, Debug, Event)] -pub struct RoomAccountDataEvent> { +pub struct RoomAccountDataEvent { /// Data specific to the event type. pub content: C, } /// An ephemeral room event. #[derive(Clone, Debug, Event)] -pub struct EphemeralRoomEvent> { +pub struct EphemeralRoomEvent { /// Data specific to the event type. pub content: C, @@ -36,7 +37,7 @@ pub struct EphemeralRoomEvent> { +pub struct SyncEphemeralRoomEvent { /// Data specific to the event type. pub content: C, } @@ -46,7 +47,7 @@ pub struct SyncEphemeralRoomEvent> { +pub struct MessageLikeEvent { /// Data specific to the event type. pub content: C, @@ -71,7 +72,7 @@ pub struct MessageLikeEvent> { /// `SyncMessageLikeEvent` implements the comparison traits using only the `event_id` field, a /// sorted list would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct SyncMessageLikeEvent> { +pub struct SyncMessageLikeEvent { /// Data specific to the event type. pub content: C, @@ -93,9 +94,7 @@ pub struct SyncMessageLikeEvent + RedactedEventContent, -> { +pub struct RedactedMessageLikeEvent { /// Data specific to the event type. pub content: C, @@ -120,9 +119,7 @@ pub struct RedactedMessageLikeEvent< /// `RedactedSyncMessageLikeEvent` implements the comparison traits using only the `event_id` field, /// a sorted list would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct RedactedSyncMessageLikeEvent< - C: EventContent + RedactedEventContent, -> { +pub struct RedactedSyncMessageLikeEvent { /// Data specific to the event type. pub content: C, @@ -144,7 +141,7 @@ pub struct RedactedSyncMessageLikeEvent< /// `StateEvent` implements the comparison traits using only the `event_id` field, a sorted list /// would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct StateEvent> { +pub struct StateEvent { /// Data specific to the event type. pub content: C, @@ -175,7 +172,7 @@ pub struct StateEvent> { /// `SyncStateEvent` implements the comparison traits using only the `event_id` field, a sorted list /// would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct SyncStateEvent> { +pub struct SyncStateEvent { /// Data specific to the event type. pub content: C, @@ -200,7 +197,7 @@ pub struct SyncStateEvent> { /// A stripped-down state event, used for previews of rooms the user has been invited to. #[derive(Clone, Debug, Event)] -pub struct StrippedStateEvent> { +pub struct StrippedStateEvent { /// Data specific to the event type. pub content: C, @@ -216,7 +213,7 @@ pub struct StrippedStateEvent> { /// A minimal state event, used for creating a new room. #[derive(Clone, Debug, Event)] -pub struct InitialStateEvent> { +pub struct InitialStateEvent { /// Data specific to the event type. pub content: C, @@ -235,7 +232,7 @@ pub struct InitialStateEvent> { /// `RedactedStateEvent` implements the comparison traits using only the `event_id` field, a sorted /// list would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct RedactedStateEvent + RedactedEventContent> { +pub struct RedactedStateEvent { /// Data specific to the event type. pub content: C, @@ -266,9 +263,7 @@ pub struct RedactedStateEvent + Reda /// `RedactedSyncStateEvent` implements the comparison traits using only the `event_id` field, a /// sorted list would be sorted lexicographically based on the event's `EventId`. #[derive(Clone, Debug, Event)] -pub struct RedactedSyncStateEvent< - C: EventContent + RedactedEventContent, -> { +pub struct RedactedSyncStateEvent { /// Data specific to the event type. pub content: C, @@ -293,7 +288,7 @@ pub struct RedactedSyncStateEvent< /// An event sent using send-to-device messaging. #[derive(Clone, Debug, Event)] -pub struct ToDeviceEvent> { +pub struct ToDeviceEvent { /// Data specific to the event type. pub content: C, @@ -303,7 +298,7 @@ pub struct ToDeviceEvent> { /// The decrypted payload of an `m.olm.v1.curve25519-aes-sha2` event. #[derive(Clone, Debug, Event)] -pub struct DecryptedOlmV1Event> { +pub struct DecryptedOlmV1Event { /// Data specific to the event type. pub content: C, @@ -329,7 +324,7 @@ pub struct OlmV1Keys { /// The decrypted payload of an `m.megolm.v1.aes-sha2` event. #[derive(Clone, Debug, Event)] -pub struct DecryptedMegolmV1Event> { +pub struct DecryptedMegolmV1Event { /// Data specific to the event type. pub content: C, diff --git a/crates/ruma-common/src/events/unsigned.rs b/crates/ruma-common/src/events/unsigned.rs index b0d2131a..3b6df904 100644 --- a/crates/ruma-common/src/events/unsigned.rs +++ b/crates/ruma-common/src/events/unsigned.rs @@ -4,7 +4,7 @@ use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue}; #[cfg(feature = "unstable-msc2675")] use super::relation::Relations; -use super::{room::redaction::SyncRoomRedactionEvent, EventContent, StateEventType}; +use super::{room::redaction::SyncRoomRedactionEvent, StateEventContent}; use crate::{serde::Raw, TransactionId}; /// Extra information about a message event that is not incorporated into the event's hash. @@ -57,7 +57,7 @@ impl MessageLikeUnsigned { /// Extra information about a state event that is not incorporated into the event's hash. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct StateUnsigned> { +pub struct StateUnsigned { /// The time in milliseconds that has elapsed since the event was sent. /// /// This field is generated by the local homeserver, and may be incorrect if the local time on @@ -81,7 +81,7 @@ pub struct StateUnsigned> { pub relations: Option, } -impl> StateUnsigned { +impl StateUnsigned { /// Create a new `Unsigned` with fields set to `None`. pub fn new() -> Self { Self { @@ -118,7 +118,7 @@ impl> StateUnsigned { /// /// Needs to be public for UI tests. #[doc(hidden)] -impl> StateUnsigned { +impl StateUnsigned { pub fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result { #[derive(Deserialize)] #[serde(bound = "")] // Disable default C: Deserialize bound @@ -148,7 +148,7 @@ impl> StateUnsigned { pub fn _map_prev_unsigned(&self, f: impl FnOnce(&C) -> T) -> StateUnsigned where - T: EventContent, + T: StateEventContent, { StateUnsigned { age: self.age, @@ -160,7 +160,7 @@ impl> StateUnsigned { } } -impl> Default for StateUnsigned { +impl Default for StateUnsigned { fn default() -> Self { Self::new() } diff --git a/crates/ruma-common/tests/events/ui/04-event-sanity-check.rs b/crates/ruma-common/tests/events/ui/04-event-sanity-check.rs index c5c63bf1..fab82618 100644 --- a/crates/ruma-common/tests/events/ui/04-event-sanity-check.rs +++ b/crates/ruma-common/tests/events/ui/04-event-sanity-check.rs @@ -4,14 +4,14 @@ extern crate serde; use ruma_common::{ - events::{EventContent, StateEventType, StateUnsigned}, + events::{StateEventContent, StateUnsigned}, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId, }; use ruma_macros::Event; /// State event. #[derive(Clone, Debug, Event)] -pub struct StateEvent> { +pub struct StateEvent { pub content: C, pub event_id: Box, pub sender: Box, diff --git a/crates/ruma-common/tests/events/ui/05-named-fields.rs b/crates/ruma-common/tests/events/ui/05-named-fields.rs index 444c917f..9fb1defb 100644 --- a/crates/ruma-common/tests/events/ui/05-named-fields.rs +++ b/crates/ruma-common/tests/events/ui/05-named-fields.rs @@ -1,8 +1,8 @@ -use ruma_common::events::{EventContent, StateEventType}; +use ruma_common::events::StateEventContent; use ruma_macros::Event; /// State event. #[derive(Clone, Debug, Event)] -pub struct StateEvent>(C); +pub struct StateEvent(C); fn main() {} diff --git a/crates/ruma-common/tests/events/ui/05-named-fields.stderr b/crates/ruma-common/tests/events/ui/05-named-fields.stderr index 3517fa62..9f701944 100644 --- a/crates/ruma-common/tests/events/ui/05-named-fields.stderr +++ b/crates/ruma-common/tests/events/ui/05-named-fields.stderr @@ -1,5 +1,5 @@ error: the `Event` derive only supports structs with named fields --> tests/events/ui/05-named-fields.rs:6:12 | -6 | pub struct StateEvent>(C); +6 | pub struct StateEvent(C); | ^^^^^^^^^^ diff --git a/crates/ruma-common/tests/events/ui/06-no-content-field.rs b/crates/ruma-common/tests/events/ui/06-no-content-field.rs index 32a4d807..ecab017f 100644 --- a/crates/ruma-common/tests/events/ui/06-no-content-field.rs +++ b/crates/ruma-common/tests/events/ui/06-no-content-field.rs @@ -1,9 +1,9 @@ -use ruma_common::events::{EventContent, StateEventType}; +use ruma_common::events::StateEventContent; use ruma_macros::Event; /// State event. #[derive(Clone, Debug, Event)] -pub struct StateEvent> { +pub struct StateEvent { pub not_content: C, } diff --git a/crates/ruma-common/tests/events/ui/06-no-content-field.stderr b/crates/ruma-common/tests/events/ui/06-no-content-field.stderr index ff7d8ee6..fd067d02 100644 --- a/crates/ruma-common/tests/events/ui/06-no-content-field.stderr +++ b/crates/ruma-common/tests/events/ui/06-no-content-field.stderr @@ -1,5 +1,5 @@ error: struct must contain a `content` field - --> $DIR/06-no-content-field.rs:5:24 + --> tests/events/ui/06-no-content-field.rs:5:24 | 5 | #[derive(Clone, Debug, Event)] | ^^^^^