Attempted specialisation. Failed.

Fortune for rsh's current commit: Great curse − 大凶
specialisation
Avril 3 years ago
parent ab97278dbd
commit af19935167
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,8 +1,11 @@
//! Remote communication //! Remote communication
#![cfg_attr(nightly, feature(const_fn_trait_bound))] #![cfg_attr(nightly, feature(const_fn_trait_bound))]
#![cfg_attr(nightly, feature(specialization))]
#![allow(dead_code)] #![allow(dead_code)]
#[macro_use] extern crate mopa;
#[macro_use] extern crate serde; #[macro_use] extern crate serde;
#[macro_use] extern crate pin_project; #[macro_use] extern crate pin_project;

@ -25,18 +25,15 @@ pub use serial::*;
mod builder; mod builder;
pub use builder::*; pub use builder::*;
pub mod value;
pub use value::MessageValue;
/// Size of encrypted AES key /// Size of encrypted AES key
pub const RSA_BLOCK_SIZE: usize = 512; pub const RSA_BLOCK_SIZE: usize = 512;
/// Max size to pre-allocate when reading a message buffer. /// Max size to pre-allocate when reading a message buffer.
pub const MAX_ALLOC_SIZE: usize = 4096; // 4kb 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<T: ?Sized> MessageValue for T
where T: Serialize + for<'de> Deserialize<'de>{}
/// A message that can send a value into bytes. /// A message that can send a value into bytes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Message<V: ?Sized + MessageValue> pub struct Message<V: ?Sized + MessageValue>

@ -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<dyn Any +'static>);
/// A value that can be used for messages.
pub trait MessageValue: Serialize + for<'de> Deserialize<'de>
{
fn as_dynamic(&self) -> Option<MessageValueAnyRef<'_>> { None }
fn as_dynamic_mut(&mut self) -> Option<MessageValueAnyMut<'_>> { None }
fn into_dynamic(self) -> Result<MessageValueAny, Self> { Err(self) }
}
default impl<T: ?Sized> MessageValue for T
where T: Serialize + for<'de> Deserialize<'de>
{
default fn as_dynamic(&self) -> Option<MessageValueAnyRef<'_>> { None }
default fn as_dynamic_mut(&mut self) -> Option<MessageValueAnyMut<'_>> { None }
default fn into_dynamic(self) -> Result<MessageValueAny, Self> { Err(self) }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DynamicMessageValue<T>(T)
where T: Any + 'static;
//impl<T> MessageValue for DynamicMessageValue<T>
//where T: Serialize + for<'de> Deserialize<'de> + Any{}
Loading…
Cancel
Save