pub trait CacheCodec:
Clone
+ Send
+ Sync
+ 'static {
// Required methods
fn encode<T>(&self, value: &T) -> Result<Bytes>
where T: Serialize;
fn decode<T>(&self, bytes: &Bytes) -> Result<T>
where T: DeserializeOwned;
}Expand description
Serialization boundary for cached values.
Implement this trait to replace the default PostcardCodec.
§Example
use hydracache_core::{CacheCodec, PostcardCodec};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct User {
id: u64,
}
let codec = PostcardCodec;
let bytes = codec.encode(&User { id: 1 }).unwrap();
let decoded: User = codec.decode(&bytes).unwrap();
assert_eq!(decoded, User { id: 1 });Required Methods§
Sourcefn encode<T>(&self, value: &T) -> Result<Bytes>where
T: Serialize,
fn encode<T>(&self, value: &T) -> Result<Bytes>where
T: Serialize,
Encode a typed value into bytes.
Sourcefn decode<T>(&self, bytes: &Bytes) -> Result<T>where
T: DeserializeOwned,
fn decode<T>(&self, bytes: &Bytes) -> Result<T>where
T: DeserializeOwned,
Decode bytes back into a typed value.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".