From 1b4234c5057730d587e9cde5517c6710f78fece0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 12 Feb 2022 01:20:00 +0100 Subject: [PATCH] serde: Add Raw::{cast, cast_ref} --- crates/ruma-serde/src/raw.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ruma-serde/src/raw.rs b/crates/ruma-serde/src/raw.rs index 42e94617..93ebe95c 100644 --- a/crates/ruma-serde/src/raw.rs +++ b/crates/ruma-serde/src/raw.rs @@ -2,6 +2,7 @@ use std::{ clone::Clone, fmt::{self, Debug, Formatter}, marker::PhantomData, + mem, }; use serde::{ @@ -35,6 +36,7 @@ use crate::cow::MyCowStr; /// .deserialize() // deserialize to the inner type /// .unwrap(); // finally get to the AnyRoomEvent /// ``` +#[repr(transparent)] pub struct Raw { json: Box, _ev: PhantomData, @@ -152,6 +154,20 @@ impl Raw { { serde_json::from_str(self.json.get()) } + + /// Turns `Raw` into `Raw` without changing the underlying JSON. + /// + /// This is useful for turning raw specific event types into raw event enum types. + pub fn cast(self) -> Raw { + Raw::from_json(self.into_json()) + } + + /// Turns `&Raw` into `&Raw` without changing the underlying JSON. + /// + /// This is useful for turning raw specific event types into raw event enum types. + pub fn cast_ref(&self) -> &Raw { + unsafe { mem::transmute(self) } + } } impl Clone for Raw {