Skip to content

Commit

Permalink
bevy_reflect: Add DynamicSet to dynamic_types example (bevyengine…
Browse files Browse the repository at this point in the history
…#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<T: Reflect>`, `FromIterator<Box<dyn Reflect>>`, and
`IntoIterator for &DynamicSet`.

## Testing

You can run the example locally:

```
cargo run --example dynamic_types
```
  • Loading branch information
MrGVSV authored Aug 8, 2024
1 parent aeef1c0 commit 297c0a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
42 changes: 42 additions & 0 deletions crates/bevy_reflect/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,36 @@ impl Debug for DynamicSet {
}
}

impl FromIterator<Box<dyn Reflect>> for DynamicSet {
fn from_iter<I: IntoIterator<Item = Box<dyn Reflect>>>(values: I) -> Self {
let mut this = Self {
represented_type: None,
hash_table: HashTable::new(),
};

for value in values {
this.insert_boxed(value);
}

this
}
}

impl<T: Reflect> FromIterator<T> for DynamicSet {
fn from_iter<I: IntoIterator<Item = T>>(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<dyn Reflect>;
type IntoIter = bevy_utils::hashbrown::hash_table::IntoIter<Self::Item>;
Expand All @@ -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<dyn Reflect>>,
fn(&'a Box<dyn Reflect>) -> 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:
Expand Down
27 changes: 20 additions & 7 deletions examples/reflection/dynamic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]);

Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down

0 comments on commit 297c0a3

Please sign in to comment.