From 2a8cd3c50c816d0cfb4c826087234aee3cf6674f Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Tue, 18 Nov 2025 22:30:40 +0200 Subject: feat(crdt): delete cell entries in place --- crdt/src/lib.rs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'crdt/src/lib.rs') diff --git a/crdt/src/lib.rs b/crdt/src/lib.rs index a92b52d..cd86b97 100644 --- a/crdt/src/lib.rs +++ b/crdt/src/lib.rs @@ -1,6 +1,6 @@ mod vector_clock; -use std::{collections::BTreeSet, fmt::Display}; +use std::{collections::BTreeSet, fmt::Display, mem}; use num_rational::Ratio; use uuid::Uuid; @@ -165,6 +165,10 @@ impl Op { (end_cell_idx, start_cell_idx) }; + for entry in row.value_mut().cells[i..(j + 1)].iter_mut() { + entry.delete(self.id); + } + let span_duration: Ratio = row.value().cells[i..j + 1] .iter() .map(|cell| cell.value().duration) @@ -210,12 +214,20 @@ pub struct Doc { } #[derive(Debug)] -pub enum Entry { +pub enum Entry { Active { value: T }, Deleted { value: T, deleted_by: Uuid }, } -impl Entry { +impl Default for Entry { + fn default() -> Self { + Self::Active { + value: T::default(), + } + } +} + +impl Entry { pub fn active(value: T) -> Self { Self::Active { value } } @@ -240,27 +252,39 @@ impl Entry { Self::Deleted { value, .. } => value, } } + + pub fn delete(&mut self, op_id: Uuid) { + if matches!(self, Self::Active { .. }) { + *self = match mem::take(self) { + Self::Active { value } => Self::Deleted { + value, + deleted_by: op_id, + }, + _ => unreachable!(), + } + } + } } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Grid { id: DerivedId, rows: Vec>, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Row { id: DerivedId, cells: Vec>, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Cell { id: DerivedId, duration: Ratio, } -#[derive(PartialEq, Eq, Debug, Clone, Copy)] +#[derive(PartialEq, Eq, Debug, Clone, Copy, Default)] pub struct DerivedId { // TODO These IDs can be interned on the Doc id: Uuid, -- cgit v1.2.3