forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_reflection.rs
33 lines (27 loc) · 957 Bytes
/
generic_reflection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Demonstrates how reflection is used with generic Rust types.
use bevy::prelude::*;
use std::any::TypeId;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// You must manually register each instance of a generic type
.register_type::<MyType<u32>>()
.add_systems(Startup, setup)
.run();
}
/// The `#[derive(Reflect)]` macro will automatically add any required bounds to `T`,
/// such as `Reflect` and `GetTypeRegistration`.
#[derive(Reflect)]
struct MyType<T> {
value: T,
}
fn setup(type_registry: Res<AppTypeRegistry>) {
let type_registry = type_registry.read();
let registration = type_registry.get(TypeId::of::<MyType<u32>>()).unwrap();
info!(
"Registration for {} exists",
registration.type_info().type_path(),
);
// MyType<String> was not manually registered, so it does not exist
assert!(type_registry.get(TypeId::of::<MyType<String>>()).is_none());
}