diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-11-18 22:30:40 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-11-18 22:30:40 +0200 |
| commit | 2a8cd3c50c816d0cfb4c826087234aee3cf6674f (patch) | |
| tree | 0bfe8877dfdfae96fdd7e73a77c74a847f3c3a11 /crdt/src | |
| parent | 28d319992fa41d3cd3452d92f01b55988cd2921a (diff) | |
feat(crdt): delete cell entries in place
Diffstat (limited to 'crdt/src')
| -rw-r--r-- | crdt/src/lib.rs | 38 |
1 files changed, 31 insertions, 7 deletions
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<u32> = row.value().cells[i..j + 1] .iter() .map(|cell| cell.value().duration) @@ -210,12 +214,20 @@ pub struct Doc { } #[derive(Debug)] -pub enum Entry<T> { +pub enum Entry<T: Default> { Active { value: T }, Deleted { value: T, deleted_by: Uuid }, } -impl<T> Entry<T> { +impl<T: Default> Default for Entry<T> { + fn default() -> Self { + Self::Active { + value: T::default(), + } + } +} + +impl<T: Default> Entry<T> { pub fn active(value: T) -> Self { Self::Active { value } } @@ -240,27 +252,39 @@ impl<T> Entry<T> { 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<Entry<Row>>, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Row { id: DerivedId, cells: Vec<Entry<Cell>>, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Cell { id: DerivedId, duration: Ratio<u32>, } -#[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, |
