summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Kingsley <josh@joshkingsley.me>2025-11-13 22:25:57 +0200
committerJosh Kingsley <josh@joshkingsley.me>2025-11-13 22:25:57 +0200
commitccf7a93504965e0ecbd6d30adee1c8404fa65425 (patch)
tree737cd6b7ff0ece98e334190f8f7fe3526ca81f68
parent9d8e048aa2e532eeac63de16d0c5f5da11b8e60c (diff)
feat(crdt): remove lifetimes
-rw-r--r--crdt/src/lib.rs54
1 files changed, 31 insertions, 23 deletions
diff --git a/crdt/src/lib.rs b/crdt/src/lib.rs
index df8425d..4c41dd0 100644
--- a/crdt/src/lib.rs
+++ b/crdt/src/lib.rs
@@ -26,7 +26,7 @@ impl Doc {
});
}
- pub fn realize(&self) -> RealizedDoc<'_> {
+ pub fn realize(&self) -> RealizedDoc {
let mut realized = RealizedDoc::default();
for op in &self.ops {
@@ -44,10 +44,9 @@ pub struct Op {
}
impl Op {
- fn apply<'a>(&'a self, realized: &mut RealizedDoc<'a>) {
+ fn apply(&self, realized: &mut RealizedDoc) {
match &self.payload {
OpPayload::CreateGrid {
- id: grid_id,
rows,
base_cells_per_row,
} => {
@@ -55,18 +54,21 @@ impl Op {
.map(|row_idx| {
let cells = (0..*base_cells_per_row)
.map(|cell_idx| Cell {
- id: DerivedId::new(grid_id, "cell", cell_idx),
+ id: self.id.derive_id("cell", cell_idx),
})
.collect();
Row {
- id: DerivedId::new(grid_id, "row", row_idx),
+ id: self.id.derive_id("row", row_idx),
cells,
}
})
.collect();
- realized.grids.push(Grid { id: grid_id, rows });
+ realized.grids.push(Grid {
+ id: self.id.derive_id("grid", 0),
+ rows,
+ });
}
}
}
@@ -74,40 +76,47 @@ impl Op {
pub enum OpPayload {
CreateGrid {
- id: Uuid,
rows: usize,
base_cells_per_row: usize,
},
}
#[derive(Default)]
-pub struct RealizedDoc<'a> {
- grids: Vec<Grid<'a>>,
+pub struct RealizedDoc {
+ grids: Vec<Grid>,
}
-pub struct Grid<'a> {
- id: &'a Uuid,
- rows: Vec<Row<'a>>,
+pub struct Grid {
+ id: DerivedId,
+ rows: Vec<Row>,
}
-pub struct Row<'a> {
- id: DerivedId<'a>,
- cells: Vec<Cell<'a>>,
+pub struct Row {
+ id: DerivedId,
+ cells: Vec<Cell>,
}
-pub struct Cell<'a> {
- id: DerivedId<'a>,
+pub struct Cell {
+ id: DerivedId,
}
-pub struct DerivedId<'a> {
- id: &'a Uuid,
+pub struct DerivedId {
+ id: Uuid,
tag: &'static str,
index: usize,
}
-impl<'a> DerivedId<'a> {
- pub fn new(id: &'a Uuid, tag: &'static str, index: usize) -> Self {
- Self { id, tag, index }
+trait DerivableId {
+ fn derive_id(&self, tag: &'static str, index: usize) -> DerivedId;
+}
+
+impl DerivableId for Uuid {
+ fn derive_id(&self, tag: &'static str, index: usize) -> DerivedId {
+ DerivedId {
+ id: self.clone(),
+ tag,
+ index,
+ }
}
}
@@ -124,7 +133,6 @@ mod tests {
doc.append_op(
&actor_id,
OpPayload::CreateGrid {
- id: Uuid::now_v7(),
rows: 4,
base_cells_per_row: 16,
},