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 bevy_reflect::Map trait #8691

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ macro_rules! impl_reflect_for_hashmap {
.map(|(key, value)| (key as &dyn Reflect, value as &dyn Reflect))
}

fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)> {
self.iter_mut()
.nth(index)
.map(|(key, value)| (key as &dyn Reflect, value as &mut dyn Reflect))
}

fn len(&self) -> usize {
Self::len(self)
}
Expand Down
65 changes: 65 additions & 0 deletions crates/bevy_reflect/src/map.rs
Copy link
Member

@MrGVSV MrGVSV May 27, 2023

Choose a reason for hiding this comment

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

Could we also add a test that ensures get_at_mut is working as intended?

Edit: I guess we don't actually have one for get_at 😅. Would you be able to add one for that as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I tried to write tests as helpful as possible, but still not sure whether they are. Looking forward to your feedback on that

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub trait Map: Reflect {
/// Returns the key-value pair at `index` by reference, or `None` if out of bounds.
fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)>;

/// Returns the key-value pair at `index` by reference where the value is a mutable reference, or `None` if out of bounds.
fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)>;

/// Returns the number of elements in the map.
fn len(&self) -> usize;

Expand Down Expand Up @@ -257,6 +260,12 @@ impl Map for DynamicMap {
.map(|(key, value)| (&**key, &**value))
}

fn get_at_mut(&mut self, index: usize) -> Option<(&dyn Reflect, &mut dyn Reflect)> {
self.values
.get_mut(index)
.map(|(key, value)| (&**key, &mut **value))
}

fn insert_boxed(
&mut self,
key: Box<dyn Reflect>,
Expand Down Expand Up @@ -491,6 +500,8 @@ pub fn map_apply<M: Map>(a: &mut M, b: &dyn Reflect) {
#[cfg(test)]
mod tests {
use super::DynamicMap;
use super::Map;
use crate::reflect::Reflect;

#[test]
fn test_into_iter() {
Expand All @@ -511,4 +522,58 @@ mod tests {
assert_eq!(expected[index], value);
}
}

#[test]
fn test_map_get_at() {
let values = ["first", "second", "third"];
let mut map = DynamicMap::default();
map.insert(0usize, values[0].to_string());
map.insert(1usize, values[1].to_string());
map.insert(1usize, values[2].to_string());

let (key_r, value_r) = map.get_at(1).expect("Item wasn't found");
let value = value_r
.downcast_ref::<String>()
.expect("Couldn't downcast to String");
let key = key_r
.downcast_ref::<usize>()
.expect("Couldn't downcast to usize");
assert_eq!(key, &1usize);
assert_eq!(value, &values[2].to_owned());

assert!(map.get_at(2).is_none());
map.remove(&1usize as &dyn Reflect);
assert!(map.get_at(1).is_none());
}

#[test]
fn test_map_get_at_mut() {
let values = ["first", "second", "third"];
let mut map = DynamicMap::default();
map.insert(0usize, values[0].to_string());
map.insert(1usize, values[1].to_string());
map.insert(1usize, values[2].to_string());

let (key_r, value_r) = map.get_at_mut(1).expect("Item wasn't found");
let value = value_r
.downcast_mut::<String>()
.expect("Couldn't downcast to String");
let key = key_r
.downcast_ref::<usize>()
.expect("Couldn't downcast to usize");
assert_eq!(key, &1usize);
assert_eq!(value, &mut values[2].to_owned());

*value = values[0].to_owned();

assert_eq!(
map.get(&1usize as &dyn Reflect)
.expect("Item wasn't found")
.downcast_ref::<String>()
.expect("Couldn't downcast to String"),
&values[0].to_owned()
);

assert!(map.get_at(2).is_none());
}
}