razor_rpc_codec/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(docsrs, allow(unused_attributes))]
3
4//! # razor-rpc-codec
5//!
6//! This crate provides some core trait and codec implementations for [`razor-rpc`](https://docs.rs/razor-rpc) and [`razor-stream`](https://docs.rs/razor-stream).
7//!
8
9/*
10 *  Note that there's no unify output interface in each serde impl,
11 *  whatever we want to serialize into (std::io::Write / Buffer/ Vec<u8>),
12 *  require the codec implement to match.
13 */
14
15use serde::{Deserialize, Serialize};
16
17/// The codec is immutable, if need changing (like setting up cipher), should have inner
18/// mutablilty
19pub trait Codec: Default + Send + Sync + Sized + 'static {
20    fn encode<T: Serialize>(&self, task: &T) -> Result<Vec<u8>, ()>;
21
22    /// sererialized the msg into buf (with std::io::Writer), and return the size written
23    fn encode_into<T: Serialize>(&self, task: &T, buf: &mut Vec<u8>) -> Result<usize, ()>;
24
25    fn decode<'a, T: Deserialize<'a>>(&self, buf: &'a [u8]) -> Result<T, ()>;
26}
27
28#[cfg(feature = "msgpack")]
29mod msgpack;
30#[cfg(feature = "msgpack")]
31pub use msgpack::*;