1use core::{any::TypeId, hash::Hash};
2
3use bevy_platform::{
4 collections::HashMap,
5 hash::{Hashed, NoOpHash, PassHash},
6};
7use indexmap::map::IndexMap;
8
9pub use indexmap::map::Entry as TypeIdMapEntry;
11
12pub type PreHashMap<K, V> = HashMap<Hashed<K>, V, PassHash>;
15
16pub trait PreHashMapExt<K, V> {
18 fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V;
22}
23
24impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K, V> {
25 #[inline]
26 fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V {
27 use bevy_platform::collections::hash_map::RawEntryMut;
28 let entry = self
29 .raw_entry_mut()
30 .from_key_hashed_nocheck(key.hash(), key);
31 match entry {
32 RawEntryMut::Occupied(entry) => entry.into_mut(),
33 RawEntryMut::Vacant(entry) => {
34 let (_, value) = entry.insert_hashed_nocheck(key.hash(), key.clone(), func());
35 value
36 }
37 }
38 }
39}
40
41pub type TypeIdMap<V> = IndexMap<TypeId, V, NoOpHash>;
44
45pub trait TypeIdMapExt<V> {
70 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
75
76 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
78
79 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
81
82 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
85
86 fn entry_type<T: ?Sized + 'static>(&mut self) -> TypeIdMapEntry<'_, TypeId, V>;
88}
89
90impl<V> TypeIdMapExt<V> for TypeIdMap<V> {
91 #[inline]
92 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
93 self.insert(TypeId::of::<T>(), v)
94 }
95
96 #[inline]
97 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
98 self.get(&TypeId::of::<T>())
99 }
100
101 #[inline]
102 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
103 self.get_mut(&TypeId::of::<T>())
104 }
105
106 #[inline]
107 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
108 self.shift_remove(&TypeId::of::<T>())
109 }
110
111 #[inline]
112 fn entry_type<T: ?Sized + 'static>(&mut self) -> TypeIdMapEntry<'_, TypeId, V> {
113 self.entry(TypeId::of::<T>())
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120 use static_assertions::assert_impl_all;
121
122 assert_impl_all!(PreHashMap::<u64, usize>: Clone);
124
125 #[test]
126 fn fast_typeid_hash() {
127 struct Hasher;
128
129 impl core::hash::Hasher for Hasher {
130 fn finish(&self) -> u64 {
131 0
132 }
133 fn write(&mut self, _: &[u8]) {
134 panic!("Hashing of core::any::TypeId changed");
135 }
136 fn write_u64(&mut self, _: u64) {}
137 }
138
139 Hash::hash(&TypeId::of::<()>(), &mut Hasher);
140 }
141
142 crate::cfg::alloc! {
143 #[test]
144 fn stable_hash_within_same_program_execution() {
145 use alloc::vec::Vec;
146
147 let mut map_1 = <HashMap<_, _>>::default();
148 let mut map_2 = <HashMap<_, _>>::default();
149 for i in 1..10 {
150 map_1.insert(i, i);
151 map_2.insert(i, i);
152 }
153 assert_eq!(
154 map_1.iter().collect::<Vec<_>>(),
155 map_2.iter().collect::<Vec<_>>()
156 );
157 }
158 }
159}