Remove result from anchor cmp functions

Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Keith Simmons 2022-03-24 11:48:31 -07:00
parent ab631cf6c3
commit 92c7b5d6ef
11 changed files with 68 additions and 141 deletions

View file

@ -24,7 +24,7 @@ impl Anchor {
bias: Bias::Right,
};
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Result<Ordering> {
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
let fragment_id_comparison = if self.timestamp == other.timestamp {
Ordering::Equal
} else {
@ -33,13 +33,13 @@ impl Anchor {
.cmp(&buffer.fragment_id_for_anchor(other))
};
Ok(fragment_id_comparison
fragment_id_comparison
.then_with(|| self.offset.cmp(&other.offset))
.then_with(|| self.bias.cmp(&other.bias)))
.then_with(|| self.bias.cmp(&other.bias))
}
pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
if self.cmp(other, buffer).unwrap().is_le() {
if self.cmp(other, buffer).is_le() {
self.clone()
} else {
other.clone()
@ -47,7 +47,7 @@ impl Anchor {
}
pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
if self.cmp(other, buffer).unwrap().is_ge() {
if self.cmp(other, buffer).is_ge() {
self.clone()
} else {
other.clone()
@ -117,8 +117,8 @@ pub trait AnchorRangeExt {
impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering> {
Ok(match self.start.cmp(&other.start, buffer)? {
Ordering::Equal => other.end.cmp(&self.end, buffer)?,
Ok(match self.start.cmp(&other.start, buffer) {
Ordering::Equal => other.end.cmp(&self.end, buffer),
ord @ _ => ord,
})
}

View file

@ -340,59 +340,41 @@ fn test_anchors() {
let anchor_at_offset_2 = buffer.anchor_before(2);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
anchor_at_offset_0.cmp(&anchor_at_offset_0, &buffer),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
anchor_at_offset_1.cmp(&anchor_at_offset_1, &buffer),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
anchor_at_offset_2.cmp(&anchor_at_offset_2, &buffer),
Ordering::Equal
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
anchor_at_offset_0.cmp(&anchor_at_offset_1, &buffer),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
anchor_at_offset_1.cmp(&anchor_at_offset_2, &buffer),
Ordering::Less
);
assert_eq!(
anchor_at_offset_0
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
anchor_at_offset_0.cmp(&anchor_at_offset_2, &buffer),
Ordering::Less
);
assert_eq!(
anchor_at_offset_1
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
anchor_at_offset_1.cmp(&anchor_at_offset_0, &buffer),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
anchor_at_offset_2.cmp(&anchor_at_offset_1, &buffer),
Ordering::Greater
);
assert_eq!(
anchor_at_offset_2
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
anchor_at_offset_2.cmp(&anchor_at_offset_0, &buffer),
Ordering::Greater
);
}