Skip to main content

ReplicaState

Struct ReplicaState 

Source
pub struct ReplicaState { /* private fields */ }
Expand description

A durable per-replica CRDT state — one participant (an editor’s FFI node, or the supervisor) in a multi-replica document session.

Unlike crate::crdt::CrdtDoc used by the rebuild-from-text merge, a ReplicaState is kept across cycles: it accumulates ops and exchanges only deltas with its peers via ReplicaState::state_vector / ReplicaState::diff / ReplicaState::apply_update.

Implementations§

Source§

impl ReplicaState

Source

pub fn new(client_id: u64) -> Self

Create a fresh empty replica with a stable client_id. Distinct replicas MUST use distinct client ids so concurrent inserts order deterministically (yrs orders concurrent inserts at the same position by ascending client id, matching the agent-before-human convention in crate::crdt).

Source

pub fn from_encoded(client_id: u64, state: &[u8]) -> Result<Self>

Bootstrap a replica from a previously encoded state (the durable boundary checkpoint / a peer’s full snapshot on first contact).

Source

pub fn text(&self) -> String

The current converged text.

Source

pub fn apply_local_edit(&self, offset: u32, delete_len: u32, insert: &str)

Apply a local edit: delete delete_len chars at offset, then insert insert there (one transaction). Mirrors crate::crdt::CrdtDoc::apply_edit.

Source

pub fn state_vector(&self) -> Vec<u8> ⓘ

This replica’s state vector, encoded for the wire. The state vector is the compact per-client causal summary (each client’s clock), NOT the whole document: a peer replies with diff carrying only the ops this vector is missing.

Source

pub fn diff(&self, their_sv: &[u8]) -> Result<Vec<u8>>

The incremental update carrying exactly the ops their_sv is missing (encode_state_as_update(their_sv)) — a delta, never a whole-document snapshot. This is the sync reply a replica sends a peer that announced their_sv.

Source

pub fn apply_update(&self, update: &[u8]) -> Result<()>

Apply a remote update. Idempotent (re-applying a known update is a no-op) and causal-buffered by yrs (an update whose causal dependencies have not arrived is buffered and integrated once they do), so duplicate or out-of-order delivery converges rather than corrupting.

Source

pub fn encode_state(&self) -> Vec<u8> ⓘ

The full encoded state — a durable projection / boundary checkpoint, or the snapshot a peer needs on first contact (no shared history yet).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.