pub struct List { /* private fields */ }Expand description
Ordered list with stable positioning for CRDT operations.
List maintains a stable ordering of elements using Position keys
in a BTreeMap. This design enables concurrent insertions without requiring
coordination between distributed replicas.
§Key Features
- Stable ordering: Elements maintain their relative positions even with concurrent modifications
- Insertion anywhere: Can insert between any two existing elements
- CRDT semantics: Proper merge behavior for distributed systems
- Efficient access: O(log n) access by position, O(1) by index for small lists
§Usage Patterns
let mut list = List::new();
// Simple append operations
list.push("first"); // Returns index 0
list.push("second"); // Returns index 1
// Insert between existing elements using index
list.insert(1, "between").unwrap();
// Access by traditional index
assert_eq!(list.get(0).unwrap().as_text(), Some("first"));
// Advanced users can use Position for precise control
let pos1 = Position::new(1, 1); // 1.0
let pos2 = Position::new(2, 1); // 2.0
let middle = Position::between(&pos1, &pos2);
list.insert_at_position(middle, "advanced");§Concurrent Operations
When two replicas insert at the same logical position, the rational number system ensures a consistent final order:
Replica A: ["item1", "item3"] -> inserts "item2" between them
Replica B: ["item1", "item3"] -> inserts "item4" between them
After merge: ["item1", "item2", "item4", "item3"]
(order determined by Position comparison)Implementations§
Source§impl List
impl List
Sourcepub fn push(&mut self, value: impl Into<Value>) -> usize
pub fn push(&mut self, value: impl Into<Value>) -> usize
Pushes a value to the end of the list Returns the index of the newly added element
Sourcepub fn insert(
&mut self,
index: usize,
value: impl Into<Value>,
) -> Result<(), CRDTError>
pub fn insert( &mut self, index: usize, value: impl Into<Value>, ) -> Result<(), CRDTError>
Inserts a value at a specific index
Sourcepub fn get(&self, index: usize) -> Option<&Value>
pub fn get(&self, index: usize) -> Option<&Value>
Gets a value by index (0-based), filtering out tombstones
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut Value>
pub fn get_mut(&mut self, index: usize) -> Option<&mut Value>
Gets a mutable reference to a value by index (0-based), filtering out tombstones
Sourcepub fn insert_at_position(
&mut self,
position: Position,
value: impl Into<Value>,
)
pub fn insert_at_position( &mut self, position: Position, value: impl Into<Value>, )
Inserts a value at a specific position (advanced API)
Sourcepub fn get_by_position(&self, position: &Position) -> Option<&Value>
pub fn get_by_position(&self, position: &Position) -> Option<&Value>
Gets a value by position
Sourcepub fn get_by_position_mut(&mut self, position: &Position) -> Option<&mut Value>
pub fn get_by_position_mut(&mut self, position: &Position) -> Option<&mut Value>
Gets a mutable reference to a value by position
Sourcepub fn set(&mut self, index: usize, value: impl Into<Value>) -> Option<Value>
pub fn set(&mut self, index: usize, value: impl Into<Value>) -> Option<Value>
Sets a value at a specific index, returns the old value if present Only considers non-tombstone elements for indexing
Sourcepub fn remove(&mut self, index: usize) -> Option<Value>
pub fn remove(&mut self, index: usize) -> Option<Value>
Removes a value by index (tombstones it for CRDT semantics) Only considers non-tombstone elements for indexing
Sourcepub fn remove_by_position(&mut self, position: &Position) -> Option<Value>
pub fn remove_by_position(&mut self, position: &Position) -> Option<Value>
Removes a value by position
Sourcepub fn iter(&self) -> impl Iterator<Item = &Value>
pub fn iter(&self) -> impl Iterator<Item = &Value>
Returns an iterator over the values in order (excluding tombstones)
Sourcepub fn iter_all(&self) -> impl Iterator<Item = &Value>
pub fn iter_all(&self) -> impl Iterator<Item = &Value>
Returns an iterator over all values including tombstones
Sourcepub fn iter_with_positions(&self) -> impl Iterator<Item = (&Position, &Value)>
pub fn iter_with_positions(&self) -> impl Iterator<Item = (&Position, &Value)>
Returns an iterator over position-value pairs in order
Trait Implementations§
Source§impl<'de> Deserialize<'de> for List
impl<'de> Deserialize<'de> for List
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl FromIterator<Value> for List
impl FromIterator<Value> for List
impl Data for List
impl Eq for List
impl StructuralPartialEq for List
Auto Trait Implementations§
impl Freeze for List
impl RefUnwindSafe for List
impl Send for List
impl Sync for List
impl Unpin for List
impl UnwindSafe for List
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more