//! Utilities for type-safe byte encoding and decoding. use bytes::Bytes; #[macro_use] mod macros; mod channel; mod json; mod response; pub use channel::*; pub use json::*; pub use response::*; pub trait Encode: Sized + Send { fn encode(self) -> Target; fn encode_into(self) -> T where Target: Encode, { self.encode().encode() } } pub trait Decode: Sized { fn decode(self) -> anyhow::Result; fn decode_into(self) -> anyhow::Result where Target: Decode, { self.decode()?.decode() } } impl_identity!(Bytes); impl_identity!(Vec); /// Helps cast between the top level message types. /// Implement whichever ones are most convenient for the source type. pub trait CastBytes: Sized { fn from_bytes(bytes: Bytes) -> Self { Self::from_vec(bytes.into()) } fn into_bytes(self) -> Bytes { self.into_vec().into() } fn from_vec(bytes: Vec) -> Self { Self::from_bytes(bytes.into()) } fn into_vec(self) -> Vec { self.into_bytes().into() } } impl CastBytes for Bytes { fn from_bytes(bytes: Bytes) -> Self { bytes } fn into_bytes(self) -> Bytes { self } } impl CastBytes for Vec { fn from_vec(vec: Vec) -> Self { vec } fn into_vec(self) -> Vec { self } }