Skip to content

Commit

Permalink
fix manual registration
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Nov 26, 2020
1 parent 2cd3a2f commit ba21f0b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_reflect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ let reflect_value: Box<dyn Reflect> = Box::new(MyType {
value: "Hello".to_string(),
});

// This means we no longer have direct access to MyType or it 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.get::<MyType>(), but what if we
// 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!
Expand All @@ -146,7 +146,7 @@ let reflect_do_thing = type_registry
.get_type_data::<ReflectDoThing>(reflect_value.type_id())
.unwrap();

// We can use this generated type to convert our `&dyn Reflect` reference to an `&dyn DoThing` reference
// 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(&*reflect_value).unwrap();

// Which means we can now call do_thing(). Magic!
Expand Down
6 changes: 2 additions & 4 deletions crates/bevy_reflect/src/impls/bevy_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ pub trait RegisterTypeBuilder {
impl RegisterTypeBuilder for AppBuilder {
fn register_type<T: GetTypeRegistration>(&mut self) -> &mut Self {
{
let registry = &mut self.resources().get_mut::<TypeRegistryArc>().unwrap();
registry
.write()
.add_registration(T::get_type_registration());
let registry = self.resources().get_mut::<TypeRegistryArc>().unwrap();
registry.write().register::<T>();
}
self
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ mod tests {
let serialized = to_string_pretty(&serializer, PrettyConfig::default()).unwrap();

let mut deserializer = Deserializer::from_str(&serialized).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&mut registry);
let reflect_deserializer = ReflectDeserializer::new(&registry);
let value = reflect_deserializer.deserialize(&mut deserializer).unwrap();
let dynamic_struct = value.take::<DynamicStruct>().unwrap();

Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ pub trait GetTypeRegistration {
impl TypeRegistry {
pub fn register<T>(&mut self)
where
T: Reflect,
T: GetTypeRegistration,
{
let registration = TypeRegistration::of::<T>();
self.add_registration(registration);
self.add_registration(T::get_type_registration());
}

pub fn add_registration(&mut self, registration: TypeRegistration) {
Expand Down
6 changes: 3 additions & 3 deletions examples/reflection/trait_reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ fn setup(type_registry: Res<TypeRegistry>) {
value: "Hello".to_string(),
});

// This means we no longer have direct access to MyType or it 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.get::<MyType>(), but what if we
// 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!
Expand All @@ -44,7 +44,7 @@ fn setup(type_registry: Res<TypeRegistry>) {
.get_type_data::<ReflectDoThing>(reflect_value.type_id())
.unwrap();

// We can use this generated type to convert our `&dyn Reflect` reference to an `&dyn DoThing` reference
// 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(&*reflect_value).unwrap();

// Which means we can now call do_thing(). Magic!
Expand Down

0 comments on commit ba21f0b

Please sign in to comment.