From af19935167f4b8566c8850f1a67bf5ac1b5ea2c8 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 3 Aug 2021 16:17:10 +0100 Subject: [PATCH] Attempted specialisation. Failed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rsh's current commit: Great curse − 大凶 --- src/main.rs | 3 +++ src/message.rs | 9 +++------ src/message/value.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/message/value.rs diff --git a/src/main.rs b/src/main.rs index fe60f0a..3d0c104 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ //! Remote communication #![cfg_attr(nightly, feature(const_fn_trait_bound))] +#![cfg_attr(nightly, feature(specialization))] + #![allow(dead_code)] +#[macro_use] extern crate mopa; #[macro_use] extern crate serde; #[macro_use] extern crate pin_project; diff --git a/src/message.rs b/src/message.rs index a2b5f52..5c3f400 100644 --- a/src/message.rs +++ b/src/message.rs @@ -25,18 +25,15 @@ pub use serial::*; mod builder; pub use builder::*; +pub mod value; +pub use value::MessageValue; + /// Size of encrypted AES key pub const RSA_BLOCK_SIZE: usize = 512; /// Max size to pre-allocate when reading a message buffer. pub const MAX_ALLOC_SIZE: usize = 4096; // 4kb -/// 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>{} - /// A message that can send a value into bytes. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Message diff --git a/src/message/value.rs b/src/message/value.rs new file mode 100644 index 0000000..74fb189 --- /dev/null +++ b/src/message/value.rs @@ -0,0 +1,32 @@ +//! Message values +use super::*; +use std::any::Any; +use serde::de::DeserializeOwned; + +pub struct MessageValueAnyRef<'a>(&'a (dyn Any +'static)); +pub struct MessageValueAnyMut<'a>(&'a mut (dyn Any +'static)); +pub struct MessageValueAny(Box); + +/// A value that can be used for messages. +pub trait MessageValue: Serialize + for<'de> Deserialize<'de> +{ + fn as_dynamic(&self) -> Option> { None } + fn as_dynamic_mut(&mut self) -> Option> { None } + fn into_dynamic(self) -> Result { Err(self) } +} + +default impl MessageValue for T +where T: Serialize + for<'de> Deserialize<'de> +{ + default fn as_dynamic(&self) -> Option> { None } + default fn as_dynamic_mut(&mut self) -> Option> { None } + default fn into_dynamic(self) -> Result { Err(self) } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct DynamicMessageValue(T) +where T: Any + 'static; + +//impl MessageValue for DynamicMessageValue +//where T: Serialize + for<'de> Deserialize<'de> + Any{} +