Skip to main content

DocumentType

Trait DocumentType 

Source
pub trait DocumentType:
    Clone
    + Send
    + Sync
    + 'static {
    const TYPE_ID: u8;
    const TYPE_NAME: &'static str;

    // Required methods
    fn identity(&self) -> (u32, u64);
    fn encode(&self) -> Vec<u8> ;
    fn decode(data: &[u8]) -> Option<Self>
       where Self: Sized;
    fn merge(&mut self, other: &Self) -> bool;

    // Provided methods
    fn to_delta_op(&self) -> Option<AppOperation> { ... }
    fn apply_delta_op(&mut self, _op: &AppOperation) -> bool { ... }
}
Expand description

A registered document type that can be synced through the mesh.

Implementations must be deterministic - the same logical state must encode consistently for merge operations to work correctly. Document identity (source_node, timestamp) is used for deduplication instead of content hash, since CRDT merge may change byte ordering.

Required Associated Constants§

Source

const TYPE_ID: u8

Unique type identifier (marker byte in 0xC0-0xCF range).

Source

const TYPE_NAME: &'static str

Human-readable type name for debugging.

Required Methods§

Source

fn identity(&self) -> (u32, u64)

Document identity for deduplication.

Returns (source_node, timestamp) tuple that uniquely identifies this document instance.

Source

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

Encode to wire format (payload only, not including type header).

Source

fn decode(data: &[u8]) -> Option<Self>
where Self: Sized,

Decode from wire format.

Input is the payload after the type header.

Source

fn merge(&mut self, other: &Self) -> bool

Merge with another instance using CRDT semantics.

Returns true if our state changed.

Provided Methods§

Source

fn to_delta_op(&self) -> Option<AppOperation>

Convert to a delta operation for efficient sync.

Returns None if this type doesn’t support delta sync (will use full-state sync instead).

Source

fn apply_delta_op(&mut self, _op: &AppOperation) -> bool

Apply a delta operation to this document.

Returns true if state changed.

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§

Source§

impl DocumentType for CannedMessageDocument

Source§

const TYPE_ID: u8 = CANNED_MESSAGE_TYPE_ID

Source§

const TYPE_NAME: &'static str = "CannedMessage"