Skip to content

Reflect::hash collides with Hash::hash #943

@CleanCut

Description

@CleanCut

With the merging of @cart's #926, the hash method of bevy::prelude::Reflect now collides with the hash method from std::hash::Hash. You can workaround by disambiguating, so this isn't a showstopper. I'm reporting it in the hopes that there's an easy tweak to avoid having to do the workaround. If not, no big deal - and I'm fine closing this.

With the following imports:

use bevy::prelude::*;
use std::hash::Hash;

When you attempt to call the hash method on a primitive value like an integer (which is something you might want to do within an implementation of the Hash trait for a struct, such as on line 59 here -- PlayerID is a newtype over usize):

https://github.com/CleanCut/punchball/blob/9f8ddb2de2b8a91629307ee81b3f64aaa1eed1de/src/player/collision.rs#L52-L62

You get this error:

error[E0034]: multiple applicable items in scope
  --> src/player/collision.rs:58:23
   |
58 |             player_id.hash(state);
   |                       ^^^^ multiple `hash` found
   |
   = note: candidate #1 is defined in an impl of the trait `Hash` for the type `usize`
   = note: candidate #2 is defined in an impl of the trait `bevy::prelude::Reflect` for the type `usize`
help: disambiguate the associated function for candidate #1
   |
58 |             Hash::hash(&player_id, state);
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: disambiguate the associated function for candidate #2
   |
58 |             bevy::prelude::Reflect::hash(&player_id, state);
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0034`.

The workaround is to change player_id.hash(state); to Hash::hash(&player_id, state); to disambiguate.

https://github.com/CleanCut/punchball/blob/a6abd16d239fb7c6ea2656990c6da16056d4ffce/src/player/collision.rs#L52-L62

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-Code-QualityA section of code that is hard to understand or change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions