meio_protocol/lib.rs
1//! The generic protocol types.
2
3#![warn(missing_docs)]
4
5use anyhow::Error;
6use serde::{de::DeserializeOwned, Serialize};
7use std::fmt::Debug;
8
9/// Generic procotol type.
10///
11/// The trait gives the following guarantees to protocol data type:
12///
13/// - Data can be serialized and deserialized;
14/// - Instance can be sent to another thread (to be sent from a separate thread);
15/// - Be printable for debugging purposes;
16/// - Type has `'static` lifetime to be compatible with actor's handlers.
17///
18pub trait ProtocolData: Serialize + DeserializeOwned + Debug + Send + 'static {}
19
20impl<T> ProtocolData for T where T: Serialize + DeserializeOwned + Debug + Send + 'static {}
21
22/// The set of types for provide interaction capabilities for parties.
23pub trait Protocol: Send + 'static {
24 /// The message type sent to a server (from a client).
25 type ToServer: ProtocolData;
26 /// The message type sent to a client (from a server).
27 type ToClient: ProtocolData;
28 /// The codec (serialization format) used to pack and unpack messages.
29 type Codec: ProtocolCodec;
30}
31
32/// The serialization format for a `Protocol`.
33pub trait ProtocolCodec: Send {
34 // TODO: Consider adding `self` reference to implement stateful (smart) formats.
35 /// Decodes binary data to a type.
36 fn decode<T: ProtocolData>(data: &[u8]) -> Result<T, Error>;
37 /// Encodes value to a binary data.
38 fn encode<T: ProtocolData>(value: &T) -> Result<Vec<u8>, Error>;
39}