//! Message values use super::*; use std::mem; use serde::{Serialize, Deserialize}; /// A value that can be used for messages. pub trait MessageValue: Serialize + for<'de> Deserialize<'de>{} impl MessageValue for T where T: Serialize + for<'de> Deserialize<'de>{} /* use std::any::Any; use serde::de::DeserializeOwned; #[derive(Debug, Serialize, Deserialize)] pub struct DynamicMessageValue(T) where T: Any + 'static; pub struct MessageValueAnyRef<'a>(&'a (dyn Any +'static)); pub struct MessageValueAnyMut<'a>(&'a mut (dyn Any +'static)); pub struct MessageValueAny(Box); //impl MessageValue for DynamicMessageValue //where T: Serialize + for<'de> Deserialize<'de> + Any{} */ /// A type-unsafe value that can be used to transmute `SerializedMessage` instances. /// /// This operation is unsafe and can result in deserializing the `SerializedMessage` failing, or even worse, a type confusion. /// /// This type does not implement `MessageValue`, as serialized messages of this value cannot be created (from `Message::serialize()`), nor deserialized. They must first be converted into a typed `SerializedMessage` with `UntypedSerializedMessage::into_typed()`. /// /// This is an empty (!) type. #[derive(Debug)] pub enum UntypedMessageValue{} impl SerializedMessage { /// Transmute into a specifically typed `SerializedMessage` /// /// # Safety /// If `V` is not the original type of this message (before being untyped), then deserialisation will likely fail, or, much worse, cause a *type consufion* bug, where the object of type `V` is successfully deserialized with an invalid value from an unknown type. /// Take special care when using this. pub const unsafe fn into_typed(self) -> SerializedMessage { mem::transmute(self) } } impl SerializedMessage { /// Consume this value into an untyped `SerializedMessage`. /// /// # Safety /// This operation is safe, however, doing anything at all with the resulting `SerializedMessage` is not. /// It must be unsafly converted into a types message before it can be deserialized. pub const fn into_untyped(self) -> SerializedMessage { unsafe { mem::transmute(self) } } }