From a7b06c92185bbe210caf5f98d67608a79f25be87 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 8 Jan 2024 23:32:29 +0100 Subject: [PATCH] move `Anchor` and `AnchorBias` to the new `anchor` module --- src/anchor.rs | 72 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++- src/replica.rs | 2 +- src/run_indices.rs | 7 ---- src/run_tree.rs | 85 +++++----------------------------------------- 5 files changed, 84 insertions(+), 86 deletions(-) create mode 100644 src/anchor.rs diff --git a/src/anchor.rs b/src/anchor.rs new file mode 100644 index 0000000..f664003 --- /dev/null +++ b/src/anchor.rs @@ -0,0 +1,72 @@ +use crate::*; + +/// TODO: docs +#[derive(Copy, Clone, PartialEq, Eq)] +#[cfg_attr( + any(feature = "encode", feature = "serde"), + derive(serde::Serialize, serde::Deserialize) +)] +pub struct Anchor { + /// TODO: docs + replica_id: ReplicaId, + + /// The [`RunTs`] of the [`EditRun`] containing this [`Anchor`]. + contained_in: RunTs, + + /// TODO: docs + offset: Length, +} + +impl core::fmt::Debug for Anchor { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + if self == &Self::zero() { + write!(f, "zero") + } else { + write!(f, "{:x}.{}", self.replica_id, self.offset) + } + } +} + +impl Anchor { + #[inline(always)] + pub(crate) fn is_zero(&self) -> bool { + self.replica_id == 0 + } + + #[inline(always)] + pub(crate) fn new( + replica_id: ReplicaId, + offset: Length, + run_ts: RunTs, + ) -> Self { + Self { replica_id, offset, contained_in: run_ts } + } + + #[inline(always)] + pub(crate) fn offset(&self) -> Length { + self.offset + } + + #[inline(always)] + pub(crate) fn replica_id(&self) -> ReplicaId { + self.replica_id + } + + #[inline(always)] + pub(crate) fn run_ts(&self) -> RunTs { + self.contained_in + } + + /// A special value used to create an anchor at the start of the document. + #[inline] + pub const fn zero() -> Self { + Self { replica_id: 0, offset: 0, contained_in: 0 } + } +} + +/// TODO: docs +#[derive(PartialEq, Eq)] +pub(crate) enum AnchorBias { + Left, + Right, +} diff --git a/src/lib.rs b/src/lib.rs index 5144a6a..31fc3ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,6 +126,7 @@ extern crate alloc; +mod anchor; mod backlog; mod crdt_edit; mod gtree; @@ -137,6 +138,7 @@ mod text_edit; mod utils; mod version_map; +use anchor::*; use backlog::Backlog; pub use backlog::{BackloggedDeletions, BackloggedInsertions}; pub use crdt_edit::{Deletion, Insertion}; @@ -145,7 +147,7 @@ pub use replica::Replica; use replica::*; pub use replica_id::ReplicaId; use replica_id::{ReplicaIdMap, ReplicaIdMapValuesMut}; -use run_indices::{AnchorBias, RunIndices}; +use run_indices::RunIndices; use run_tree::*; pub use text_edit::Text; use utils::*; diff --git a/src/replica.rs b/src/replica.rs index 8ca2399..932760e 100644 --- a/src/replica.rs +++ b/src/replica.rs @@ -468,7 +468,7 @@ impl Replica { /// somewhere in its Gtree. #[inline] fn has_anchor(&self, anchor: Anchor) -> bool { - self.version_map.get(anchor.replica_id()) >= anchor.character_ts() + self.version_map.get(anchor.replica_id()) >= anchor.offset() } /// Returns `true` if this `Replica` has already merged the given diff --git a/src/run_indices.rs b/src/run_indices.rs index 01eabdb..5a4ba50 100644 --- a/src/run_indices.rs +++ b/src/run_indices.rs @@ -16,13 +16,6 @@ impl core::fmt::Debug for RunIndices { } } -/// TODO: docs -#[derive(PartialEq, Eq)] -pub(crate) enum AnchorBias { - Left, - Right, -} - impl RunIndices { pub fn assert_invariants(&self, run_tree: &RunTree) { for (&replica_id, indices) in self.map.iter() { diff --git a/src/run_tree.rs b/src/run_tree.rs index fd09114..3f943cb 100644 --- a/src/run_tree.rs +++ b/src/run_tree.rs @@ -481,10 +481,10 @@ impl RunTree { let delete_from = if deletion.start().is_zero() { 0 } else { - deletion.start().offset - run.start() + deletion.start().offset() - run.start() }; - let delete_up_to = deletion.end().offset - run.start(); + let delete_up_to = deletion.end().offset() - run.start(); let delete_range = (delete_from..delete_up_to).into(); self.delete_leaf_range(start_idx, leaf_offset, delete_range); ranges.push((delete_range + leaf_offset).into()); @@ -512,7 +512,7 @@ impl RunTree { let delete_from = if deletion.start().is_zero() { 0 } else { - deletion.start().offset - start.start() + deletion.start().offset() - start.start() }; let deleted_up_to = deletion.version_map().get(start.replica_id()); @@ -565,7 +565,7 @@ impl RunTree { ranges.push(start_offset..visible_offset); } } else { - let delete_up_to = deletion.end().offset - run.start(); + let delete_up_to = deletion.end().offset() - run.start(); self.delete_leaf_range( end_idx, @@ -680,8 +680,8 @@ impl RunTree { // If the insertion is anchored in the middle of the anchor run then // there can't be any other runs that are tied with it. In this case we // can just split the anchor run and insert the new run after it. - if insertion.anchor().offset < anchor.end() { - let insert_at = insertion.anchor().offset - anchor.start(); + if insertion.anchor().offset() < anchor.end() { + let insert_at = insertion.anchor().offset() - anchor.start(); return self.split_run_with_another(run, anchor_idx, insert_at); } @@ -873,8 +873,8 @@ impl EditRun { debug_assert!(!anchor.is_zero()); self.replica_id() == anchor.replica_id() - && self.text.start() < anchor.offset - && self.text.end() >= anchor.offset + && self.text.start() < anchor.offset() + && self.text.end() >= anchor.offset() } #[inline(always)] @@ -1015,75 +1015,6 @@ impl EditRun { } } -/// TODO: docs -#[derive(Copy, Clone, PartialEq, Eq)] -#[cfg_attr( - any(feature = "encode", feature = "serde"), - derive(serde::Serialize, serde::Deserialize) -)] -pub struct Anchor { - /// TODO: docs - replica_id: ReplicaId, - - /// The [`RunTs`] of the [`EditRun`] containing this [`Anchor`]. - contained_in: RunTs, - - /// TODO: docs - offset: Length, -} - -impl core::fmt::Debug for Anchor { - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - if self == &Self::zero() { - write!(f, "zero") - } else { - write!(f, "{:x}.{}", self.replica_id, self.offset) - } - } -} - -impl Anchor { - #[inline(always)] - pub(crate) fn character_ts(&self) -> Length { - self.offset - } - - #[inline(always)] - pub(crate) fn is_zero(&self) -> bool { - self.replica_id == 0 - } - - #[inline(always)] - pub(crate) fn new( - replica_id: ReplicaId, - offset: Length, - run_ts: RunTs, - ) -> Self { - Self { replica_id, offset, contained_in: run_ts } - } - - #[inline(always)] - pub(crate) fn offset(&self) -> Length { - self.offset - } - - #[inline(always)] - pub(crate) fn replica_id(&self) -> ReplicaId { - self.replica_id - } - - #[inline(always)] - pub(crate) fn run_ts(&self) -> RunTs { - self.contained_in - } - - /// A special value used to create an anchor at the start of the document. - #[inline] - pub const fn zero() -> Self { - Self { replica_id: 0, offset: 0, contained_in: 0 } - } -} - #[derive(Debug, Clone, Copy)] pub enum Diff { Add(Length),