Bevy Reflect
This crate enables you to dynamically interact with Rust types:
- Derive the Reflect traits
- Interact with fields using their names (for named structs) or indices (for tuple structs)
- "Patch" your types with new values
- Look up nested fields using "path strings"
- Iterate over struct fields
- Automatically serialize and deserialize via Serde (without explicit serde impls)
- Trait "reflection"
Features
Derive the Reflect traits
// this will automatically implement the Reflect trait and the Struct trait (because the type is a struct)
// this will automatically implement the Reflect trait and the TupleStruct trait (because the type is a tuple struct)
;
// We will use this value to illustrate `bevy_reflect` features
let mut foo = Foo ;
Interact with fields using their names
assert_eq!;
*foo..unwrap = 2;
assert_eq!;
"Patch" your types with new values
let mut dynamic_struct = default;
dynamic_struct.insert;
dynamic_struct.insert;
foo.apply;
assert_eq!;
assert_eq!;
Look up nested fields using "path strings"
let value = *foo..unwrap;
assert_eq!;
Iterate over struct fields
for in foo.iter_fields.enumerate
Automatically serialize and deserialize via Serde (without explicit serde impls)
let mut registry = default;
registry.;
registry.;
registry.;
registry.;
registry.;
registry.;
let serializer = new;
let serialized = to_string_pretty.unwrap;
let mut deserializer = from_str.unwrap;
let reflect_deserializer = new;
let value = reflect_deserializer.deserialize.unwrap;
let dynamic_struct = value..unwrap;
assert!;
Trait "reflection"
Call a trait on a given &dyn Reflect
reference without knowing the underlying type!
// First, lets box our type as a Box<dyn Reflect>
let reflect_value: = Box new;
// This means we no longer have direct access to MyType or its methods. We can only call Reflect methods on reflect_value.
// What if we want to call `do_thing` on our type? We could downcast using reflect_value.downcast_ref::<MyType>(), but what if we
// don't know the type at compile time?
// Normally in rust we would be out of luck at this point. Lets use our new reflection powers to do something cool!
let mut type_registry = default;
type_registry.;
// The #[reflect] attribute we put on our DoThing trait generated a new `ReflectDoThing` struct, which implements TypeData.
// This was added to MyType's TypeRegistration.
let reflect_do_thing = type_registry
.
.unwrap;
// We can use this generated type to convert our `&dyn Reflect` reference to a `&dyn DoThing` reference
let my_trait: &dyn DoThing = reflect_do_thing.get.unwrap;
// Which means we can now call do_thing(). Magic!
println!;
// This works because the #[reflect(MyTrait)] we put on MyType informed the Reflect derive to insert a new instance
// of ReflectDoThing into MyType's registration. The instance knows how to cast &dyn Reflect to &dyn DoThing, because it
// knows that &dyn Reflect should first be downcasted to &MyType, which can then be safely casted to &dyn DoThing
Why make this?
The whole point of Rust is static safety! Why build something that makes it easy to throw it all away?
- Some problems are inherently dynamic (scripting, some types of serialization / deserialization)
- Sometimes the dynamic way is easier
- Sometimes the dynamic way puts less burden on your users to derive a bunch of traits (this was a big motivator for the Bevy project)