@@ -9,7 +9,7 @@ use syn::{
9
9
10
10
use crate :: bevy_ecs_path;
11
11
12
- static READ_ONLY_ATTRIBUTE_NAME : & str = "read_only " ;
12
+ static MUTABLE_ATTRIBUTE_NAME : & str = "mutable " ;
13
13
static READ_ONLY_DERIVE_ATTRIBUTE_NAME : & str = "read_only_derive" ;
14
14
15
15
pub fn derive_fetch_impl ( input : TokenStream ) -> TokenStream {
@@ -26,7 +26,7 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
26
26
ty_generics,
27
27
where_clause,
28
28
has_world_lifetime,
29
- has_read_only_attr ,
29
+ has_mutable_attr ,
30
30
world_lifetime,
31
31
state_lifetime,
32
32
phantom_field_idents,
@@ -47,8 +47,8 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
47
47
. map_or ( false , |ident| ident == READ_ONLY_DERIVE_ATTRIBUTE_NAME )
48
48
} ) ;
49
49
let read_only_derive_macro_call = if let Some ( read_only_derive_attr) = read_only_derive_attr {
50
- if has_read_only_attr {
51
- panic ! ( "Attributes `read_only` and `read_only_derive` are mutually exclusive " ) ;
50
+ if !has_mutable_attr {
51
+ panic ! ( "Attribute `read_only_derive` can only be be used for a struct marked with the `mutable` attribute " ) ;
52
52
}
53
53
let derive_args = & read_only_derive_attr. tokens ;
54
54
quote ! { #[ derive #derive_args] }
@@ -74,25 +74,10 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
74
74
75
75
let path = bevy_ecs_path ( ) ;
76
76
77
- let struct_read_only_declaration = if has_read_only_attr {
77
+ let struct_read_only_declaration = if has_mutable_attr {
78
78
quote ! {
79
- // Statically checks that the safety guarantee actually holds true. We need this to make
80
- // sure that we don't compile `ReadOnlyFetch` if our struct contains nested `WorldQuery`
81
- // members that don't implement it.
82
- #[ allow( dead_code) ]
83
- const _: ( ) = {
84
- fn assert_readonly<T : #path:: query:: ReadOnlyFetch >( ) { }
85
-
86
- // We generate a readonly assertion for every struct member.
87
- fn assert_all #impl_generics ( ) #where_clause {
88
- #( assert_readonly:: <<#query_types as #path:: query:: WorldQuery >:: Fetch >( ) ; ) *
89
- }
90
- } ;
91
- }
92
- } else {
93
- quote ! {
94
- // TODO: it would be great to be able to dedup this by just deriving `Fetch` again with `read_only` attribute,
95
- // but supporting QSelf types is tricky.
79
+ // TODO: it would be great to be able to dedup this by just deriving `Fetch` again
80
+ // without the `mutable` attribute, but supporting QSelf types is tricky.
96
81
#read_only_derive_macro_call
97
82
struct #struct_name_read_only #impl_generics #where_clause {
98
83
#( #field_idents: <<#query_types as #path:: query:: WorldQuery >:: ReadOnlyFetch as #path:: query:: Fetch <#world_lifetime, #world_lifetime>>:: Item , ) *
@@ -154,6 +139,21 @@ pub fn derive_fetch_impl(input: TokenStream) -> TokenStream {
154
139
type ReadOnlyFetch = #read_only_fetch_struct_name #ty_generics;
155
140
}
156
141
}
142
+ } else {
143
+ quote ! {
144
+ // Statically checks that the safety guarantee actually holds true. We need this to make
145
+ // sure that we don't compile `ReadOnlyFetch` if our struct contains nested `WorldQuery`
146
+ // members that don't implement it.
147
+ #[ allow( dead_code) ]
148
+ const _: ( ) = {
149
+ fn assert_readonly<T : #path:: query:: ReadOnlyFetch >( ) { }
150
+
151
+ // We generate a readonly assertion for every struct member.
152
+ fn assert_all #impl_generics ( ) #where_clause {
153
+ #( assert_readonly:: <<#query_types as #path:: query:: WorldQuery >:: Fetch >( ) ; ) *
154
+ }
155
+ } ;
156
+ }
157
157
} ;
158
158
159
159
let tokens = TokenStream :: from ( quote ! {
@@ -265,7 +265,7 @@ pub fn derive_filter_fetch_impl(input: TokenStream) -> TokenStream {
265
265
ty_generics,
266
266
where_clause,
267
267
world_lifetime,
268
- has_read_only_attr : _,
268
+ has_mutable_attr : _,
269
269
has_world_lifetime,
270
270
state_lifetime,
271
271
phantom_field_idents,
@@ -383,7 +383,7 @@ pub fn derive_filter_fetch_impl(input: TokenStream) -> TokenStream {
383
383
// This struct is used to share common tokens between `Fetch` and `FilterFetch` implementations.
384
384
struct FetchImplTokens < ' a > {
385
385
struct_name : Ident ,
386
- // Equals `struct_name` if `has_read_only_attr ` is true .
386
+ // Equals `struct_name` if `has_mutable_attr ` is false .
387
387
struct_name_read_only : Ident ,
388
388
fetch_struct_name : Ident ,
389
389
state_struct_name : Ident ,
@@ -392,7 +392,7 @@ struct FetchImplTokens<'a> {
392
392
impl_generics : ImplGenerics < ' a > ,
393
393
ty_generics : TypeGenerics < ' a > ,
394
394
where_clause : Option < & ' a WhereClause > ,
395
- has_read_only_attr : bool ,
395
+ has_mutable_attr : bool ,
396
396
has_world_lifetime : bool ,
397
397
world_lifetime : GenericParam ,
398
398
state_lifetime : GenericParam ,
@@ -405,10 +405,10 @@ struct FetchImplTokens<'a> {
405
405
}
406
406
407
407
fn fetch_impl_tokens ( ast : & DeriveInput ) -> FetchImplTokens {
408
- let has_read_only_attr = ast. attrs . iter ( ) . any ( |attr| {
408
+ let has_mutable_attr = ast. attrs . iter ( ) . any ( |attr| {
409
409
attr. path
410
410
. get_ident ( )
411
- . map_or ( false , |ident| ident == READ_ONLY_ATTRIBUTE_NAME )
411
+ . map_or ( false , |ident| ident == MUTABLE_ATTRIBUTE_NAME )
412
412
} ) ;
413
413
414
414
let world_lifetime = ast. generics . params . first ( ) . and_then ( |param| match param {
@@ -429,17 +429,17 @@ fn fetch_impl_tokens(ast: &DeriveInput) -> FetchImplTokens {
429
429
let ( impl_generics, ty_generics, where_clause) = ast. generics . split_for_impl ( ) ;
430
430
431
431
let struct_name = ast. ident . clone ( ) ;
432
- let struct_name_read_only = if has_read_only_attr {
433
- ast. ident . clone ( )
434
- } else {
432
+ let struct_name_read_only = if has_mutable_attr {
435
433
Ident :: new ( & format ! ( "{}ReadOnly" , struct_name) , Span :: call_site ( ) )
434
+ } else {
435
+ ast. ident . clone ( )
436
436
} ;
437
437
let fetch_struct_name = Ident :: new ( & format ! ( "{}Fetch" , struct_name) , Span :: call_site ( ) ) ;
438
438
let state_struct_name = Ident :: new ( & format ! ( "{}State" , struct_name) , Span :: call_site ( ) ) ;
439
- let read_only_fetch_struct_name = if has_read_only_attr {
440
- fetch_struct_name. clone ( )
441
- } else {
439
+ let read_only_fetch_struct_name = if has_mutable_attr {
442
440
Ident :: new ( & format ! ( "{}ReadOnlyFetch" , struct_name) , Span :: call_site ( ) )
441
+ } else {
442
+ fetch_struct_name. clone ( )
443
443
} ;
444
444
445
445
let fields = match & ast. data {
@@ -496,7 +496,7 @@ fn fetch_impl_tokens(ast: &DeriveInput) -> FetchImplTokens {
496
496
impl_generics,
497
497
ty_generics,
498
498
where_clause,
499
- has_read_only_attr ,
499
+ has_mutable_attr ,
500
500
has_world_lifetime,
501
501
world_lifetime,
502
502
state_lifetime,
0 commit comments