|
| 1 | +--- |
| 2 | +title: "Type data creation and registration changes" |
| 3 | +pull_requests: [24518] |
| 4 | +--- |
| 5 | + |
| 6 | +Type data definition and registration have been reworked to enable `on_insert` and `on_register` registration callbacks. |
| 7 | + |
| 8 | +Type data is no longer automatically implemented on types implementing `Clone`. |
| 9 | +Instead, it must be manually implemented or derived: |
| 10 | + |
| 11 | +```rust |
| 12 | +// BEFORE |
| 13 | +#[derive(Clone)] |
| 14 | +struct ReflectSomeTypeData; |
| 15 | + |
| 16 | +// AFTER |
| 17 | +#[derive(TypeData)] |
| 18 | +struct ReflectSomeTypeData; |
| 19 | + |
| 20 | +// or manually: |
| 21 | +// impl TypeData for ReflectSomeTypeData {} |
| 22 | +``` |
| 23 | + |
| 24 | +Additionally, type data can no longer be inserted directly onto a `&mut TypeRegistration`. |
| 25 | +Instead, it must be inserted during construction, with an owned `TypeRegistration`. |
| 26 | +This was done to ensure that registration callbacks weren't accidentally missed. |
| 27 | + |
| 28 | +```rust |
| 29 | +// BEFORE |
| 30 | +let mut registration = TypeRegistration::of::<MyType>(); |
| 31 | +registration.register_type_data::<SomeTypeData, _>(); |
| 32 | + |
| 33 | +// AFTER |
| 34 | +let registration = TypeRegistration::of::<MyType>() |
| 35 | + .register_type_data::<SomeTypeData, _>(); |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +Methods for registering type data on the `TypeRegistry` remain unchanged. |
| 40 | +However, because of the new restrictions around inserting type data on `TypeRegistration`, |
| 41 | +instances of `TypeRegistry::get_mut` can be replaced with the new `TypeRegistry::registration_scope` |
| 42 | +in order to insert multiple type data without additional lookups. |
| 43 | + |
| 44 | +```rust |
| 45 | +// BEFORE |
| 46 | +let registration = registry.get_mut(TypeId::of::<MyType>()); |
| 47 | +registration.register_type_data::<SomeTypeData, _>(); |
| 48 | + |
| 49 | +// AFTER |
| 50 | +let registration = registry.registration_scope(TypeId::of::<MyType>(), |mut registration| { |
| 51 | + registration.register_type_data::<SomeTypeData, _>(); |
| 52 | +}); |
| 53 | +``` |
0 commit comments