Skip to content

Commit

Permalink
fix: Allow partial comparison of different ref variants
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsvante committed Oct 19, 2023
1 parent 04b5cbd commit 10a006d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/entity_ref/ref_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,19 @@ impl<T: RefTarget> Eq for Ref<T> {}

impl<T: RefTarget> PartialOrd for Ref<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Ref::Id(id1), Ref::Id(id2)) => id1.partial_cmp(id2),
(Ref::ExternalId(external_id1), Ref::ExternalId(external_id2)) => {
external_id1.partial_cmp(external_id2)
}
(Ref::Slug(slug1), Ref::Slug(slug2)) => slug1.partial_cmp(slug2),
_ => None,
}
Some(self.cmp(other))
}
}

impl<T: RefTarget> Ord for Ref<T> {
fn cmp(&self, other: &Self) -> Ordering {
// NOTE: None is returned by partial_eq when different variants are compared.
self.partial_cmp(other).unwrap_or(Ordering::Less)
match (self, other) {
(Ref::Id(id1), Ref::Id(id2)) => id1.cmp(id2),
(Ref::ExternalId(external_id1), Ref::ExternalId(external_id2)) => {
external_id1.cmp(external_id2)
}
(Ref::Slug(slug1), Ref::Slug(slug2)) => slug1.cmp(slug2),
_ => Ordering::Less,
}
}
}

0 comments on commit 10a006d

Please sign in to comment.