use anyhow::Result;
pub trait Message: Send + Sync + 'static {}
impl<T> Message for T where T: Send + Sync + 'static {}
#[cfg_attr(
feature = "bincode",
doc = r##"
# [`bincode`] + [`serde`] support
With the `bincode` feature enabled, this trait will automatically be implemented for types which
implement [`serde::Serialize`].
"##
)]
pub trait TryIntoBytes {
fn try_into_bytes(self) -> Result<Vec<u8>>;
}
#[cfg_attr(
feature = "bincode",
doc = r##"
# [`bincode`] + [`serde`] support
With the `bincode` feature enabled, this trait will automatically be implemented for types which
implement [`serde::de::DeserializeOwned`].
"##
)]
pub trait TryFromBytes: Sized {
fn try_from_bytes(buf: &[u8]) -> Result<Self>;
}
#[cfg(feature = "bincode")]
impl<T> TryIntoBytes for T
where
T: serde::Serialize,
{
fn try_into_bytes(self) -> Result<Vec<u8>> {
bincode::serialize(&self).map_err(anyhow::Error::new)
}
}
#[cfg(feature = "bincode")]
impl<T> TryFromBytes for T
where
T: serde::de::DeserializeOwned,
{
fn try_from_bytes(buf: &[u8]) -> Result<Self> {
bincode::deserialize(buf).map_err(anyhow::Error::new)
}
}