Skip to content

Commit fd4d936

Browse files
MrGVSValice-i-cecilekillercup
authored
Add release notes for PR #5781: bevy_reflect: Recursive registration (#1282)
Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Pascal Hertleif <[email protected]>
1 parent b021956 commit fd4d936

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
TODO
1+
Bevy uses [reflection](https://docs.rs/bevy_reflect/latest/bevy_reflect/) in order to dynamically process data for things like serialization and deserialization.
2+
A Bevy app has a `TypeRegistry` to keep track of which types exist.
3+
Users can register their custom types when initializing the app or plugin.
4+
5+
```rust
6+
#[derive(Reflect)]
7+
struct Data<T> {
8+
value: T,
9+
}
10+
11+
#[derive(Reflect)]
12+
struct Blob {
13+
contents: Vec<u8>,
14+
}
15+
16+
app
17+
.register_type::<Data<Blob>>()
18+
.register_type::<Blob>()
19+
.register_type::<Vec<u8>>()
20+
```
21+
22+
In the code above, `Data<Blob>` depends on `Blob` which depends on `Vec<u8>`,
23+
which means that all three types need to be manually registered—
24+
even if we only care about `Data<Blob>`.
25+
26+
This is both tedious and error-prone, especially when these type dependencies are only
27+
used in the context of other types (i.e. they aren't used as standalone types).
28+
29+
In 0.14, any type that derives `Reflect` will automatically register all of its type dependencies.
30+
So when we register `Data<Blob>`, `Blob` will be registered as well (which will register `Vec<u8>`),
31+
thus simplifying our registration down to a single line:
32+
33+
```rust
34+
app.register_type::<Data<Blob>>()
35+
```
36+
37+
Note that removing the registration for `Data<Blob>` now also means that `Blob` and `Vec<u8>` may
38+
not be registered either, unless they were registered some other way.
39+
If those types are needed as standalone types, they should be registered separately.

0 commit comments

Comments
 (0)