Skip to content

Commit 8c50a65

Browse files
committed
Manually implement common traits for EventId
# Objective Fixes #8528 ## Solution Manually implement `PartialEq`, `Eq`, `PartialOrd`, `Ord`, and `Hash` for `bevy_ecs::event::EventId`. These new implementations do not rely on the `Event` implementing the same traits allowing `EventId` to be used in more cases.
1 parent a29d328 commit 8c50a65

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

crates/bevy_ecs/src/event.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ use crate as bevy_ecs;
44
use crate::system::{Local, Res, ResMut, Resource, SystemParam};
55
use bevy_utils::detailed_trace;
66
use std::ops::{Deref, DerefMut};
7-
use std::{fmt, hash::Hash, iter::Chain, marker::PhantomData, slice::Iter};
7+
use std::{
8+
cmp::Ordering,
9+
fmt,
10+
hash::{Hash, Hasher},
11+
iter::Chain,
12+
marker::PhantomData,
13+
slice::Iter,
14+
};
815
/// A type that can be stored in an [`Events<E>`] resource
916
/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter.
1017
///
@@ -16,7 +23,6 @@ impl<T> Event for T where T: Send + Sync + 'static {}
1623
///
1724
/// An `EventId` can among other things be used to trace the flow of an event from the point it was
1825
/// sent to the point it was processed.
19-
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash)]
2026
pub struct EventId<E: Event> {
2127
pub id: usize,
2228
_marker: PhantomData<E>,
@@ -46,6 +52,32 @@ impl<E: Event> fmt::Debug for EventId<E> {
4652
}
4753
}
4854

55+
impl<E: Event> PartialEq for EventId<E> {
56+
fn eq(&self, other: &Self) -> bool {
57+
self.id == other.id
58+
}
59+
}
60+
61+
impl<E: Event> Eq for EventId<E> {}
62+
63+
impl<E: Event> PartialOrd for EventId<E> {
64+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
65+
Some(self.cmp(other))
66+
}
67+
}
68+
69+
impl<E: Event> Ord for EventId<E> {
70+
fn cmp(&self, other: &Self) -> Ordering {
71+
self.id.cmp(&other.id)
72+
}
73+
}
74+
75+
impl<E: Event> Hash for EventId<E> {
76+
fn hash<H: Hasher>(&self, state: &mut H) {
77+
Hash::hash(&self.id, state);
78+
}
79+
}
80+
4981
#[derive(Debug)]
5082
struct EventInstance<E: Event> {
5183
pub event_id: EventId<E>,

0 commit comments

Comments
 (0)