Skip to main content

arc_malachitebft_codec/
lib.rs

1use core::error::Error;
2
3use bytes::Bytes;
4
5pub trait Codec<T>: Send + Sync + 'static {
6    type Error: Error + Send;
7
8    fn decode(&self, bytes: Bytes) -> Result<T, Self::Error>;
9    fn encode(&self, msg: &T) -> Result<Bytes, Self::Error>;
10}
11
12/// Codec extension trait for types that can also compute the length of the encoded data.
13pub trait HasEncodedLen<T>: Codec<T> {
14    fn encoded_len(&self, msg: &T) -> Result<usize, <Self as Codec<T>>::Error>;
15}