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 ...
50 lines
1.0 KiB
Rust
50 lines
1.0 KiB
Rust
use std::ops::{Add, AddAssign, Sub};
|
|
|
|
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
|
|
pub struct OffsetUtf16(pub usize);
|
|
|
|
impl<'a> Add<&'a Self> for OffsetUtf16 {
|
|
type Output = Self;
|
|
|
|
fn add(self, other: &'a Self) -> Self::Output {
|
|
Self(self.0 + other.0)
|
|
}
|
|
}
|
|
|
|
impl Add for OffsetUtf16 {
|
|
type Output = Self;
|
|
|
|
fn add(self, other: Self) -> Self::Output {
|
|
Self(self.0 + other.0)
|
|
}
|
|
}
|
|
|
|
impl<'a> Sub<&'a Self> for OffsetUtf16 {
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: &'a Self) -> Self::Output {
|
|
debug_assert!(*other <= self);
|
|
Self(self.0 - other.0)
|
|
}
|
|
}
|
|
|
|
impl Sub for OffsetUtf16 {
|
|
type Output = OffsetUtf16;
|
|
|
|
fn sub(self, other: Self) -> Self::Output {
|
|
Self(self.0 - other.0)
|
|
}
|
|
}
|
|
|
|
impl<'a> AddAssign<&'a Self> for OffsetUtf16 {
|
|
fn add_assign(&mut self, other: &'a Self) {
|
|
self.0 += other.0;
|
|
}
|
|
}
|
|
|
|
impl AddAssign<Self> for OffsetUtf16 {
|
|
fn add_assign(&mut self, other: Self) {
|
|
self.0 += other.0;
|
|
}
|
|
}
|