Skip to content

bevy_ecs::event::EventId should manually implement Eq, PartialEq, Ord, PartialOrd, and Hash #8528

Closed
@Themayu

Description

@Themayu

Currently, bevy_ecs::event::EventId makes use of derive macros for its implementations of a set of traits:

#[derive(Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct EventId<E: Event> {
    pub id: usize,
    _marker: PhantomData<E>,
}

This creates an issue when attempting to actually use these implementations (especially that of Hash), as it requires them to be implemented on the events themselves; despite the generic only being used in a marker type.

It would be better to manually implement these traits to ignore the marker, freeing them up to be used in more places than they are currently:

error[E0599]: the method `insert` exists for struct `Local<'_, HashSet<EventId<MouseButtonInput>>>`, but its trait bounds were not satisfied
   --> src\interaction.rs:327:54
    |
327 |  if !old_events.contains(&id) && processed_events.insert(id) {
    |                                                   ^^^^^^
    |
   ::: D:\Programs\Rust\cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ecs-0.10.0\src\event.rs:20:1
    |
20  | pub struct EventId<E: Event> {
    | ---------------------------- doesn't satisfy `bevy::ecs::event::EventId<MouseButtonInput>: Hash`
    |
    = note: the following trait bounds were not satisfied:
            `bevy::ecs::event::EventId<MouseButtonInput>: Hash`

In the above error message, I am unable to insert an EventId object into a HashSet as MouseButtonInput does not implement Hash. As MouseButtonInput is only being used in a marker, I don't believe it should be relied upon in the Hash implementation; however the derive macro is not smart enough to know this. For this reason, I believe the traits should be implemented manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-TrivialNice and easy! A great choice to get started with Bevy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions