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 bevy_platform::collections::hash_map::Entry as TypeIdHashMapEntry;
11
12pub use indexmap::map::Entry as TypeIdIndexMapEntry;
14
15#[deprecated(
17 since = "0.20.0",
18 note = "use `TypeIdHashMapEntry` or `TypeIdIndexMapEntry` instead"
19)]
20pub use indexmap::map::Entry as TypeIdMapEntry;
21
22pub type PreHashMap<K, V> = HashMap<Hashed<K>, V, PassHash>;
25
26pub trait PreHashMapExt<K, V> {
28 fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V;
32}
33
34impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K, V> {
35 #[inline]
36 fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V {
37 use bevy_platform::collections::hash_map::RawEntryMut;
38 let entry = self
39 .raw_entry_mut()
40 .from_key_hashed_nocheck(key.hash(), key);
41 match entry {
42 RawEntryMut::Occupied(entry) => entry.into_mut(),
43 RawEntryMut::Vacant(entry) => {
44 let (_, value) = entry.insert_hashed_nocheck(key.hash(), key.clone(), func());
45 value
46 }
47 }
48 }
49}
50
51pub type TypeIdHashMap<V> = HashMap<TypeId, V, NoOpHash>;
53
54pub type TypeIdIndexMap<V> = IndexMap<TypeId, V, NoOpHash>;
57
58#[deprecated(
60 since = "0.20.0",
61 note = "use `TypeIdHashMap` or `TypeIdIndexMap` instead"
62)]
63pub type TypeIdMap<V> = TypeIdIndexMap<V>;
64
65pub trait TypeIdMapExt<V> {
89 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
94
95 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
97
98 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
100
101 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
104
105 fn entry_type<T: ?Sized + 'static>(&mut self) -> TypeIdIndexMapEntry<'_, TypeId, V>;
107}
108
109impl<V> TypeIdMapExt<V> for TypeIdIndexMap<V> {
110 #[inline]
111 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
112 self.insert(TypeId::of::<T>(), v)
113 }
114
115 #[inline]
116 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
117 self.get(&TypeId::of::<T>())
118 }
119
120 #[inline]
121 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
122 self.get_mut(&TypeId::of::<T>())
123 }
124
125 #[inline]
126 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
127 self.shift_remove(&TypeId::of::<T>())
128 }
129
130 #[inline]
131 fn entry_type<T: ?Sized + 'static>(&mut self) -> TypeIdIndexMapEntry<'_, TypeId, V> {
132 self.entry(TypeId::of::<T>())
133 }
134}
135
136pub trait TypeIdHashMapExt<V> {
138 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
143
144 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
146
147 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
149
150 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
153
154 fn entry_type<T: ?Sized + 'static>(
156 &mut self,
157 ) -> TypeIdHashMapEntry<'_, TypeId, V, NoOpHash>;
158}
159
160impl<V> TypeIdHashMapExt<V> for TypeIdHashMap<V> {
161 #[inline]
162 fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
163 self.insert(TypeId::of::<T>(), v)
164 }
165
166 #[inline]
167 fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
168 self.get(&TypeId::of::<T>())
169 }
170
171 #[inline]
172 fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
173 self.get_mut(&TypeId::of::<T>())
174 }
175
176 #[inline]
177 fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
178 self.remove(&TypeId::of::<T>())
179 }
180
181 #[inline]
182 fn entry_type<T: ?Sized + 'static>(
183 &mut self,
184 ) -> TypeIdHashMapEntry<'_, TypeId, V, NoOpHash> {
185 self.entry(TypeId::of::<T>())
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192 use static_assertions::assert_impl_all;
193
194 assert_impl_all!(PreHashMap::<u64, usize>: Clone);
196
197 #[test]
198 fn fast_typeid_hash() {
199 struct Hasher;
200
201 impl core::hash::Hasher for Hasher {
202 fn finish(&self) -> u64 {
203 0
204 }
205 fn write(&mut self, _: &[u8]) {
206 panic!("Hashing of core::any::TypeId changed");
207 }
208 fn write_u64(&mut self, _: u64) {}
209 }
210
211 Hash::hash(&TypeId::of::<()>(), &mut Hasher);
212 }
213
214 crate::cfg::alloc! {
215 #[test]
216 fn stable_hash_within_same_program_execution() {
217 use alloc::vec::Vec;
218
219 let mut map_1 = <HashMap<_, _>>::default();
220 let mut map_2 = <HashMap<_, _>>::default();
221 for i in 1..10 {
222 map_1.insert(i, i);
223 map_2.insert(i, i);
224 }
225 assert_eq!(
226 map_1.iter().collect::<Vec<_>>(),
227 map_2.iter().collect::<Vec<_>>()
228 );
229 }
230 }
231}