Development Documentation (main branch) - For stable release docs, see docs.rs/eidetica
Skip to main content

Codec

Trait Codec 

Source
pub trait Codec: Sized {
    // Required methods
    fn encode(&self) -> Result<Vec<u8>>;
    fn decode(bytes: &[u8]) -> Result<Self>;
}
Expand description

Explicit byte encoding for data at a Store’s persistence boundary.

Encoding is independent of merging, cloning and Serde. Implementations must preserve all state needed by future merges, including tombstones, and decode one complete value rather than silently accepting malformed or trailing data. Existing Store formats must remain readable; changing a durable format needs a separate compatibility decision.

There is no blanket Serde implementation. A type can use JSON explicitly:

use eidetica::{crdt::Codec, Result};

#[derive(serde::Serialize, serde::Deserialize)]
struct MyData { value: String }

impl Codec for MyData {
    fn encode(&self) -> Result<Vec<u8>> {
        Ok(serde_json::to_vec(self)?)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        Ok(serde_json::from_slice(bytes)?)
    }
}
let data = MyData { value: "example".into() };
assert_eq!(MyData::decode(&data.encode()?)?.value, data.value);

Required Methods§

Source

fn encode(&self) -> Result<Vec<u8>>

Encode a complete value, including all merge-relevant state.

Source

fn decode(bytes: &[u8]) -> Result<Self>

Decode one complete value, rejecting malformed or trailing data.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§