bevy/examples/reflection/generic_reflection.rs

29 lines
817 B
Rust
Raw Normal View History

//! Demonstrates how reflection is used with generic Rust types.
use bevy::{prelude::*, reflect::TypeRegistry};
2020-11-28 00:39:59 +00:00
use std::any::TypeId;
fn main() {
App::new()
2020-11-28 00:39:59 +00:00
.add_plugins(DefaultPlugins)
// You must manually register each instance of a generic type
2020-11-28 00:39:59 +00:00
.register_type::<MyType<u32>>()
.add_startup_system(setup)
2020-11-28 00:39:59 +00:00
.run();
}
#[derive(Reflect)]
struct MyType<T: Reflect> {
value: T,
}
fn setup(type_registry: Res<TypeRegistry>) {
let type_registry = type_registry.read();
let registration = type_registry.get(TypeId::of::<MyType<u32>>()).unwrap();
info!("Registration for {} exists", registration.short_name());
2020-11-28 00:39:59 +00:00
// MyType<String> was not manually registered, so it does not exist
assert!(type_registry.get(TypeId::of::<MyType<String>>()).is_none());
}