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

bevy_reflect: support map insertion #3701

Closed
wants to merge 2 commits into from

Conversation

Davier
Copy link
Contributor

@Davier Davier commented Jan 16, 2022

Objective

Fixes #3609

Solution

  • add an insert_boxed() method on the Map trait
  • implement it for HashMap using a new FromReflect generic bound
  • add a map_apply() helper method to implement Map::apply(), that inserts new values instead of ignoring them

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jan 16, 2022
@alice-i-cecile alice-i-cecile added A-Reflection Runtime information about types C-Feature A new feature, making something new possible and removed S-Needs-Triage This issue needs to be labelled labels Jan 16, 2022
@Semihazah
Copy link

May I please have an update on the status of this merge? It's important for any scenes with components with Hashmaps.

@alice-i-cecile
Copy link
Member

So, this needs another review before it can be considered for merge. If you're comfortable with the code base (and especially if you've tested it out), feel free to leave one. Good-faith community approvals count on Bevy :)

@@ -166,7 +166,7 @@ impl<T: FromReflect> FromReflect for Vec<T> {
}
}

impl<K: Reflect + Eq + Hash, V: Reflect> Map for HashMap<K, V> {
impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
Copy link
Member

Choose a reason for hiding this comment

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

I imagine this could be a breaking change. Maybe add a Migration Guide section on the PR comment explaining that key and value types now have a FromReflect bound instead of Reflect?

let value = value.take::<V>().unwrap_or_else(|value| {
V::from_reflect(&*value).unwrap_or_else(|| {
panic!(
"Attempted to push invalid value of type {}.",
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Why is this push and not insert?

Suggested change
"Attempted to push invalid value of type {}.",
"Attempted to insert invalid value of type {}.",

Comment on lines +255 to +261
/// Applies the elements of `b` to the corresponding elements of `a`.
///
/// If a key from `b` does not exist in `a`, the value is cloned and inserted.
///
/// # Panics
///
/// This function panics if `b` is not a map.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Maybe reiterate that these two parameters are maps (as opposed to an iterator of tuples or something else).

Suggested change
/// Applies the elements of `b` to the corresponding elements of `a`.
///
/// If a key from `b` does not exist in `a`, the value is cloned and inserted.
///
/// # Panics
///
/// This function panics if `b` is not a map.
/// Applies the elements of reflected map `b` to the corresponding elements of map `a`.
///
/// If a key from `b` does not exist in `a`, the value is cloned and inserted.
///
/// # Panics
///
/// This function panics if `b` is not a reflected map.

Feel free to change my wording if preferred.

@alice-i-cecile alice-i-cecile added this to the Bevy 0.8 milestone Apr 25, 2022
@alice-i-cecile alice-i-cecile added the S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! label Jun 28, 2022
bors bot pushed a commit that referenced this pull request Jul 4, 2022
# Objective

This is a rebase of #3701 which is currently scheduled for 0.8 but is marked for adoption.

> Fixes #3609

## Solution
> - add an `insert_boxed()` method on the `Map` trait
> - implement it for `HashMap` using a new `FromReflect` generic bound
> - add a `map_apply()` helper method to implement `Map::apply()`, that inserts new values instead of ignoring them


---

## Changelog
TODO

Co-authored-by: james7132 <contact@jamessliu.com>
@NiklasEi
Copy link
Member

NiklasEi commented Jul 4, 2022

Adopted and finished in #5173

@NiklasEi NiklasEi closed this Jul 4, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this pull request Aug 8, 2022
# Objective

This is a rebase of bevyengine#3701 which is currently scheduled for 0.8 but is marked for adoption.

> Fixes bevyengine#3609

## Solution
> - add an `insert_boxed()` method on the `Map` trait
> - implement it for `HashMap` using a new `FromReflect` generic bound
> - add a `map_apply()` helper method to implement `Map::apply()`, that inserts new values instead of ignoring them


---

## Changelog
TODO

Co-authored-by: james7132 <contact@jamessliu.com>
james7132 added a commit to james7132/bevy that referenced this pull request Oct 28, 2022
# Objective

This is a rebase of bevyengine#3701 which is currently scheduled for 0.8 but is marked for adoption.

> Fixes bevyengine#3609

## Solution
> - add an `insert_boxed()` method on the `Map` trait
> - implement it for `HashMap` using a new `FromReflect` generic bound
> - add a `map_apply()` helper method to implement `Map::apply()`, that inserts new values instead of ignoring them


---

## Changelog
TODO

Co-authored-by: james7132 <contact@jamessliu.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

This is a rebase of bevyengine#3701 which is currently scheduled for 0.8 but is marked for adoption.

> Fixes bevyengine#3609

## Solution
> - add an `insert_boxed()` method on the `Map` trait
> - implement it for `HashMap` using a new `FromReflect` generic bound
> - add a `map_apply()` helper method to implement `Map::apply()`, that inserts new values instead of ignoring them


---

## Changelog
TODO

Co-authored-by: james7132 <contact@jamessliu.com>
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 S-Adopt-Me The original PR author has no intent to complete this work. Pick me up!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants