Skip to content

Commit 201e386

Browse files
committed
Use Custom Hash Function For ComponentId
1 parent 12f6831 commit 201e386

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

crates/bevy_ecs/hecs/src/world.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use bevy_utils::{HashMap, HashSet};
2222
use core::{
2323
any::TypeId,
2424
cmp::{Ord, Ordering},
25-
fmt, mem, ptr,
25+
fmt,
26+
hash::{Hash, Hasher},
27+
mem, ptr,
2628
};
2729

2830
#[cfg(feature = "std")]
@@ -915,7 +917,8 @@ impl From<MissingComponent> for ComponentError {
915917

916918
/// Uniquely identifies a type of component. This is conceptually similar to
917919
/// Rust's [`TypeId`], but allows for external type IDs to be defined.
918-
#[derive(Eq, PartialEq, Hash, Debug, Clone, Copy)]
920+
#[allow(clippy::derive_hash_xor_eq)] // Fine because we uphold k1 == k2 ⇒ hash(k1) == hash(k2)
921+
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
919922
pub enum ComponentId {
920923
/// A Rust-native [`TypeId`]
921924
RustTypeId(TypeId),
@@ -924,6 +927,23 @@ pub enum ComponentId {
924927
ExternalId(u64),
925928
}
926929

930+
impl Hash for ComponentId {
931+
fn hash<H: Hasher>(&self, state: &mut H) {
932+
match self {
933+
ComponentId::RustTypeId(id) => {
934+
id.hash(state);
935+
// Write a byte to distinguish the variants
936+
state.write_u8(0);
937+
}
938+
ComponentId::ExternalId(id) => {
939+
state.write_u64(*id);
940+
// Write a byte to distinguish the variants
941+
state.write_u8(1);
942+
}
943+
}
944+
}
945+
}
946+
927947
impl Default for ComponentId {
928948
fn default() -> Self {
929949
ComponentId::ExternalId(std::u64::MAX)

0 commit comments

Comments
 (0)