From 8b2faa14ad21e3fba775006f26119ff1814b2864 Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Thu, 29 Jun 2023 23:41:37 -0700 Subject: [PATCH] section: Deref Derive Attribute --- content/news/2023-07-07-bevy-0.11/index.md | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/content/news/2023-07-07-bevy-0.11/index.md b/content/news/2023-07-07-bevy-0.11/index.md index 3d0bc99389..f66c0c0385 100644 --- a/content/news/2023-07-07-bevy-0.11/index.md +++ b/content/news/2023-07-07-bevy-0.11/index.md @@ -17,11 +17,31 @@ Since our last release a few months ago we've added a _ton_ of new features, bug * **Feature**: description -## Feature +## Deref Derive Attribute -
authors: @todo
+
authors: @MrGVSV
-Description +Bevy code tends to make heavy use of the [newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) pattern, +which is why we have dedicated derives for [`Deref`](https://docs.rs/bevy/latest/bevy/prelude/derive.Deref.html) and [`DerefMut`](https://docs.rs/bevy/latest/bevy/prelude/derive.DerefMut.html). + +This previously only worked for structs with a single field: + +```rust +#[derive(Resource, Deref, DerefMut)] +struct Score(i32); +``` + +For 0.11, we've improved these derives by adding the `#[deref]` attribute, which allows them to be used on structs with multiple fields. +This makes working with generic newtypes much easier: + +```rust +#[derive(Component, Deref, DerefMut)] +struct Health { + #[deref] // <- use the `health` field as the `Deref` and `DerefMut` target + health: u16, + _character_type: PhantomData, +} +``` ## What's Next?