use bincode::{
config::Configuration,
serde::{decode_from_slice, encode_to_vec},
};
use super::*;
pub trait KeyBytes {
fn to_bytes(&self, serialization_config: Configuration) -> Vec<u8>;
fn from_bytes(
bytes: &[u8],
serialization_config: Configuration,
) -> Result<Self, DeserializationError>
where
Self: Sized;
}
impl<T: Serialize + DeserializeOwned> KeyBytes for T {
fn to_bytes(&self, serialization_config: Configuration) -> Vec<u8> {
encode_to_vec(self, serialization_config).expect("Serialization should not fail")
}
fn from_bytes(
bytes: &[u8],
serialization_config: Configuration,
) -> Result<Self, DeserializationError>
where
Self: Sized,
{
decode_from_slice::<Self, Configuration>(bytes, serialization_config).map(|(key, _)| key)
}
}