Skip to content

Commit b6198ad

Browse files
mgsloannathansobo
andauthored
Add Ord and PartialOrd impls for gpui entity types (zed-industries#26968)
Motivation is to be able to use entities as TreeMap keys. Release Notes: - N/A Co-authored-by: Nathan <[email protected]>
1 parent 5210d9e commit b6198ad

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

crates/gpui/src/app/entity_map.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use slotmap::{KeyData, SecondaryMap, SlotMap};
77
use std::{
88
any::{type_name, Any, TypeId},
99
cell::RefCell,
10+
cmp::Ordering,
1011
fmt::{self, Display},
1112
hash::{Hash, Hasher},
1213
marker::PhantomData,
@@ -350,6 +351,18 @@ impl PartialEq for AnyEntity {
350351

351352
impl Eq for AnyEntity {}
352353

354+
impl Ord for AnyEntity {
355+
fn cmp(&self, other: &Self) -> Ordering {
356+
self.entity_id.cmp(&other.entity_id)
357+
}
358+
}
359+
360+
impl PartialOrd for AnyEntity {
361+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
362+
Some(self.cmp(other))
363+
}
364+
}
365+
353366
impl std::fmt::Debug for AnyEntity {
354367
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355368
f.debug_struct("AnyEntity")
@@ -495,6 +508,18 @@ impl<T> PartialEq<WeakEntity<T>> for Entity<T> {
495508
}
496509
}
497510

511+
impl<T: 'static> Ord for Entity<T> {
512+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
513+
self.entity_id().cmp(&other.entity_id())
514+
}
515+
}
516+
517+
impl<T: 'static> PartialOrd for Entity<T> {
518+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
519+
Some(self.cmp(other))
520+
}
521+
}
522+
498523
/// A type erased, weak reference to a entity.
499524
#[derive(Clone)]
500525
pub struct AnyWeakEntity {
@@ -599,6 +624,18 @@ impl PartialEq for AnyWeakEntity {
599624

600625
impl Eq for AnyWeakEntity {}
601626

627+
impl Ord for AnyWeakEntity {
628+
fn cmp(&self, other: &Self) -> Ordering {
629+
self.entity_id.cmp(&other.entity_id)
630+
}
631+
}
632+
633+
impl PartialOrd for AnyWeakEntity {
634+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
635+
Some(self.cmp(other))
636+
}
637+
}
638+
602639
/// A weak reference to a entity of the given type.
603640
#[derive(Deref, DerefMut)]
604641
pub struct WeakEntity<T> {
@@ -711,6 +748,18 @@ impl<T> PartialEq<Entity<T>> for WeakEntity<T> {
711748
}
712749
}
713750

751+
impl<T: 'static> Ord for WeakEntity<T> {
752+
fn cmp(&self, other: &Self) -> Ordering {
753+
self.entity_id().cmp(&other.entity_id())
754+
}
755+
}
756+
757+
impl<T: 'static> PartialOrd for WeakEntity<T> {
758+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
759+
Some(self.cmp(other))
760+
}
761+
}
762+
714763
#[cfg(any(test, feature = "leak-detection"))]
715764
static LEAK_BACKTRACE: std::sync::LazyLock<bool> =
716765
std::sync::LazyLock::new(|| std::env::var("LEAK_BACKTRACE").map_or(false, |b| !b.is_empty()));

0 commit comments

Comments
 (0)