occams_rpc_core/codec.rs
1use serde::{Deserialize, Serialize};
2
3/*
4 * Note that there's no unify output interface in each serde impl,
5 * whatever we want to serialize into (std::io::Write / Buffer/ Vec<u8>),
6 * require the codec implement to match.
7 */
8
9/// Interface for [occams-rpc-codec](https://docs.rs/occams-rpc-codec)
10///
11/// The codec is immutable, if need changing (like setting up cipher), should have inner
12/// mutablilty
13pub trait Codec: Default + Send + Sync + Sized + 'static {
14 fn encode<T: Serialize>(&self, task: &T) -> Result<Vec<u8>, ()>;
15
16 /// sererialized the msg into buf (with std::io::Writer), and return the size written
17 fn encode_into<T: Serialize>(&self, task: &T, buf: &mut Vec<u8>) -> Result<usize, ()>;
18
19 fn decode<'a, T: Deserialize<'a>>(&self, buf: &'a [u8]) -> Result<T, ()>;
20}