Path: blob/main/examples/reflection/generic_reflection.rs
6849 views
//! Demonstrates how reflection is used with generic Rust types.12use bevy::prelude::*;3use std::any::TypeId;45fn main() {6App::new()7.add_plugins(DefaultPlugins)8// You must manually register each instance of a generic type9.register_type::<MyType<u32>>()10.add_systems(Startup, setup)11.run();12}1314/// The `#[derive(Reflect)]` macro will automatically add any required bounds to `T`,15/// such as `Reflect` and `GetTypeRegistration`.16#[derive(Reflect)]17struct MyType<T> {18value: T,19}2021fn setup(type_registry: Res<AppTypeRegistry>) {22let type_registry = type_registry.read();2324let registration = type_registry.get(TypeId::of::<MyType<u32>>()).unwrap();25info!(26"Registration for {} exists",27registration.type_info().type_path(),28);2930// MyType<String> was not manually registered, so it does not exist31assert!(type_registry.get(TypeId::of::<MyType<String>>()).is_none());32}333435