-
Notifications
You must be signed in to change notification settings - Fork 403
Add 0.15 release notes for remote reflection #1752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alice-i-cecile
merged 2 commits into
bevyengine:main
from
MrGVSV:mrgvsv/notes/6042-remote-reflection
Oct 30, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 51 additions & 3 deletions
54
release-content/0.15/release-notes/6042_bevy_reflect_Reflect_remote_types.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,52 @@ | ||
| <!-- bevy_reflect: Reflect remote types --> | ||
| <!-- https://github.com/bevyengine/bevy/pull/6042 --> | ||
| The [`bevy_reflect`] crate relies on types implementing [`Reflect`] in order to make them reflectable. | ||
| Fields of structs and enums that don't implement `Reflect` must be specifically ignored with `#[reflect(ignore)]`. | ||
| And due to Rust's [orphan rule], this is often the case for types not owned by the current crate. | ||
|
|
||
| <!-- TODO --> | ||
| Based on [`serde`](https://serde.rs/remote-derive.html), Bevy 0.15 introduces a way to reflect remote types | ||
| using a new [`#[reflect_remote(...)]`][reflect_remote] attribute macro. | ||
| This allows users to define a model for reflection to base its behavior on, | ||
| while still operating with the actual type. | ||
|
|
||
| ```rust | ||
| // Pretend this type is defined in a crate called `external_crate` | ||
| #[derive(Default)] | ||
| struct Name { | ||
| pub value: String, | ||
| } | ||
|
|
||
| // We can define our model, including other derives and reflection attributes | ||
| #[reflect_remote(external_crate::Name)] | ||
| #[derive(Default)] | ||
| #[reflect(Default)] | ||
| struct NameWrapper { | ||
| pub value: String, | ||
| } | ||
|
|
||
| // Now we can use `Name` as a field in a reflected type without having to ignore it | ||
| #[derive(Reflect)] | ||
| struct Player { | ||
| #[reflect(remote = NameWrapper)] | ||
| name: external_crate::Name, | ||
| } | ||
| ``` | ||
|
|
||
| Under the hood, this works by transforming our model into a transparent wrapper around the actual type: | ||
|
|
||
| ```rust | ||
| #[repr(transparent)] | ||
| struct NameWrapper(pub external_crate::Name); | ||
| ``` | ||
|
|
||
| The macro then uses the model to generate all the reflection trait implementations, | ||
| driven by a new [`ReflectRemote`] trait for swapping between our wrapper and the remote type. | ||
| Compile-time assertions are also generated to help ensure the model and the actual type stay in sync. | ||
|
|
||
| While this feature has many aspects complete, including generic support, enum support, and nesting, | ||
| there are still some limitations we hope to address in future releases, | ||
| including support for reflecting a remote type with private fields. | ||
|
|
||
| [`bevy_reflect`]: https://docs.rs/bevy/0.15/bevy_reflect/ | ||
| [`Reflect`]: https://docs.rs/bevy/0.15/bevy_reflect/trait.Reflect.html | ||
| [orphan rule]: https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type | ||
| [reflect_remote]: https://docs.rs/bevy/0.15/bevy_reflect/attr.reflect_remote.html | ||
| [`ReflectRemote`]: https://docs.rs/bevy/0.15/bevy_reflect/trait.ReflectRemote.html | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.