use std::marker::PhantomData; use serde::{Serialize, de::DeserializeOwned}; use crate::Error; pub trait CacheCodec: Send + Sync { type Value: Clone + Send + Sync + 'static; fn encode(&self, value: &Self::Value) -> Result, Error>; fn decode(&self, bytes: &[u8]) -> Result; } pub struct JsonCodec(PhantomData V>); impl Clone for JsonCodec { fn clone(&self) -> Self { *self } } impl Copy for JsonCodec {} impl Default for JsonCodec { fn default() -> Self { Self::new() } } impl JsonCodec { pub const fn new() -> Self { Self(PhantomData) } } impl CacheCodec for JsonCodec where V: Clone + Send + Sync + Serialize + DeserializeOwned + 'static, { type Value = V; fn encode(&self, value: &Self::Value) -> Result, Error> { serde_json::to_vec(value).map_err(|_| Error::InvalidEntry) } fn decode(&self, bytes: &[u8]) -> Result { serde_json::from_slice(bytes).map_err(|_| Error::InvalidEntry) } }