pub struct CacheCodec { /* private fields */ }Expand description
Type-erased codec for serializing and deserializing cache entries.
CacheCodec carries the entry’s stable type_id/version plus two plain
function pointers — it is Copy and has no heap allocation. Construct one
via CacheCodec::from_impl for types that implement CacheCodecImpl,
or CacheCodec::new for custom cases (e.g. when the orphan rule prevents
a direct impl).
Implementations§
Source§impl CacheCodec
impl CacheCodec
Sourcepub fn new(
type_id: &'static str,
version: u32,
serialize_body: fn(&Arc<dyn Any + Send + Sync>, &mut CacheEntryWriter<'_>) -> Result<()>,
deserialize_body: fn(&mut CacheEntryReader<'_>) -> Result<Arc<dyn Any + Send + Sync>>,
) -> Self
pub fn new( type_id: &'static str, version: u32, serialize_body: fn(&Arc<dyn Any + Send + Sync>, &mut CacheEntryWriter<'_>) -> Result<()>, deserialize_body: fn(&mut CacheEntryReader<'_>) -> Result<Arc<dyn Any + Send + Sync>>, ) -> Self
Create a CacheCodec from explicit body function pointers.
Prefer from_impl when the value type implements
CacheCodecImpl. Use this for types where a direct impl isn’t
possible (e.g. the orphan rule prevents it). type_id and version
play the same role as the corresponding CacheCodecImpl constants.
Sourcepub fn from_impl<T: CacheCodecImpl + 'static>() -> Self
pub fn from_impl<T: CacheCodecImpl + 'static>() -> Self
Create a CacheCodec from a CacheCodecImpl implementation.
Sourcepub fn serialize(
&self,
value: &Arc<dyn Any + Send + Sync>,
writer: &mut dyn Write,
) -> Result<()>
pub fn serialize( &self, value: &Arc<dyn Any + Send + Sync>, writer: &mut dyn Write, ) -> Result<()>
Serialize value into writer: envelope first, then the body.
Sourcepub fn deserialize(
&self,
data: &Bytes,
) -> CacheDecode<Arc<dyn Any + Send + Sync>>
pub fn deserialize( &self, data: &Bytes, ) -> CacheDecode<Arc<dyn Any + Send + Sync>>
Deserialize an entry from data.
Never fails: any non-fatal failure to interpret the bytes becomes a
CacheDecode::Miss with the reason why (see CacheMissReason).
Reading from an in-memory Bytes cannot do I/O, so there is no fault
channel — a miss is the only non-Hit outcome.
Trait Implementations§
Source§impl Clone for CacheCodec
impl Clone for CacheCodec
Source§fn clone(&self) -> CacheCodec
fn clone(&self) -> CacheCodec
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more