
# Objective Now that `bevy_platform::cfg` is merged, we can start tidying up features. This PR starts with `bevy_utils`. ## Solution - Removed `serde` and `critical-section` features (they were just re-exports of `bevy_platform` anyway) - Removed `std`, `alloc` features, relying on `bevy_platform::cfg` to check for availability. - Added `parallel` feature to provide access to the `Parallel` type. - Moved the `HashMap` type aliases into `map.rs` for better organisation. ## Testing - CI
83 lines
2.7 KiB
Rust
83 lines
2.7 KiB
Rust
use core::{any::TypeId, hash::Hash};
|
|
|
|
use bevy_platform::{
|
|
collections::HashMap,
|
|
hash::{Hashed, NoOpHash, PassHash},
|
|
};
|
|
|
|
/// A [`HashMap`] pre-configured to use [`Hashed`] keys and [`PassHash`] passthrough hashing.
|
|
/// Iteration order only depends on the order of insertions and deletions.
|
|
pub type PreHashMap<K, V> = HashMap<Hashed<K>, V, PassHash>;
|
|
|
|
/// Extension methods intended to add functionality to [`PreHashMap`].
|
|
pub trait PreHashMapExt<K, V> {
|
|
/// Tries to get or insert the value for the given `key` using the pre-computed hash first.
|
|
/// If the [`PreHashMap`] does not already contain the `key`, it will clone it and insert
|
|
/// the value returned by `func`.
|
|
fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V;
|
|
}
|
|
|
|
impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K, V> {
|
|
#[inline]
|
|
fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V {
|
|
use bevy_platform::collections::hash_map::RawEntryMut;
|
|
let entry = self
|
|
.raw_entry_mut()
|
|
.from_key_hashed_nocheck(key.hash(), key);
|
|
match entry {
|
|
RawEntryMut::Occupied(entry) => entry.into_mut(),
|
|
RawEntryMut::Vacant(entry) => {
|
|
let (_, value) = entry.insert_hashed_nocheck(key.hash(), key.clone(), func());
|
|
value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A specialized hashmap type with Key of [`TypeId`]
|
|
/// Iteration order only depends on the order of insertions and deletions.
|
|
pub type TypeIdMap<V> = HashMap<TypeId, V, NoOpHash>;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use static_assertions::assert_impl_all;
|
|
|
|
// Check that the HashMaps are Clone if the key/values are Clone
|
|
assert_impl_all!(PreHashMap::<u64, usize>: Clone);
|
|
|
|
#[test]
|
|
fn fast_typeid_hash() {
|
|
struct Hasher;
|
|
|
|
impl core::hash::Hasher for Hasher {
|
|
fn finish(&self) -> u64 {
|
|
0
|
|
}
|
|
fn write(&mut self, _: &[u8]) {
|
|
panic!("Hashing of core::any::TypeId changed");
|
|
}
|
|
fn write_u64(&mut self, _: u64) {}
|
|
}
|
|
|
|
Hash::hash(&TypeId::of::<()>(), &mut Hasher);
|
|
}
|
|
|
|
crate::cfg::alloc! {
|
|
#[test]
|
|
fn stable_hash_within_same_program_execution() {
|
|
use alloc::vec::Vec;
|
|
|
|
let mut map_1 = <HashMap<_, _>>::default();
|
|
let mut map_2 = <HashMap<_, _>>::default();
|
|
for i in 1..10 {
|
|
map_1.insert(i, i);
|
|
map_2.insert(i, i);
|
|
}
|
|
assert_eq!(
|
|
map_1.iter().collect::<Vec<_>>(),
|
|
map_2.iter().collect::<Vec<_>>()
|
|
);
|
|
}
|
|
}
|
|
} |