bluejay_core/
indexable.rs1use std::cmp::Ordering;
2use std::hash::Hash;
3use std::ops::Deref;
4
5pub trait Indexable {
6 type Id: Hash + Eq + Ord;
7
8 fn id(&self) -> &Self::Id;
9}
10
11pub struct Indexed<'a, T: Indexable>(pub &'a T);
12
13impl<T: Indexable> Clone for Indexed<'_, T> {
14 fn clone(&self) -> Self {
15 *self
16 }
17}
18
19impl<T: Indexable> Copy for Indexed<'_, T> {}
20
21impl<'a, T: Indexable> From<&'a T> for Indexed<'a, T> {
22 fn from(value: &'a T) -> Self {
23 Self(value)
24 }
25}
26
27impl<T: Indexable> Hash for Indexed<'_, T> {
28 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
29 self.0.id().hash(state);
30 }
31}
32
33impl<T: Indexable> PartialEq for Indexed<'_, T> {
34 fn eq(&self, other: &Self) -> bool {
35 self.0.id() == other.0.id()
36 }
37}
38
39impl<T: Indexable> PartialOrd for Indexed<'_, T> {
40 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
41 Some(self.cmp(other))
42 }
43}
44
45impl<T: Indexable> Ord for Indexed<'_, T> {
46 fn cmp(&self, other: &Self) -> Ordering {
47 self.0.id().cmp(other.0.id())
48 }
49}
50
51impl<T: Indexable> Eq for Indexed<'_, T> {}
52
53impl<T: Indexable> Deref for Indexed<'_, T> {
54 type Target = T;
55
56 fn deref(&self) -> &Self::Target {
57 self.0
58 }
59}