events: Reintroduce *EventContent as trait aliases

This commit is contained in:
Jonas Platte 2022-03-30 16:56:55 +02:00
parent 9f56e6e497
commit 5ec3ad2290
No known key found for this signature in database
GPG Key ID: BBA95679259D342F
12 changed files with 96 additions and 48 deletions

View File

@ -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<T>(data: &'a T, user_id: &'a UserId) -> serde_json::Result<Self>
where
T: EventContent<EventType = GlobalAccountDataEventType>,
T: GlobalAccountDataEventContent,
{
Ok(Self {
user_id,

View File

@ -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<Self>
where
T: EventContent<EventType = RoomAccountDataEventType>,
T: RoomAccountDataEventContent,
{
Ok(Self {
data: Raw::from_json(to_raw_json_value(data)?),

View File

@ -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<Self>
where
T: EventContent<EventType = MessageLikeEventType>,
T: MessageLikeEventContent,
{
Ok(Self {
room_id,

View File

@ -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<Self>
where
T: EventContent<EventType = StateEventType>,
T: StateEventContent,
{
Ok(Self {
room_id,

View File

@ -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<T: $($def+)+> $id for T {}
)*
}
}
trait_aliases! {
/// An alias for `EventContent<EventType = GlobalAccountDataEventType>`.
trait GlobalAccountDataEventContent = EventContent<EventType = GlobalAccountDataEventType>;
/// An alias for `EventContent<EventType = RoomAccountDataEventType>`.
trait RoomAccountDataEventContent = EventContent<EventType = RoomAccountDataEventType>;
/// An alias for `EventContent<EventType = EphemeralRoomEventType>`.
trait EphemeralRoomEventContent = EventContent<EventType = EphemeralRoomEventType>;
/// An alias for `EventContent<EventType = MessageLikeEventType>`.
trait MessageLikeEventContent = EventContent<EventType = MessageLikeEventType>;
/// An alias for `EventContent<EventType = MessageLikeEventType> + RedactedEventContent`.
trait RedactedMessageLikeEventContent =
EventContent<EventType = MessageLikeEventType>, RedactedEventContent;
/// An alias for `EventContent<EventType = StateEventType>`.
trait StateEventContent = EventContent<EventType = StateEventType>;
/// An alias for `EventContent<EventType = StateEventType> + RedactedEventContent`.
trait RedactedStateEventContent =
EventContent<EventType = StateEventType>, RedactedEventContent;
/// An alias for `EventContent<EventType = ToDeviceEventType>`.
trait ToDeviceEventContent = EventContent<EventType = ToDeviceEventType>;
}

View File

@ -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<C: EventContent<EventType = GlobalAccountDataEventType>> {
pub struct GlobalAccountDataEvent<C: GlobalAccountDataEventContent> {
/// Data specific to the event type.
pub content: C,
}
/// A room account data event.
#[derive(Clone, Debug, Event)]
pub struct RoomAccountDataEvent<C: EventContent<EventType = RoomAccountDataEventType>> {
pub struct RoomAccountDataEvent<C: RoomAccountDataEventContent> {
/// Data specific to the event type.
pub content: C,
}
/// An ephemeral room event.
#[derive(Clone, Debug, Event)]
pub struct EphemeralRoomEvent<C: EventContent<EventType = EphemeralRoomEventType>> {
pub struct EphemeralRoomEvent<C: EphemeralRoomEventContent> {
/// Data specific to the event type.
pub content: C,
@ -36,7 +37,7 @@ pub struct EphemeralRoomEvent<C: EventContent<EventType = EphemeralRoomEventType
/// An ephemeral room event without a `room_id`.
#[derive(Clone, Debug, Event)]
pub struct SyncEphemeralRoomEvent<C: EventContent<EventType = EphemeralRoomEventType>> {
pub struct SyncEphemeralRoomEvent<C: EphemeralRoomEventContent> {
/// Data specific to the event type.
pub content: C,
}
@ -46,7 +47,7 @@ pub struct SyncEphemeralRoomEvent<C: EventContent<EventType = EphemeralRoomEvent
/// `MessageLikeEvent` 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 MessageLikeEvent<C: EventContent<EventType = MessageLikeEventType>> {
pub struct MessageLikeEvent<C: MessageLikeEventContent> {
/// Data specific to the event type.
pub content: C,
@ -71,7 +72,7 @@ pub struct MessageLikeEvent<C: EventContent<EventType = MessageLikeEventType>> {
/// `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<C: EventContent<EventType = MessageLikeEventType>> {
pub struct SyncMessageLikeEvent<C: MessageLikeEventContent> {
/// Data specific to the event type.
pub content: C,
@ -93,9 +94,7 @@ pub struct SyncMessageLikeEvent<C: EventContent<EventType = MessageLikeEventType
/// `RedactedMessageLikeEvent` 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 RedactedMessageLikeEvent<
C: EventContent<EventType = MessageLikeEventType> + RedactedEventContent,
> {
pub struct RedactedMessageLikeEvent<C: RedactedMessageLikeEventContent> {
/// 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<EventType = MessageLikeEventType> + RedactedEventContent,
> {
pub struct RedactedSyncMessageLikeEvent<C: RedactedMessageLikeEventContent> {
/// 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<C: EventContent<EventType = StateEventType>> {
pub struct StateEvent<C: StateEventContent> {
/// Data specific to the event type.
pub content: C,
@ -175,7 +172,7 @@ pub struct StateEvent<C: EventContent<EventType = StateEventType>> {
/// `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<C: EventContent<EventType = StateEventType>> {
pub struct SyncStateEvent<C: StateEventContent> {
/// Data specific to the event type.
pub content: C,
@ -200,7 +197,7 @@ pub struct SyncStateEvent<C: EventContent<EventType = StateEventType>> {
/// A stripped-down state event, used for previews of rooms the user has been invited to.
#[derive(Clone, Debug, Event)]
pub struct StrippedStateEvent<C: EventContent<EventType = StateEventType>> {
pub struct StrippedStateEvent<C: StateEventContent> {
/// Data specific to the event type.
pub content: C,
@ -216,7 +213,7 @@ pub struct StrippedStateEvent<C: EventContent<EventType = StateEventType>> {
/// A minimal state event, used for creating a new room.
#[derive(Clone, Debug, Event)]
pub struct InitialStateEvent<C: EventContent<EventType = StateEventType>> {
pub struct InitialStateEvent<C: StateEventContent> {
/// Data specific to the event type.
pub content: C,
@ -235,7 +232,7 @@ pub struct InitialStateEvent<C: EventContent<EventType = StateEventType>> {
/// `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<C: EventContent<EventType = StateEventType> + RedactedEventContent> {
pub struct RedactedStateEvent<C: RedactedStateEventContent> {
/// Data specific to the event type.
pub content: C,
@ -266,9 +263,7 @@ pub struct RedactedStateEvent<C: EventContent<EventType = StateEventType> + 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<EventType = StateEventType> + RedactedEventContent,
> {
pub struct RedactedSyncStateEvent<C: RedactedStateEventContent> {
/// 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<C: EventContent<EventType = ToDeviceEventType>> {
pub struct ToDeviceEvent<C: ToDeviceEventContent> {
/// Data specific to the event type.
pub content: C,
@ -303,7 +298,7 @@ pub struct ToDeviceEvent<C: EventContent<EventType = ToDeviceEventType>> {
/// The decrypted payload of an `m.olm.v1.curve25519-aes-sha2` event.
#[derive(Clone, Debug, Event)]
pub struct DecryptedOlmV1Event<C: EventContent<EventType = MessageLikeEventType>> {
pub struct DecryptedOlmV1Event<C: MessageLikeEventContent> {
/// 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<C: EventContent<EventType = MessageLikeEventType>> {
pub struct DecryptedMegolmV1Event<C: MessageLikeEventContent> {
/// Data specific to the event type.
pub content: C,

View File

@ -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<C: EventContent<EventType = StateEventType>> {
pub struct StateUnsigned<C: StateEventContent> {
/// 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<C: EventContent<EventType = StateEventType>> {
pub relations: Option<Relations>,
}
impl<C: EventContent<EventType = StateEventType>> StateUnsigned<C> {
impl<C: StateEventContent> StateUnsigned<C> {
/// Create a new `Unsigned` with fields set to `None`.
pub fn new() -> Self {
Self {
@ -118,7 +118,7 @@ impl<C: EventContent<EventType = StateEventType>> StateUnsigned<C> {
///
/// Needs to be public for UI tests.
#[doc(hidden)]
impl<C: EventContent<EventType = StateEventType>> StateUnsigned<C> {
impl<C: StateEventContent> StateUnsigned<C> {
pub fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result<Self> {
#[derive(Deserialize)]
#[serde(bound = "")] // Disable default C: Deserialize bound
@ -148,7 +148,7 @@ impl<C: EventContent<EventType = StateEventType>> StateUnsigned<C> {
pub fn _map_prev_unsigned<T>(&self, f: impl FnOnce(&C) -> T) -> StateUnsigned<T>
where
T: EventContent<EventType = StateEventType>,
T: StateEventContent,
{
StateUnsigned {
age: self.age,
@ -160,7 +160,7 @@ impl<C: EventContent<EventType = StateEventType>> StateUnsigned<C> {
}
}
impl<C: EventContent<EventType = StateEventType>> Default for StateUnsigned<C> {
impl<C: StateEventContent> Default for StateUnsigned<C> {
fn default() -> Self {
Self::new()
}

View File

@ -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<C: EventContent<EventType = StateEventType>> {
pub struct StateEvent<C: StateEventContent> {
pub content: C,
pub event_id: Box<EventId>,
pub sender: Box<UserId>,

View File

@ -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: EventContent<EventType = StateEventType>>(C);
pub struct StateEvent<C: StateEventContent>(C);
fn main() {}

View File

@ -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: EventContent<EventType = StateEventType>>(C);
6 | pub struct StateEvent<C: StateEventContent>(C);
| ^^^^^^^^^^

View File

@ -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<C: EventContent<EventType = StateEventType>> {
pub struct StateEvent<C: StateEventContent> {
pub not_content: C,
}

View File

@ -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)]
| ^^^^^