@ -5,16 +5,26 @@ use std::io::Read;
#[ cfg(feature = " serialize " ) ]
#[ cfg(feature = " serialize " ) ]
pub use super ::payload_serializer ::* ;
pub use super ::payload_serializer ::* ;
/// Trait that serializes a type into bytes and can fail
pub trait TryIntoBytes {
fn try_into_bytes ( self ) -> IPCResult < Vec < u8 > > ;
}
/// Trait that serializes a type into bytes and never fails
pub trait IntoBytes {
fn into_bytes ( self ) -> Vec < u8 > ;
}
/// Trait to convert event data into sending bytes
/// Trait to convert event data into sending bytes
/// It is implemented for all types that implement Serialize
/// It is implemented for all types that implement Serialize
pub trait EventSendPayload {
pub trait Into Payload {
fn to_payload_bytes ( self ) -> IPCResult < Vec < u8 > > ;
fn in to_payload( self , ctx : & Context ) -> IPCResult < Vec < u8 > > ;
}
}
/// Trait to get the event data from receiving bytes.
/// Trait to get the event data from receiving bytes.
/// It is implemented for all types that are DeserializeOwned
/// It is implemented for all types that are DeserializeOwned
pub trait EventReceivePayload : Sized {
pub trait From Payload: Sized {
fn from_payload_bytes < R : Read > ( reader : R ) -> IPCResult < Self > ;
fn from_payload < R : Read > ( reader : R ) -> IPCResult < Self > ;
}
}
/// A payload wrapper type for sending bytes directly without
/// A payload wrapper type for sending bytes directly without
@ -35,14 +45,14 @@ impl BytePayload {
}
}
}
}
impl EventSend Payload for BytePayload {
impl Into Payload for BytePayload {
fn to_payload_bytes ( self ) -> IPCResult < Vec < u8 > > {
fn in to_payload( self , _ : & Context ) -> IPCResult < Vec < u8 > > {
Ok ( self . bytes )
Ok ( self . bytes )
}
}
}
}
impl EventReceive Payload for BytePayload {
impl From Payload for BytePayload {
fn from_payload _bytes < R : Read > ( mut reader : R ) -> IPCResult < Self > {
fn from_payload < R : Read > ( mut reader : R ) -> IPCResult < Self > {
let mut buf = Vec ::new ( ) ;
let mut buf = Vec ::new ( ) ;
reader . read_to_end ( & mut buf ) ? ;
reader . read_to_end ( & mut buf ) ? ;
@ -70,14 +80,10 @@ impl<P1, P2> TandemPayload<P1, P2> {
}
}
}
}
impl < P1 , P2 > EventSendPayload for TandemPayload < P1 , P2 >
impl < P1 : IntoPayload , P2 : IntoPayload > IntoPayload for TandemPayload < P1 , P2 > {
where
fn into_payload ( self , ctx : & Context ) -> IPCResult < Vec < u8 > > {
P1 : EventSendPayload ,
let mut p1_bytes = self . load1 . into_payload ( & ctx ) ? ;
P2 : EventSendPayload ,
let mut p2_bytes = self . load2 . into_payload ( & ctx ) ? ;
{
fn to_payload_bytes ( self ) -> IPCResult < Vec < u8 > > {
let mut p1_bytes = self . load1 . to_payload_bytes ( ) ? ;
let mut p2_bytes = self . load2 . to_payload_bytes ( ) ? ;
let mut p1_length_bytes = ( p1_bytes . len ( ) as u64 ) . to_be_bytes ( ) . to_vec ( ) ;
let mut p1_length_bytes = ( p1_bytes . len ( ) as u64 ) . to_be_bytes ( ) . to_vec ( ) ;
let mut p2_length_bytes = ( p2_bytes . len ( ) as u64 ) . to_be_bytes ( ) . to_vec ( ) ;
let mut p2_length_bytes = ( p2_bytes . len ( ) as u64 ) . to_be_bytes ( ) . to_vec ( ) ;
@ -92,12 +98,8 @@ where
}
}
}
}
impl < P1 , P2 > EventReceivePayload for TandemPayload < P1 , P2 >
impl < P1 : FromPayload , P2 : FromPayload > FromPayload for TandemPayload < P1 , P2 > {
where
fn from_payload < R : Read > ( mut reader : R ) -> IPCResult < Self > {
P1 : EventReceivePayload ,
P2 : EventReceivePayload ,
{
fn from_payload_bytes < R : Read > ( mut reader : R ) -> IPCResult < Self > {
let p1_length = reader . read_u64 ::< BigEndian > ( ) ? ;
let p1_length = reader . read_u64 ::< BigEndian > ( ) ? ;
let mut load1_bytes = vec! [ 0 u8 ; p1_length as usize ] ;
let mut load1_bytes = vec! [ 0 u8 ; p1_length as usize ] ;
reader . read_exact ( & mut load1_bytes ) ? ;
reader . read_exact ( & mut load1_bytes ) ? ;
@ -107,14 +109,15 @@ where
reader . read_exact ( & mut load2_bytes ) ? ;
reader . read_exact ( & mut load2_bytes ) ? ;
Ok ( Self {
Ok ( Self {
load1 : P1 ::from_payload _bytes ( load1_bytes . as_slice ( ) ) ? ,
load1 : P1 ::from_payload ( load1_bytes . as_slice ( ) ) ? ,
load2 : P2 ::from_payload _bytes ( load2_bytes . as_slice ( ) ) ? ,
load2 : P2 ::from_payload ( load2_bytes . as_slice ( ) ) ? ,
} )
} )
}
}
}
}
impl EventSendPayload for ( ) {
#[ cfg(not(feature = " serialize " )) ]
fn to_payload_bytes ( self ) -> IPCResult < Vec < u8 > > {
impl IntoPayload for ( ) {
fn into_payload ( self , _ : & Context ) -> IPCResult < Vec < u8 > > {
Ok ( vec! [ ] )
Ok ( vec! [ ] )
}
}
}
}
@ -123,8 +126,8 @@ impl EventSendPayload for () {
mod serde_payload {
mod serde_payload {
use super ::DynamicSerializer ;
use super ::DynamicSerializer ;
use crate ::context ::Context ;
use crate ::context ::Context ;
use crate ::payload ::EventReceivePayload ;
use crate ::payload ::{ FromPayload , TryIntoBytes } ;
use crate ::prelude ::{ EventSendPayload, IPCResult } ;
use crate ::prelude ::{ IPCResult, IntoPayload } ;
use byteorder ::ReadBytesExt ;
use byteorder ::ReadBytesExt ;
use serde ::de ::DeserializeOwned ;
use serde ::de ::DeserializeOwned ;
use serde ::Serialize ;
use serde ::Serialize ;
@ -147,10 +150,7 @@ mod serde_payload {
}
}
}
}
impl < T > Clone for SerdePayload < T >
impl < T : Clone > Clone for SerdePayload < T > {
where
T : Clone ,
{
fn clone ( & self ) -> Self {
fn clone ( & self ) -> Self {
Self {
Self {
serializer : self . serializer . clone ( ) ,
serializer : self . serializer . clone ( ) ,
@ -159,11 +159,8 @@ mod serde_payload {
}
}
}
}
impl < T > EventSendPayload for SerdePayload < T >
impl < T : Serialize > TryIntoBytes for SerdePayload < T > {
where
fn try_into_bytes ( self ) -> IPCResult < Vec < u8 > > {
T : Serialize ,
{
fn to_payload_bytes ( self ) -> IPCResult < Vec < u8 > > {
let mut buf = Vec ::new ( ) ;
let mut buf = Vec ::new ( ) ;
let mut data_bytes = self . serializer . serialize ( self . data ) ? ;
let mut data_bytes = self . serializer . serialize ( self . data ) ? ;
let format_id = self . serializer as u8 ;
let format_id = self . serializer as u8 ;
@ -174,11 +171,14 @@ mod serde_payload {
}
}
}
}
impl < T > EventReceivePayload for SerdePayload < T >
impl < T : Serialize > IntoPayload for SerdePayload < T > {
where
fn into_payload ( self , _ : & Context ) -> IPCResult < Vec < u8 > > {
T : DeserializeOwned ,
self . try_into_bytes ( )
{
}
fn from_payload_bytes < R : Read > ( mut reader : R ) -> IPCResult < Self > {
}
impl < T : DeserializeOwned > FromPayload for SerdePayload < T > {
fn from_payload < R : Read > ( mut reader : R ) -> IPCResult < Self > {
let format_id = reader . read_u8 ( ) ? ;
let format_id = reader . read_u8 ( ) ? ;
let serializer = DynamicSerializer ::from_primitive ( format_id as usize ) ? ;
let serializer = DynamicSerializer ::from_primitive ( format_id as usize ) ? ;
let data = serializer . deserialize ( reader ) ? ;
let data = serializer . deserialize ( reader ) ? ;
@ -187,14 +187,13 @@ mod serde_payload {
}
}
}
}
pub trait IntoSerdePayload : Sized {
impl< T : Serialize > IntoPayload for T {
fn into_ serde_ payload( self , ctx : & Context ) -> SerdePayload< Self > {
fn into_ payload( self , ctx : & Context ) -> IPCResult< Vec < u8 > > {
ctx . create_serde_payload ( self )
ctx . create_serde_payload ( self ) . into_payload ( & ctx )
}
}
}
}
impl < T > IntoSerdePayload for T where T : Serialize { }
}
}
use crate ::context ::Context ;
#[ cfg(feature = " serialize " ) ]
#[ cfg(feature = " serialize " ) ]
pub use serde_payload ::* ;
pub use serde_payload ::* ;