Manually implement common traits for EventId (#8529)

# 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.
This commit is contained in:
konsti219 2023-05-04 14:22:25 +02:00 committed by GitHub
parent 22121e69fb
commit 5da8af7d37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,14 @@ use crate as bevy_ecs;
use crate::system::{Local, Res, ResMut, Resource, SystemParam};
use bevy_utils::detailed_trace;
use std::ops::{Deref, DerefMut};
use std::{fmt, hash::Hash, iter::Chain, marker::PhantomData, slice::Iter};
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
iter::Chain,
marker::PhantomData,
slice::Iter,
};
/// A type that can be stored in an [`Events<E>`] resource
/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter.
///
@ -16,7 +23,6 @@ impl<T> Event for T where T: Send + Sync + 'static {}
///
/// An `EventId` can among other things be used to trace the flow of an event from the point it was
/// sent to the point it was processed.
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EventId<E: Event> {
pub id: usize,
_marker: PhantomData<E>,
@ -46,6 +52,32 @@ impl<E: Event> fmt::Debug for EventId<E> {
}
}
impl<E: Event> PartialEq for EventId<E> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<E: Event> Eq for EventId<E> {}
impl<E: Event> PartialOrd for EventId<E> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<E: Event> Ord for EventId<E> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl<E: Event> Hash for EventId<E> {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(&self.id, state);
}
}
#[derive(Debug)]
struct EventInstance<E: Event> {
pub event_id: EventId<E>,