@@ -145,48 +145,90 @@ pub type QueryItem<'w, 's, Q> = <<Q as WorldQuery>::Fetch as Fetch<'w, 's>>::Ite
145
145
/// optional_foo: Option<&'w OptionalFoo>,
146
146
/// }
147
147
///
148
+ /// // You can also compose derived queries with regular ones in tuples.
149
+ /// fn my_system(query: Query<(&Foo, MyQuery, FooQuery)>) {
150
+ /// for (foo, my_query, foo_query) in query.iter() {
151
+ /// foo; my_query; foo_query;
152
+ /// }
153
+ /// }
154
+ ///
155
+ /// # my_system.system();
148
156
/// ```
149
157
///
150
158
/// ## Read-only queries
151
159
///
152
160
/// All queries that are derived with `Fetch` macro have their read-only variants with `ReadOnly`
153
- /// suffix. If you are going to use a query only for reading , you can mark it with `read_only`
154
- /// attribute.
161
+ /// suffix. If you are going to use a query which can only read data from the ECS , you can mark it
162
+ /// with the `read_only` attribute.
155
163
///
156
164
/// ```
157
165
/// # use bevy_ecs::prelude::*;
158
166
/// use bevy_ecs::query::{Fetch, ReadOnlyFetch, WorldQuery};
159
167
///
160
168
/// #[derive(Component)]
161
- /// struct Foo ;
169
+ /// struct A(i32) ;
162
170
/// #[derive(Component)]
163
- /// struct Bar ;
171
+ /// struct B(i32) ;
164
172
///
165
173
/// #[derive(Fetch)]
166
- /// #[read_only]
167
- /// struct FooQuery<'w> {
168
- /// foo: &'w Foo,
169
- /// bar_query: BarQueryReadOnly<'w>,
174
+ /// struct SumQuery<'w> {
175
+ /// a: &'w A,
176
+ /// b: &'w B,
177
+ /// }
178
+ ///
179
+ /// // This implementation is only available when iterating with `iter_mut`.
180
+ /// impl<'w> SumQuery<'w> {
181
+ /// fn calc(&self) -> i32 {
182
+ /// let Self { a: A(a), b: B(b) } = self;
183
+ /// a + b
184
+ /// }
185
+ /// }
186
+ ///
187
+ /// // If you want to use it with `iter`, you'll need to write an additional implementation.
188
+ /// impl<'w> SumQueryReadOnly<'w> {
189
+ /// fn calc(&self) -> i32 {
190
+ /// let Self { a: A(a), b: B(b) } = self;
191
+ /// a + b
192
+ /// }
170
193
/// }
171
194
///
195
+ /// // If you are never going to mutate the data, you can mark the query with the `read_only`
196
+ /// // attribute and write the implementation only once.
172
197
/// #[derive(Fetch)]
173
- /// struct BarQuery<'w> {
174
- /// bar: &'w Bar,
198
+ /// #[read_only]
199
+ /// struct ProductQuery<'w> {
200
+ /// a: &'w A,
201
+ /// b: &'w B,
175
202
/// }
176
203
///
177
- /// fn assert_read_only<T: ReadOnlyFetch>() {}
204
+ /// impl<'w> ProductQuery<'w> {
205
+ /// fn calc(&self) -> i32 {
206
+ /// let Self { a: A(a), b: B(b) } = self;
207
+ /// a * b
208
+ /// }
209
+ /// }
178
210
///
179
- /// assert_read_only::<<FooQuery as WorldQuery>::Fetch>();
180
- /// assert_read_only::<<BarQuery as WorldQuery>::ReadOnlyFetch>();
181
- /// // the following will fail to compile:
182
- /// // assert_read_only::<<BarQuery as WorldQuery>::Fetch>();
211
+ /// fn my_system(mut sum_query: Query<SumQuery>, product_query: Query<ProductQuery>) {
212
+ /// // Iterator's item is `SumQueryReadOnly`.
213
+ /// for sum in sum_query.iter() {
214
+ /// println!("Sum: {}", sum.calc());
215
+ /// }
216
+ /// // Iterator's item is `SumQuery`.
217
+ /// for sum in sum_query.iter_mut() {
218
+ /// println!("Sum (mut): {}", sum.calc());
219
+ /// }
220
+ /// // Iterator's item is `ProductQuery`.
221
+ /// for product in product_query.iter() {
222
+ /// println!("Product: {}", product.calc());
223
+ /// }
224
+ /// }
183
225
/// ```
184
226
///
185
227
/// If you want to use derive macros with read-only query variants, you need to pass them with
186
- /// using `read_only_derive` attribute.
187
- ///
188
- /// # use bevy_ecs::prelude::*;
189
- /// use bevy_ecs::query::{Fetch, ReadOnlyFetch, WorldQuery};
228
+ /// using the `read_only_derive` attribute. As the `Fetch` macro generates an additional struct
229
+ /// for `ReadOnlyFetch` implementation (granted it isn't marked with the `read_only` attribute),
230
+ /// you may want it to inherit the same derives. Since derive macros can't access information about
231
+ /// other derives, they need to be passed manually with the `read_only_derive` attribute.
190
232
///
191
233
/// ```
192
234
/// # use bevy_ecs::prelude::*;
0 commit comments