Skip to main content

Codec

Trait Codec 

Source
pub trait Codec<T>:
    Send
    + Sync
    + 'static {
    // Required methods
    fn encode<'a>(
        &'a self,
        value: &'a T,
    ) -> BoxFuture<'a, Result<Payload, BoxError>>;
    fn decode(&self, payload: Payload) -> BoxFuture<'_, Result<T, BoxError>>;

    // Provided method
    fn encode_owned(
        &self,
        value: Arc<T>,
    ) -> BoxFuture<'_, Result<Payload, BoxError>>
       where T: Send + Sync + 'static { ... }
}
Expand description

Encodes cached values to payloads and decodes them back.

Encoding and decoding are asynchronous so a codec may await. For ordinary synchronous serializers implement SyncCodec and wrap the codec in FromSync. Every decode must return an independent value: the cache may retain one payload and decode it more than once.

Required Methods§

Source

fn encode<'a>( &'a self, value: &'a T, ) -> BoxFuture<'a, Result<Payload, BoxError>>

Serialize value. A failure counts as a serialization_dump error and skips the remote write.

Source

fn decode(&self, payload: Payload) -> BoxFuture<'_, Result<T, BoxError>>

Deserialize payload into an independent value. A failure counts as a serialization_load error and is treated as a miss.

Provided Methods§

Source

fn encode_owned( &self, value: Arc<T>, ) -> BoxFuture<'_, Result<Payload, BoxError>>
where T: Send + Sync + 'static,

Serialize a shared value owned by the cache.

The engine calls this method for remote writes. By default it delegates to encode without copying the value. Override it to move the shared handle into a background job without requiring T: Clone. The codec owns scheduling, admission and the lifetime of any jobs it starts; those jobs do not inherit the cache’s shadow-capacity token.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: Send + 'static, C: SyncCodec<T>> Codec<T> for FromSync<C>

Source§

impl<T: Serialize + DeserializeOwned + Send + Sync + 'static> Codec<T> for JsonCodec