You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.3 KiB
67 lines
2.3 KiB
//! 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<T: ?Sized> 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>(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<dyn Any +'static>);
|
|
|
|
//impl<T> MessageValue for DynamicMessageValue<T>
|
|
//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<V>` with `UntypedSerializedMessage::into_typed<V>()`.
|
|
///
|
|
/// This is an empty (!) type.
|
|
#[derive(Debug)]
|
|
pub enum UntypedMessageValue{}
|
|
|
|
impl SerializedMessage<UntypedMessageValue>
|
|
{
|
|
/// 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<V: MessageValue + ?Sized>(self) -> SerializedMessage<V>
|
|
{
|
|
mem::transmute(self)
|
|
}
|
|
}
|
|
|
|
impl<V: MessageValue + ?Sized> SerializedMessage<V>
|
|
{
|
|
/// 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<UntypedMessageValue>
|
|
{
|
|
unsafe {
|
|
mem::transmute(self)
|
|
}
|
|
}
|
|
}
|