-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bevy_derive: Add
#[deref]
attribute (#8552)
# Objective 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 a dedicated derive for [`Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) and [`DerefMut`](https://doc.rust-lang.org/std/ops/trait.DerefMut.html). This derive works for any struct with a single field: ```rust #[derive(Component, Deref, DerefMut)] struct MyNewtype(usize); ``` One reason for the single-field limitation is to prevent confusion and footguns related that would arise from allowing multi-field structs: <table align="center"> <tr> <th colspan="2"> Similar structs, different derefs </th> </tr> <tr> <td> ```rust #[derive(Deref, DerefMut)] struct MyStruct { foo: usize, // <- Derefs usize bar: String, } ``` </td> <td> ```rust #[derive(Deref, DerefMut)] struct MyStruct { bar: String, // <- Derefs String foo: usize, } ``` </td> </tr> <tr> <th colspan="2"> Why `.1`? </th> </tr> <tr> <td colspan="2"> ```rust #[derive(Deref, DerefMut)] struct MyStruct(Vec<usize>, Vec<f32>); let mut foo = MyStruct(vec![123], vec![1.23]); // Why can we skip the `.0` here? foo.push(456); // But not here? foo.1.push(4.56); ``` </td> </tr> </table> However, there are certainly cases where it's useful to allow for structs with multiple fields. Such as for structs with one "real" field and one `PhantomData` to allow for generics: ```rust #[derive(Deref, DerefMut)] struct MyStruct<T>( // We want use this field for the `Deref`/`DerefMut` impls String, // But we need this field so that we can make this struct generic PhantomData<T> ); // ERROR: Deref can only be derived for structs with a single field // ERROR: DerefMut can only be derived for structs with a single field ``` Additionally, the possible confusion and footguns are mainly an issue for newer Rust/Bevy users. Those familiar with `Deref` and `DerefMut` understand what adding the derive really means and can anticipate its behavior. ## Solution Allow users to opt into multi-field `Deref`/`DerefMut` derives using a `#[deref]` attribute: ```rust #[derive(Deref, DerefMut)] struct MyStruct<T>( // Use this field for the `Deref`/`DerefMut` impls #[deref] String, // We can freely include any other field without a compile error PhantomData<T> ); ``` This prevents the footgun pointed out in the first issue described in the previous section, but it still leaves the possible confusion surrounding `.0`-vs-`.#`. However, the idea is that by making this behavior explicit with an attribute, users will be more aware of it and can adapt appropriately. --- ## Changelog - Added `#[deref]` attribute to `Deref` and `DerefMut` derives
- Loading branch information
Showing
33 changed files
with
696 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "bevy_macros_compile_fail_tests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Compile fail tests for Bevy Engine's various macros" | ||
homepage = "https://bevyengine.org" | ||
repository = "https://github.com/bevyengine/bevy" | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
bevy_derive = { path = "../bevy_derive" } | ||
trybuild = "1.0.71" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Compile fail tests for Bevy macros | ||
|
||
This crate is not part of the Bevy workspace in order to not fail `crater` tests for Bevy. | ||
The tests assert on the exact compiler errors and can easily fail for new Rust versions due to updated compiler errors (e.g. changes in spans). | ||
|
||
The `CI` workflow executes these tests on the stable rust toolchain (see [tools/ci](../../tools/ci/src/main.rs)). |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
// Nothing here, check out the integration tests |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[test] | ||
fn test() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/deref_derive/*.fail.rs"); | ||
t.pass("tests/deref_derive/*.pass.rs"); | ||
} |
15 changes: 15 additions & 0 deletions
15
crates/bevy_macros_compile_fail_tests/tests/deref_derive/invalid_attribute.fail.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use bevy_derive::Deref; | ||
|
||
// Reason: `#[deref]` doesn't take any arguments | ||
|
||
#[derive(Deref)] | ||
struct TupleStruct(usize, #[deref()] String); | ||
|
||
#[derive(Deref)] | ||
struct Struct { | ||
foo: usize, | ||
#[deref()] | ||
bar: String, | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
crates/bevy_macros_compile_fail_tests/tests/deref_derive/invalid_attribute.fail.stderr
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: unexpected token in attribute | ||
--> tests/deref_derive/invalid_attribute.fail.rs:6:34 | ||
| | ||
6 | struct TupleStruct(usize, #[deref()] String); | ||
| ^ | ||
|
||
error: unexpected token in attribute | ||
--> tests/deref_derive/invalid_attribute.fail.rs:11:12 | ||
| | ||
11 | #[deref()] | ||
| ^ |
9 changes: 9 additions & 0 deletions
9
crates/bevy_macros_compile_fail_tests/tests/deref_derive/invalid_item.fail.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use bevy_derive::Deref; | ||
|
||
#[derive(Deref)] | ||
struct UnitStruct; | ||
|
||
#[derive(Deref)] | ||
enum Enum {} | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
crates/bevy_macros_compile_fail_tests/tests/deref_derive/invalid_item.fail.stderr
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: Deref cannot be derived on field-less structs | ||
--> tests/deref_derive/invalid_item.fail.rs:3:10 | ||
| | ||
3 | #[derive(Deref)] | ||
| ^^^^^ | ||
| | ||
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: Deref can only be derived on structs | ||
--> tests/deref_derive/invalid_item.fail.rs:6:10 | ||
| | ||
6 | #[derive(Deref)] | ||
| ^^^^^ | ||
| | ||
= note: this error originates in the derive macro `Deref` (in Nightly builds, run with -Z macro-backtrace for more info) |
12 changes: 12 additions & 0 deletions
12
crates/bevy_macros_compile_fail_tests/tests/deref_derive/missing_attribute.fail.rs
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use bevy_derive::Deref; | ||
|
||
#[derive(Deref)] | ||
struct TupleStruct(usize, String); | ||
|
||
#[derive(Deref)] | ||
struct Struct { | ||
foo: usize, | ||
bar: String, | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.