refactor: add has id

This commit is contained in:
Zixuan Chen 2022-10-15 10:48:46 +08:00
parent d28fa407ea
commit d924e9fac8
3 changed files with 36 additions and 2 deletions

View file

@ -5,10 +5,10 @@
//!
//! In future, we may also use [Change] to represent a transaction. But this decision is postponed.
use crate::{
id::{Counter, ID},
op::Op,
span::HasId,
};
use rle::{HasLength, Mergable, RleVec};
use smallvec::SmallVec;
@ -121,3 +121,9 @@ impl Mergable<ChangeMergeCfg> for Change {
&& self.lamport + self.len() as Lamport == other.lamport
}
}
impl HasId for Change {
fn id_start(&self) -> ID {
self.id
}
}

View file

@ -1,7 +1,7 @@
use crate::{
container::ContainerID,
id::{Counter, ID},
span::IdSpan,
span::{HasId, IdSpan},
};
use rle::{HasLength, Mergable, RleVec, Sliceable};
mod insert_content;
@ -124,3 +124,9 @@ impl Sliceable for Op {
}
}
}
impl HasId for Op {
fn id_start(&self) -> ID {
self.id
}
}

View file

@ -155,6 +155,28 @@ impl Mergable for IdSpan {
}
}
pub trait HasId {
fn id_start(&self) -> ID;
}
pub trait HasIdSpan: HasId + HasLength {
fn id_end(&self) -> ID {
self.id_start().inc(self.len() as i32)
}
fn id_last(&self) -> ID {
self.id_start().inc(self.len() as i32 - 1)
}
}
impl<T: HasId + HasLength> HasIdSpan for T {}
impl HasId for IdSpan {
#[inline]
fn id_start(&self) -> ID {
self.min_id()
}
}
#[cfg(test)]
mod test_id_span {
use rle::RleVec;