Skip to content

Commit cdc90bb

Browse files
MrGVSVcart
andauthored
0.11 Section: Deref Derive Attribute (#663)
Co-authored-by: Carter Anderson <[email protected]>
1 parent 42d24c6 commit cdc90bb

File tree

1 file changed

+26
-0
lines changed
  • content/news/2023-07-07-bevy-0.11

1 file changed

+26
-0
lines changed

content/news/2023-07-07-bevy-0.11/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ Since our last release a few months ago we've added a _ton_ of new features, bug
1919
*
2020
## WebGPU Support
2121

22+
## Deref Derive Attribute
23+
24+
<div class="release-feature-authors">authors: @MrGVSV</div>
25+
26+
Bevy code tends to make heavy use of the [newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) pattern,
27+
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).
28+
29+
This previously only worked for structs with a single field:
30+
31+
```rust
32+
#[derive(Resource, Deref, DerefMut)]
33+
struct Score(i32);
34+
```
35+
36+
For 0.11, we've improved these derives by adding the `#[deref]` attribute, which allows them to be used on structs with multiple fields.
37+
This makes working with generic newtypes much easier:
38+
39+
```rust
40+
#[derive(Component, Deref, DerefMut)]
41+
struct Health<T: Character> {
42+
#[deref] // <- use the `health` field as the `Deref` and `DerefMut` target
43+
health: u16,
44+
_character_type: PhantomData<T>,
45+
}
46+
```
47+
2248
<div class="release-feature-authors">authors: @mockersf, many others throughout Bevy's development</div>
2349

2450
![webgpu](webgpu.svg)

0 commit comments

Comments
 (0)