pub struct TextCrdt { /* private fields */ }Expand description
A collaborative text CRDT based on RGA (Replicated Growable Array) principles.
Each character is assigned a unique identifier (actor, counter) and is
stored in an internal sequence. Deletions use tombstones: characters are
marked as deleted but remain in the internal list so that concurrent
operations from different replicas can be merged deterministically.
Ordering of concurrent inserts at the same position is resolved by
comparing (counter, actor) tuples — higher counters come first, and
ties are broken lexicographically by actor ID.
§Example
use crdt_kit::prelude::*;
let mut t1 = TextCrdt::new("alice");
t1.insert_str(0, "hello");
let mut t2 = TextCrdt::new("bob");
t2.insert_str(0, "world");
t1.merge(&t2);
t2.merge(&t1);
// Both replicas converge to the same text.
assert_eq!(t1.to_string(), t2.to_string());Implementations§
Source§impl TextCrdt
impl TextCrdt
Sourcepub fn fork(&self, new_actor: impl Into<String>) -> Self
pub fn fork(&self, new_actor: impl Into<String>) -> Self
Create a fork of this replica with a different actor ID.
The returned replica contains an identical copy of the current content and version state, but subsequent inserts will use the new actor, preventing ID collisions between the two replicas.
Sourcepub fn insert_str(&mut self, index: usize, s: &str)
pub fn insert_str(&mut self, index: usize, s: &str)
Insert a string at the given visible index.
Characters are inserted left-to-right so that the resulting visible
text contains the string starting at index.
§Panics
Panics if index is greater than self.len().
Sourcepub fn remove(&mut self, index: usize)
pub fn remove(&mut self, index: usize)
Remove (tombstone) the character at the given visible index.
§Panics
Panics if index is greater than or equal to self.len().
Sourcepub fn remove_range(&mut self, start: usize, len: usize)
pub fn remove_range(&mut self, start: usize, len: usize)
Remove a range of characters starting at start with the given len.
§Panics
Panics if start + len is greater than self.len().