Skip to content

Add TypeIdMapExt trait to make TypeIdMap operations more ergonomic #19683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

theotherphil
Copy link
Contributor

@theotherphil theotherphil commented Jun 16, 2025

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.

@theotherphil theotherphil requested a review from Shatur June 16, 2025 18:30
@theotherphil theotherphil added D-Trivial Nice and easy! A great choice to get started with Bevy S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 16, 2025
@@ -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.
///
/// # Examples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example is a bit too explicit 🤔 I would probably remove it, all methods are extremely simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s worth having a simple example of what this is doing, but maybe mot one that uses all of the new functions - I’ll cut this down after adding entry and entry_mut.

/// map.remove_type::<MyType>();
/// assert_eq!(map.len(), 0);
/// ```
pub trait TypeIdMapExt<V> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add entry/entry_mut as well?

@theotherphil theotherphil added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-Trivial Nice and easy! A great choice to get started with Bevy S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an extension trait for TypeIdMap to query values using generics
2 participants