multi_buffer: Typed MultiBufferOffset (#42707)

This PR introduces a new `MultiBufferOffset` new type wrapping size. The
goal of this is to make it clear at the type level when we are
interacting with offsets of a multi buffer versus offsets of a language
/ text buffer. This improves readability of things quite a bit by making
it clear what kind of offsets one is working with while also reducing
accidental bugs by using the wrong kin of offset for the wrong API.

This PR also uncovered two minor bugs due to that.

Does not yet introduce the MultiBufferPoint equivalent, that is for a
follow up PR.

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-11-19 22:00:58 +00:00
committed by GitHub
parent 5e21457f21
commit c98b2d6944
81 changed files with 2957 additions and 1564 deletions
+9 -3
View File
@@ -130,9 +130,15 @@ impl<T: Copy> Selection<T> {
}
}
impl Selection<usize> {
impl<T: std::ops::Sub + Copy> Selection<T> {
pub fn len(&self) -> <T as std::ops::Sub>::Output {
self.end - self.start
}
}
impl<T: Copy + Eq> Selection<T> {
#[cfg(feature = "test-support")]
pub fn from_offset(offset: usize) -> Self {
pub fn from_offset(offset: T) -> Self {
Selection {
id: 0,
start: offset,
@@ -142,7 +148,7 @@ impl Selection<usize> {
}
}
pub fn equals(&self, offset_range: &Range<usize>) -> bool {
pub fn equals(&self, offset_range: &Range<T>) -> bool {
self.start == offset_range.start && self.end == offset_range.end
}
}
+31 -12
View File
@@ -6,36 +6,55 @@ use std::{
};
#[derive(Default)]
pub struct Topic(Mutex<Vec<Weak<Mutex<Patch<usize>>>>>);
pub struct Topic<T>(Mutex<Vec<Weak<Mutex<Patch<T>>>>>);
pub struct Subscription(Arc<Mutex<Patch<usize>>>);
pub struct Subscription<T>(Arc<Mutex<Patch<T>>>);
impl Topic {
pub fn subscribe(&mut self) -> Subscription {
impl<T: Default, TDelta> Topic<T>
where
T: 'static
+ Copy
+ Ord
+ std::ops::Sub<T, Output = TDelta>
+ std::ops::Add<TDelta, Output = T>
+ std::ops::AddAssign<TDelta>
+ Default,
TDelta: Ord + Copy,
{
pub fn subscribe(&mut self) -> Subscription<T> {
let subscription = Subscription(Default::default());
self.0.get_mut().push(Arc::downgrade(&subscription.0));
subscription
}
pub fn publish(&self, edits: impl Clone + IntoIterator<Item = Edit<usize>>) {
pub fn publish(&self, edits: impl Clone + IntoIterator<Item = Edit<T>>) {
publish(&mut self.0.lock(), edits);
}
pub fn publish_mut(&mut self, edits: impl Clone + IntoIterator<Item = Edit<usize>>) {
pub fn publish_mut(&mut self, edits: impl Clone + IntoIterator<Item = Edit<T>>) {
publish(self.0.get_mut(), edits);
}
}
impl Subscription {
pub fn consume(&self) -> Patch<usize> {
impl<T: Default> Subscription<T> {
pub fn consume(&self) -> Patch<T> {
mem::take(&mut *self.0.lock())
}
}
fn publish(
subscriptions: &mut Vec<Weak<Mutex<Patch<usize>>>>,
edits: impl Clone + IntoIterator<Item = Edit<usize>>,
) {
fn publish<T, TDelta>(
subscriptions: &mut Vec<Weak<Mutex<Patch<T>>>>,
edits: impl Clone + IntoIterator<Item = Edit<T>>,
) where
T: 'static
+ Copy
+ Ord
+ std::ops::Sub<T, Output = TDelta>
+ std::ops::Add<TDelta, Output = T>
+ std::ops::AddAssign<TDelta>
+ Default,
TDelta: Ord + Copy,
{
subscriptions.retain(|subscription| {
if let Some(subscription) = subscription.upgrade() {
let mut patch = subscription.lock();
+2 -2
View File
@@ -54,7 +54,7 @@ pub struct Buffer {
deferred_ops: OperationQueue<Operation>,
deferred_replicas: HashSet<ReplicaId>,
pub lamport_clock: clock::Lamport,
subscriptions: Topic,
subscriptions: Topic<usize>,
edit_id_resolvers: HashMap<clock::Lamport, Vec<oneshot::Sender<()>>>,
wait_for_version_txs: Vec<(clock::Global, oneshot::Sender<()>)>,
}
@@ -1619,7 +1619,7 @@ impl Buffer {
self.edited_ranges_for_edit_ids(&transaction.edit_ids)
}
pub fn subscribe(&mut self) -> Subscription {
pub fn subscribe(&mut self) -> Subscription<usize> {
self.subscriptions.subscribe()
}