events: Use PrivOwnedStr for #[doc(hidden)] event_type field

This commit is contained in:
Jonas Platte 2022-01-29 01:31:18 +01:00
parent b8396b9cc0
commit 38ba38228a
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 15 additions and 7 deletions

View File

@ -320,7 +320,7 @@ fn expand_content_enum(
#[doc(hidden)] #[doc(hidden)]
#[serde(serialize_with = #serialize_custom_event_error_path)] #[serde(serialize_with = #serialize_custom_event_error_path)]
_Custom { _Custom {
event_type: ::std::string::String, event_type: crate::PrivOwnedStr,
}, },
} }
@ -329,7 +329,7 @@ fn expand_content_enum(
fn event_type(&self) -> &::std::primitive::str { fn event_type(&self) -> &::std::primitive::str {
match self { match self {
#( #variant_arms(content) => content.event_type(), )* #( #variant_arms(content) => content.event_type(), )*
Self::_Custom { event_type } => &event_type, Self::_Custom { event_type } => &event_type.0,
} }
} }
@ -345,7 +345,9 @@ fn expand_content_enum(
} }
)* )*
ty => { ty => {
::std::result::Result::Ok(Self::_Custom { event_type: ty.to_owned() }) ::std::result::Result::Ok(Self::_Custom {
event_type: crate::PrivOwnedStr(ty.into()),
})
} }
} }
} }
@ -483,7 +485,9 @@ fn expand_accessor_methods(
)* )*
Self::_Custom(event) => { Self::_Custom(event) => {
event.prev_content.as_ref().map(|c| #content_enum::_Custom { event.prev_content.as_ref().map(|c| #content_enum::_Custom {
event_type: #ruma_events::EventContent::event_type(c).to_owned(), event_type: crate::PrivOwnedStr(
#ruma_events::EventContent::event_type(c).into(),
),
}) })
}, },
} }
@ -497,8 +501,9 @@ fn expand_accessor_methods(
match self { match self {
#( #self_variants(event) => #content_variants(event.content.clone()), )* #( #self_variants(event) => #content_variants(event.content.clone()), )*
Self::_Custom(event) => #content_enum::_Custom { Self::_Custom(event) => #content_enum::_Custom {
event_type: #ruma_events::EventContent::event_type(&event.content) event_type: crate::PrivOwnedStr(
.to_owned(), #ruma_events::EventContent::event_type(&event.content).into(),
),
}, },
} }
} }

View File

@ -411,7 +411,10 @@ pub struct PrivOwnedStr(Box<str>);
/// only be created by deserializing from an unknown event type. /// only be created by deserializing from an unknown event type.
#[doc(hidden)] #[doc(hidden)]
#[allow(clippy::ptr_arg)] #[allow(clippy::ptr_arg)]
pub fn serialize_custom_event_error<S: Serializer>(_: &String, _: S) -> Result<S::Ok, S::Error> { pub fn serialize_custom_event_error<S: Serializer>(
_: &PrivOwnedStr,
_: S,
) -> Result<S::Ok, S::Error> {
Err(serde::ser::Error::custom( Err(serde::ser::Error::custom(
"Failed to serialize event [content] enum: Unknown event type.\n\ "Failed to serialize event [content] enum: Unknown event type.\n\
To send custom events, turn them into `Raw<EnumType>` by going through To send custom events, turn them into `Raw<EnumType>` by going through