Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bevy_reflect: Add dynamic type data access and iteration to `TypeRegi…
…stration` (#15347) # Objective There's currently no way to iterate through all the type data in a `TypeRegistration`. While these are all type-erased, it can still be useful to see what types (by `TypeId`) are registered for a given type. Additionally, it might be good to have ways of dynamically working with `TypeRegistration`. ## Solution Added a way to iterate through all type data on a given `TypeRegistration`. This PR also adds methods for working with type data dynamically as well as methods for conveniently checking if a given type data exists on the registration. I also took this opportunity to reorganize the methods on `TypeRegistration` as it has always bothered me haha (i.e. the constructor not being at the top, etc.). ## Testing You can test locally by running: ``` cargo test --package bevy_reflect ``` --- ## Showcase The type-erased type data on a `TypeRegistration` can now be iterated! ```rust #[derive(Reflect)] struct Foo; #[derive(Clone)] struct DataA(i32); #[derive(Clone)] struct DataB(i32); let mut registration = TypeRegistration::of::<Foo>(); registration.insert(DataA(123)); registration.insert(DataB(456)); let mut iter = registration.iter(); let (id, data) = iter.next().unwrap(); assert_eq!(id, TypeId::of::<DataA>()); assert_eq!(data.downcast_ref::<DataA>().unwrap().0, 123); let (id, data) = iter.next().unwrap(); assert_eq!(id, TypeId::of::<DataB>()); assert_eq!(data.downcast_ref::<DataB>().unwrap().0, 456); assert!(iter.next().is_none()); ```
- Loading branch information