Add TypeIdMapExt trait to make TypeIdMap operations more ergonomic (#19683)

# Objective
 
Fix https://github.com/bevyengine/bevy/issues/19642 by enabling e.g.

```
map.get_type::<MyType>();
```

in place of

```
map.get(&TypeId::of::<MyType>());
```

## Solution

Add an extension trait `TypeIdMapExt` with `insert_type`, `get_type`,
`get_type_mut` and `remove_type` counterparts for `insert`, `get`,
`get_mut` and `remove`.

## Testing

Doc test.
This commit is contained in:
theotherphil 2025-06-17 22:06:27 +01:00 committed by GitHub
parent 1079b83af9
commit 66086a2616
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,7 @@
use core::{any::TypeId, hash::Hash};
use bevy_platform::{
collections::HashMap,
collections::{hash_map::Entry, HashMap},
hash::{Hashed, NoOpHash, PassHash},
};
@ -38,6 +38,78 @@ impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K,
/// Iteration order only depends on the order of insertions and deletions.
pub type TypeIdMap<V> = HashMap<TypeId, V, NoOpHash>;
/// Extension trait to make use of [`TypeIdMap`] more ergonomic.
///
/// Each function on this trait is a trivial wrapper for a function
/// on [`HashMap`], replacing a `TypeId` key with a
/// generic parameter `T`.
///
/// # Examples
///
/// ```rust
/// # use std::any::TypeId;
/// # use bevy_utils::TypeIdMap;
/// use bevy_utils::TypeIdMapExt;
///
/// struct MyType;
///
/// // Using the built-in `HashMap` functions requires manually looking up `TypeId`s.
/// let mut map = TypeIdMap::default();
/// map.insert(TypeId::of::<MyType>(), 7);
/// assert_eq!(map.get(&TypeId::of::<MyType>()), Some(&7));
///
/// // Using `TypeIdMapExt` functions does the lookup for you.
/// map.insert_type::<MyType>(7);
/// assert_eq!(map.get_type::<MyType>(), Some(&7));
/// ```
pub trait TypeIdMapExt<V> {
/// Inserts a value for the type `T`.
///
/// If the map did not previously contain this key then [`None`] is returned,
/// otherwise the value for this key is updated and the old value returned.
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
/// Returns a reference to the value for type `T`, if one exists.
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
/// Returns a mutable reference to the value for type `T`, if one exists.
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
/// Removes type `T` from the map, returning the value for this
/// key if it was previously present.
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
/// Gets the type `T`'s entry in the map for in-place manipulation.
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash>;
}
impl<V> TypeIdMapExt<V> for TypeIdMap<V> {
#[inline]
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
self.insert(TypeId::of::<T>(), v)
}
#[inline]
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
self.get(&TypeId::of::<T>())
}
#[inline]
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
self.get_mut(&TypeId::of::<T>())
}
#[inline]
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
self.remove(&TypeId::of::<T>())
}
#[inline]
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash> {
self.entry(TypeId::of::<T>())
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -67,7 +139,7 @@ mod tests {
#[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 {