Skip to content
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

Add get_at_mut to Map trait #8596

Closed
PROMETHIA-27 opened this issue May 11, 2023 · 6 comments · Fixed by #8691
Closed

Add get_at_mut to Map trait #8596

PROMETHIA-27 opened this issue May 11, 2023 · 6 comments · Fixed by #8691
Labels
A-Reflection Runtime information about types C-Feature A new feature, making something new possible D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@PROMETHIA-27
Copy link
Contributor

What problem does this solve or what need does it fill?

Currently the solution to getting a mutable reference to the key/value at an index of a map is to get the key and value immutably, clone the key, then use that to index the map. This is unnecessarily inefficient and could easily be avoided.

What solution would you like?

A get_at_mut method that works like get_at, but returns mutable references.

I might PR this myself later if I remember.

@PROMETHIA-27 PROMETHIA-27 added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled A-Reflection Runtime information about types labels May 11, 2023
@MrGVSV MrGVSV removed the S-Needs-Triage This issue needs to be labelled label May 11, 2023
@MrGVSV
Copy link
Member

MrGVSV commented May 11, 2023

I assume this is for ordered maps? In which case, I think it makes sense to add these kinds of methods, but with a disclaimer that not all maps guarantee a particular order.

@PROMETHIA-27
Copy link
Contributor Author

Technically this is for any map actually; which makes me realize we need .iter_mut() as well to do what I'm doing properly, as I'm just relying on unordered maps being stable.

@MrGVSV
Copy link
Member

MrGVSV commented May 20, 2023

Oops I completely missed the fact that we already have Map::get_at (even though you mention it directly in the description).

@MrGVSV MrGVSV added the D-Trivial Nice and easy! A great choice to get started with Bevy label May 20, 2023
@TheBlek
Copy link
Contributor

TheBlek commented May 26, 2023

Hello! I want to tackle this problem. I'm new here, so any help would be really appreciated @MrGVSV.

Here's what I've been able to gather so far:

  • There seems to be only two implementers of this trait in bevy: utils::hashbrown::HashMap (actually implemented through a macro) and reflect::DynamicMap
  • While it is possible to implement (&mut key, &mut value) for DynamicMap, however HashMap seems to provide only (& key, &mut value) iterator to use for an implementation analogous to get_at

So my question is:
Does Map::get_mut_at really need to provide mutable reference to a key? (I wonder if changing a key through such a reference could incur invalidation of HashMap state)

@MrGVSV
Copy link
Member

MrGVSV commented May 26, 2023

Hello! I want to tackle this problem. I'm new here, so any help would be really appreciated @MrGVSV.

Awesome! Yeah feel free to leave questions in #reflection-dev on Discord if you have any. Always great to have new contributors!

Does Map::get_mut_at really need to provide mutable reference to a key? (I wonder if changing a key through such a reference could incur invalidation of HashMap state)

Good question! I don't think we should make the key mutable. I agree that that makes things a lot more complicated. On top of that, HashMap::iter_mut like you mention only provides (&K, &mut V). So we can use that as an indicator that the key should probably be immutable.

@djeedai
Copy link
Contributor

djeedai commented May 27, 2023

Mutable key on a hash map is plain wrong. HashMap is correct here.

alice-i-cecile pushed a commit that referenced this issue Jun 2, 2023
# Objective

Fixes #8596 

## Solution

Change interface of the trait Map. Adjust implementations of this trait

---

## Changelog

### Changed
- Interface of Map trait

### Added
- `Map::get_at_mut`

## Migration Guide

Every implementor of Map trait would need to implement `get_at_mut`.
Which, judging by changes in this PR, should be fairly trivial.
@github-project-automation github-project-automation bot moved this from Open to Done in Reflection Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Reflection Runtime information about types C-Feature A new feature, making something new possible D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants