text: Wrap BufferId into a newtype

This commit is contained in:
Piotr Osiewicz 2024-01-28 21:05:08 +01:00
parent 941e838be9
commit 5ab715aac9
34 changed files with 687 additions and 383 deletions

View file

@ -26,6 +26,7 @@ pub use selection::*;
use std::{
borrow::Cow,
cmp::{self, Ordering, Reverse},
fmt::Display,
future::Future,
iter::Iterator,
ops::{self, Deref, Range, Sub},
@ -59,10 +60,39 @@ pub struct Buffer {
wait_for_version_txs: Vec<(clock::Global, oneshot::Sender<()>)>,
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, PartialOrd, Ord, Eq)]
pub struct BufferId(u64);
impl Display for BufferId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl BufferId {
/// Returns Err if `id` is outside of BufferId domain.
pub fn new(id: u64) -> anyhow::Result<Self> {
Ok(Self(id))
}
/// Increments this buffer id, returning the old value.
/// So that's a post-increment operator in disguise.
pub fn next(&mut self) -> Self {
let old = *self;
self.0 += 1;
old
}
}
impl From<BufferId> for u64 {
fn from(id: BufferId) -> Self {
id.0
}
}
#[derive(Clone)]
pub struct BufferSnapshot {
replica_id: ReplicaId,
remote_id: u64,
remote_id: BufferId,
visible_text: Rope,
deleted_text: Rope,
line_ending: LineEnding,
@ -369,7 +399,7 @@ struct Edits<'a, D: TextDimension, F: FnMut(&FragmentSummary) -> bool> {
old_end: D,
new_end: D,
range: Range<(&'a Locator, usize)>,
buffer_id: u64,
buffer_id: BufferId,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
@ -478,7 +508,7 @@ pub struct UndoOperation {
}
impl Buffer {
pub fn new(replica_id: u16, remote_id: u64, mut base_text: String) -> Buffer {
pub fn new(replica_id: u16, remote_id: BufferId, mut base_text: String) -> Buffer {
let line_ending = LineEnding::detect(&base_text);
LineEnding::normalize(&mut base_text);
@ -545,7 +575,7 @@ impl Buffer {
self.lamport_clock.replica_id
}
pub fn remote_id(&self) -> u64 {
pub fn remote_id(&self) -> BufferId {
self.remote_id
}
@ -1590,7 +1620,7 @@ impl BufferSnapshot {
&self.visible_text
}
pub fn remote_id(&self) -> u64 {
pub fn remote_id(&self) -> BufferId {
self.remote_id
}