Trait bevy_reflect::Typed
source · pub trait Typed: Reflect + TypePath {
// Required method
fn type_info() -> &'static TypeInfo;
}
Expand description
A static accessor to compile-time type information.
This trait is automatically implemented by the #[derive(Reflect)]
macro
and allows type information to be processed without an instance of that type.
§Implementing
While it is recommended to leave implementing this trait to the #[derive(Reflect)]
macro,
it is possible to implement this trait manually. If a manual implementation is needed,
you must ensure that the information you provide is correct, otherwise various systems that
rely on this trait may fail in unexpected ways.
Implementors may have difficulty in generating a reference to TypeInfo
with a static
lifetime. Luckily, this crate comes with some utility structs, to make generating these
statics much simpler.
§Example
use bevy_reflect::Typed;
struct MyStruct {
foo: usize,
bar: (f32, f32)
}
impl Typed for MyStruct {
fn type_info() -> &'static TypeInfo {
static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
CELL.get_or_set(|| {
let fields = [
NamedField::new::<usize >("foo"),
NamedField::new::<(f32, f32) >("bar"),
];
let info = StructInfo::new::<Self>(&fields);
TypeInfo::Struct(info)
})
}
}
Required Methods§
Object Safety§
This trait is not object safe.