From 297c0a3954bb6c996e93188b685639ac69d3029a Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Thu, 8 Aug 2024 15:26:18 -0700 Subject: [PATCH] bevy_reflect: Add `DynamicSet` to `dynamic_types` example (#14665) # Objective The `dynamic_types` example was missing a reference to the newly added `DynamicSet` type. ## Solution Add `DynamicSet` to the `dynamic_types` example. For parity with the other dynamic types, I also implemented `FromIterator`, `FromIterator>`, and `IntoIterator for &DynamicSet`. ## Testing You can run the example locally: ``` cargo run --example dynamic_types ``` --- crates/bevy_reflect/src/set.rs | 42 ++++++++++++++++++++++++++++ examples/reflection/dynamic_types.rs | 27 +++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/crates/bevy_reflect/src/set.rs b/crates/bevy_reflect/src/set.rs index cde29d7ded25f..7319308d4cab9 100644 --- a/crates/bevy_reflect/src/set.rs +++ b/crates/bevy_reflect/src/set.rs @@ -381,6 +381,36 @@ impl Debug for DynamicSet { } } +impl FromIterator> for DynamicSet { + fn from_iter>>(values: I) -> Self { + let mut this = Self { + represented_type: None, + hash_table: HashTable::new(), + }; + + for value in values { + this.insert_boxed(value); + } + + this + } +} + +impl FromIterator for DynamicSet { + fn from_iter>(values: I) -> Self { + let mut this = Self { + represented_type: None, + hash_table: HashTable::new(), + }; + + for value in values { + this.insert(value); + } + + this + } +} + impl IntoIterator for DynamicSet { type Item = Box; type IntoIter = bevy_utils::hashbrown::hash_table::IntoIter; @@ -390,6 +420,18 @@ impl IntoIterator for DynamicSet { } } +impl<'a> IntoIterator for &'a DynamicSet { + type Item = &'a dyn Reflect; + type IntoIter = std::iter::Map< + bevy_utils::hashbrown::hash_table::Iter<'a, Box>, + fn(&'a Box) -> Self::Item, + >; + + fn into_iter(self) -> Self::IntoIter { + self.hash_table.iter().map(|v| v.as_reflect()) + } +} + /// Compares a [`Set`] with a [`Reflect`] value. /// /// Returns true if and only if all of the following are true: diff --git a/examples/reflection/dynamic_types.rs b/examples/reflection/dynamic_types.rs index 420481e8df2f6..6ccf65addc1fb 100644 --- a/examples/reflection/dynamic_types.rs +++ b/examples/reflection/dynamic_types.rs @@ -2,11 +2,12 @@ use bevy::reflect::{ reflect_trait, serde::TypedReflectDeserializer, std_traits::ReflectDefault, DynamicArray, - DynamicEnum, DynamicList, DynamicMap, DynamicStruct, DynamicTuple, DynamicTupleStruct, - DynamicVariant, FromReflect, Reflect, ReflectFromReflect, ReflectRef, TypeRegistry, Typed, + DynamicEnum, DynamicList, DynamicMap, DynamicSet, DynamicStruct, DynamicTuple, + DynamicTupleStruct, DynamicVariant, FromReflect, Reflect, ReflectFromReflect, ReflectRef, Set, + TypeRegistry, Typed, }; use serde::de::DeserializeSeed; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; fn main() { #[derive(Reflect, Default)] @@ -180,7 +181,19 @@ fn main() { assert_eq!(my_list, vec![1, 2, 3]); } - // 4. `DynamicMap` + // 4. `DynamicSet` + { + let mut dynamic_set = DynamicSet::from_iter(["x", "y", "z"]); + assert!(dynamic_set.contains(&"x")); + + dynamic_set.remove(&"y"); + + let mut my_set: HashSet<&str> = HashSet::new(); + my_set.apply(&dynamic_set); + assert_eq!(my_set, HashSet::from_iter(["x", "z"])); + } + + // 5. `DynamicMap` { let dynamic_map = DynamicMap::from_iter([("x", 1u32), ("y", 2u32), ("z", 3u32)]); @@ -191,7 +204,7 @@ fn main() { assert_eq!(my_map.get("z"), Some(&3)); } - // 5. `DynamicStruct` + // 6. `DynamicStruct` { #[derive(Reflect, Default, Debug, PartialEq)] struct MyStruct { @@ -210,7 +223,7 @@ fn main() { assert_eq!(my_struct, MyStruct { x: 1, y: 2, z: 3 }); } - // 6. `DynamicTupleStruct` + // 7. `DynamicTupleStruct` { #[derive(Reflect, Default, Debug, PartialEq)] struct MyTupleStruct(u32, u32, u32); @@ -225,7 +238,7 @@ fn main() { assert_eq!(my_tuple_struct, MyTupleStruct(1, 2, 3)); } - // 7. `DynamicEnum` + // 8. `DynamicEnum` { #[derive(Reflect, Default, Debug, PartialEq)] enum MyEnum {