Skip to content

Commit 66086a2

Browse files
authored
Add TypeIdMapExt trait to make TypeIdMap operations more ergonomic (#19683)
# Objective Fix #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.
1 parent 1079b83 commit 66086a2

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

crates/bevy_utils/src/map.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::{any::TypeId, hash::Hash};
22

33
use bevy_platform::{
4-
collections::HashMap,
4+
collections::{hash_map::Entry, HashMap},
55
hash::{Hashed, NoOpHash, PassHash},
66
};
77

@@ -38,6 +38,78 @@ impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K,
3838
/// Iteration order only depends on the order of insertions and deletions.
3939
pub type TypeIdMap<V> = HashMap<TypeId, V, NoOpHash>;
4040

41+
/// Extension trait to make use of [`TypeIdMap`] more ergonomic.
42+
///
43+
/// Each function on this trait is a trivial wrapper for a function
44+
/// on [`HashMap`], replacing a `TypeId` key with a
45+
/// generic parameter `T`.
46+
///
47+
/// # Examples
48+
///
49+
/// ```rust
50+
/// # use std::any::TypeId;
51+
/// # use bevy_utils::TypeIdMap;
52+
/// use bevy_utils::TypeIdMapExt;
53+
///
54+
/// struct MyType;
55+
///
56+
/// // Using the built-in `HashMap` functions requires manually looking up `TypeId`s.
57+
/// let mut map = TypeIdMap::default();
58+
/// map.insert(TypeId::of::<MyType>(), 7);
59+
/// assert_eq!(map.get(&TypeId::of::<MyType>()), Some(&7));
60+
///
61+
/// // Using `TypeIdMapExt` functions does the lookup for you.
62+
/// map.insert_type::<MyType>(7);
63+
/// assert_eq!(map.get_type::<MyType>(), Some(&7));
64+
/// ```
65+
pub trait TypeIdMapExt<V> {
66+
/// Inserts a value for the type `T`.
67+
///
68+
/// If the map did not previously contain this key then [`None`] is returned,
69+
/// otherwise the value for this key is updated and the old value returned.
70+
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
71+
72+
/// Returns a reference to the value for type `T`, if one exists.
73+
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
74+
75+
/// Returns a mutable reference to the value for type `T`, if one exists.
76+
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
77+
78+
/// Removes type `T` from the map, returning the value for this
79+
/// key if it was previously present.
80+
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
81+
82+
/// Gets the type `T`'s entry in the map for in-place manipulation.
83+
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash>;
84+
}
85+
86+
impl<V> TypeIdMapExt<V> for TypeIdMap<V> {
87+
#[inline]
88+
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
89+
self.insert(TypeId::of::<T>(), v)
90+
}
91+
92+
#[inline]
93+
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
94+
self.get(&TypeId::of::<T>())
95+
}
96+
97+
#[inline]
98+
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
99+
self.get_mut(&TypeId::of::<T>())
100+
}
101+
102+
#[inline]
103+
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
104+
self.remove(&TypeId::of::<T>())
105+
}
106+
107+
#[inline]
108+
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash> {
109+
self.entry(TypeId::of::<T>())
110+
}
111+
}
112+
41113
#[cfg(test)]
42114
mod tests {
43115
use super::*;
@@ -67,7 +139,7 @@ mod tests {
67139
#[test]
68140
fn stable_hash_within_same_program_execution() {
69141
use alloc::vec::Vec;
70-
142+
71143
let mut map_1 = <HashMap<_, _>>::default();
72144
let mut map_2 = <HashMap<_, _>>::default();
73145
for i in 1..10 {

0 commit comments

Comments
 (0)