From 316d1423e821e89da51c0885cfad27cf446d2a02 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 21 Mar 2022 19:09:02 +0100 Subject: [PATCH] events: Remove AsRef requirement on EventContent::EventType MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and use Display instead of AsRef for Raw::deserialize_content. --- crates/ruma-common/src/events.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/ruma-common/src/events.rs b/crates/ruma-common/src/events.rs index 75e2b141..f70b2826 100644 --- a/crates/ruma-common/src/events.rs +++ b/crates/ruma-common/src/events.rs @@ -126,6 +126,8 @@ //! type alias), allowing content to be converted to and from JSON independently of the surrounding //! event structure, if needed. +use std::fmt; + use serde::{de::IgnoredAny, Deserialize, Serialize, Serializer}; use serde_json::value::RawValue as RawJsonValue; @@ -201,7 +203,7 @@ pub use self::{ /// Use [`macros::EventContent`] to derive this traits. It is not meant to be implemented manually. pub trait EventContent: Sized + Serialize { /// The Rust enum for the event kind's known types. - type EventType: AsRef; + type EventType; /// Get the event's type, like `m.room.message`. fn event_type(&self) -> Self::EventType; @@ -237,10 +239,14 @@ pub trait RedactContent { fn redact(self, version: &RoomVersionId) -> Self::Redacted; } -impl Raw { +impl Raw +where + T: EventContent, + T::EventType: fmt::Display, +{ /// Try to deserialize the JSON as an event's content. pub fn deserialize_content(&self, event_type: T::EventType) -> serde_json::Result { - T::from_parts(event_type.as_ref(), self.json()) + T::from_parts(&event_type.to_string(), self.json()) } }