Skip to content

Commit 7c274e5

Browse files
committed
Improve bevy_ecs query docs (#1935)
Mainly documents Query, WorldQuery and the various Query Filter types as well as some smaller doc changes.
1 parent 19f467e commit 7c274e5

File tree

11 files changed

+396
-97
lines changed

11 files changed

+396
-97
lines changed

crates/bevy_ecs/src/component/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ use std::{
1010
};
1111
use thiserror::Error;
1212

13-
/// A component is data associated with an `Entity`. Each entity can have multiple different types
14-
/// of components, but only one of them per type.
13+
/// A component is data associated with an [`Entity`](crate::entity::Entity). Each entity can have
14+
/// multiple different types of components, but only one of them per type.
1515
///
1616
/// Any type that is `Send + Sync + 'static` automatically implements `Component`.
1717
///
18-
/// Components are added with new entities using [Commands::spawn](crate::system::Commands::spawn),
19-
/// or to existing entities with [Commands::insert](crate::system::Commands::insert),
20-
/// or their [World](crate::world::World) equivalents.
18+
/// Components are added with new entities using [`Commands::spawn`](crate::system::Commands::spawn),
19+
/// or to existing entities with [`EntityCommands::insert`](crate::system::EntityCommands::insert),
20+
/// or their [`World`](crate::world::World) equivalents.
2121
///
22-
/// Components can be accessed in systems by using a [Query](crate::system::Query)
22+
/// Components can be accessed in systems by using a [`Query`](crate::system::Query)
2323
/// as one of the arguments.
2424
///
25-
/// Components can be grouped together into a [Bundle](crate::bundle::Bundle).
25+
/// Components can be grouped together into a [`Bundle`](crate::bundle::Bundle).
2626
pub trait Component: Send + Sync + 'static {}
2727
impl<T: Send + Sync + 'static> Component for T {}
2828

@@ -241,6 +241,7 @@ impl Components {
241241
}
242242

243243
/// # Safety
244+
///
244245
/// `id` must be a valid [ComponentId]
245246
#[inline]
246247
pub unsafe fn get_info_unchecked(&self, id: ComponentId) -> &ComponentInfo {

crates/bevy_ecs/src/component/type_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{alloc::Layout, any::TypeId};
22

3-
/// Metadata required to store a component
3+
/// Metadata required to store a component.
44
#[derive(Debug, Clone, PartialEq, Eq)]
55
pub struct TypeInfo {
66
type_id: TypeId,
@@ -11,7 +11,7 @@ pub struct TypeInfo {
1111
}
1212

1313
impl TypeInfo {
14-
/// Metadata for `T`
14+
/// Metadata for `T`.
1515
pub fn of<T: Send + Sync + 'static>() -> Self {
1616
Self {
1717
type_id: TypeId::of::<T>(),

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ use std::{
1111
sync::atomic::{AtomicI64, Ordering},
1212
};
1313

14-
/// Lightweight unique ID of an entity
14+
/// Lightweight unique ID of an entity.
1515
///
16-
/// Obtained from [World::spawn](crate::world::World::spawn), typically via
17-
/// [Commands::spawn](crate::system::Commands::spawn). Can be stored to refer to an entity in the
16+
/// Obtained from [`World::spawn`](crate::world::World::spawn), typically via
17+
/// [`Commands::spawn`](crate::system::Commands::spawn). Can be stored to refer to an entity in the
1818
/// future.
1919
///
2020
/// `Entity` can be a part of a query, e.g. `Query<(Entity, &MyComponent)>`.
2121
/// Components of a specific entity can be accessed using
22-
/// [Query::get](crate::system::Query::get) and related methods.
22+
/// [`Query::get`](crate::system::Query::get) and related methods.
2323
#[derive(Clone, Copy, Hash, Eq, Ord, PartialEq, PartialOrd)]
2424
pub struct Entity {
2525
pub(crate) generation: u32,
2626
pub(crate) id: u32,
2727
}
2828

2929
impl Entity {
30-
/// Creates a new entity reference with a generation of 0
30+
/// Creates a new entity reference with a generation of 0.
3131
pub fn new(id: u32) -> Entity {
3232
Entity { id, generation: 0 }
3333
}
3434

35-
/// Convert to a form convenient for passing outside of rust
35+
/// Convert to a form convenient for passing outside of rust.
3636
///
3737
/// Only useful for identifying entities within the same instance of an application. Do not use
3838
/// for serialization between runs.
@@ -42,7 +42,7 @@ impl Entity {
4242
u64::from(self.generation) << 32 | u64::from(self.id)
4343
}
4444

45-
/// Reconstruct an `Entity` previously destructured with `to_bits`
45+
/// Reconstruct an `Entity` previously destructured with [`Entity::to_bits`].
4646
///
4747
/// Only useful when applied to results from `to_bits` in the same instance of an application.
4848
pub fn from_bits(bits: u64) -> Self {
@@ -52,7 +52,7 @@ impl Entity {
5252
}
5353
}
5454

55-
/// Return a transiently unique identifier
55+
/// Return a transiently unique identifier.
5656
///
5757
/// No two simultaneously-live entities share the same ID, but dead entities' IDs may collide
5858
/// with both live and dead entities. Useful for compactly representing entities within a
@@ -87,7 +87,8 @@ impl SparseSetIndex for Entity {
8787
}
8888
}
8989

90-
/// An iterator returning a sequence of Entity values from `Entities::reserve_entities`.
90+
/// An [`Iterator`] returning a sequence of [`Entity`] values from
91+
/// [`Entities::reserve_entities`](crate::entity::Entities::reserve_entities).
9192
pub struct ReserveEntitiesIterator<'a> {
9293
// Metas, so we can recover the current generation for anything in the freelist.
9394
meta: &'a [EntityMeta],
@@ -165,7 +166,7 @@ pub struct Entities {
165166
}
166167

167168
impl Entities {
168-
/// Reserve entity IDs concurrently
169+
/// Reserve entity IDs concurrently.
169170
///
170171
/// Storage for entity generation and location is lazily allocated by calling `flush`.
171172
pub fn reserve_entities(&self, count: u32) -> ReserveEntitiesIterator {
@@ -207,7 +208,7 @@ impl Entities {
207208
}
208209
}
209210

210-
/// Reserve one entity ID concurrently
211+
/// Reserve one entity ID concurrently.
211212
///
212213
/// Equivalent to `self.reserve_entities(1).next().unwrap()`, but more efficient.
213214
pub fn reserve_entity(&self) -> Entity {
@@ -240,7 +241,7 @@ impl Entities {
240241
);
241242
}
242243

243-
/// Allocate an entity ID directly
244+
/// Allocate an entity ID directly.
244245
///
245246
/// Location should be written immediately.
246247
pub fn alloc(&mut self) -> Entity {
@@ -261,7 +262,7 @@ impl Entities {
261262
}
262263
}
263264

264-
/// Allocate a specific entity ID, overwriting its generation
265+
/// Allocate a specific entity ID, overwriting its generation.
265266
///
266267
/// Returns the location of the entity currently using the given ID, if any. Location should be
267268
/// written immediately.
@@ -293,7 +294,7 @@ impl Entities {
293294
loc
294295
}
295296

296-
/// Destroy an entity, allowing it to be reused
297+
/// Destroy an entity, allowing it to be reused.
297298
///
298299
/// Must not be called while reserved entities are awaiting `flush()`.
299300
pub fn free(&mut self, entity: Entity) -> Option<EntityLocation> {
@@ -315,7 +316,7 @@ impl Entities {
315316
Some(loc)
316317
}
317318

318-
/// Ensure at least `n` allocations can succeed without reallocating
319+
/// Ensure at least `n` allocations can succeed without reallocating.
319320
pub fn reserve(&mut self, additional: u32) {
320321
self.verify_flushed();
321322

@@ -340,7 +341,7 @@ impl Entities {
340341
*self.free_cursor.get_mut() = 0;
341342
}
342343

343-
/// Access the location storage of an entity
344+
/// Access the location storage of an entity.
344345
///
345346
/// Must not be called on pending entities.
346347
pub fn get_mut(&mut self, entity: Entity) -> Option<&mut EntityLocation> {
@@ -352,7 +353,7 @@ impl Entities {
352353
}
353354
}
354355

355-
/// Returns `Ok(Location { archetype: 0, index: undefined })` for pending entities
356+
/// Returns `Ok(Location { archetype: 0, index: undefined })` for pending entities.
356357
pub fn get(&self, entity: Entity) -> Option<EntityLocation> {
357358
if (entity.id as usize) < self.meta.len() {
358359
let meta = &self.meta[entity.id as usize];
@@ -368,6 +369,7 @@ impl Entities {
368369
/// Panics if the given id would represent an index outside of `meta`.
369370
///
370371
/// # Safety
372+
///
371373
/// Must only be called for currently allocated `id`s.
372374
pub unsafe fn resolve_unknown_gen(&self, id: u32) -> Entity {
373375
let meta_len = self.meta.len();
@@ -463,7 +465,7 @@ impl EntityMeta {
463465
};
464466
}
465467

466-
/// A location of an entity in an archetype
468+
/// A location of an entity in an archetype.
467469
#[derive(Copy, Clone, Debug)]
468470
pub struct EntityLocation {
469471
/// The archetype index

crates/bevy_ecs/src/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ enum State {
6969
///
7070
/// [`Events::update_system`] is a system that does this, typically intialized automatically using
7171
/// [`AppBuilder::add_event`]. [EventReader]s are expected to read events from this collection at
72-
/// least once per loop/frame.
72+
/// least once per loop/frame.
7373
/// Events will persist across a single frame boundary and so ordering of event producers and
7474
/// consumers is not critical (although poorly-planned ordering may cause accumulating lag).
7575
/// If events are not handled by the end of the frame after they are updated, they will be
@@ -116,6 +116,8 @@ enum State {
116116
/// when events are cleared.
117117
/// This complicates consumption and risks ever-expanding memory usage if not cleaned up,
118118
/// but can be done by adding your event as a resource instead of using [`AppBuilder::add_event`].
119+
///
120+
/// [`AppBuilder::add_event`]: https://docs.rs/bevy/*/bevy/app/struct.AppBuilder.html#method.add_event
119121
#[derive(Debug)]
120122
pub struct Events<T> {
121123
events_a: Vec<EventInstance<T>>,

crates/bevy_ecs/src/query/access.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use crate::storage::SparseSetIndex;
22
use fixedbitset::FixedBitSet;
33
use std::marker::PhantomData;
44

5+
/// `Access` keeps track of read and write accesses to values within a collection.
6+
///
7+
/// This is used for ensuring systems are executed soundly.
58
#[derive(Debug, Eq, PartialEq, Clone)]
69
pub struct Access<T: SparseSetIndex> {
710
reads_all: bool,
@@ -28,18 +31,21 @@ impl<T: SparseSetIndex> Access<T> {
2831
self.writes.grow(bits);
2932
}
3033

34+
/// Adds a read access for the given index.
3135
pub fn add_read(&mut self, index: T) {
3236
self.reads_and_writes.grow(index.sparse_set_index() + 1);
3337
self.reads_and_writes.insert(index.sparse_set_index());
3438
}
3539

40+
/// Adds a write access for the given index.
3641
pub fn add_write(&mut self, index: T) {
3742
self.reads_and_writes.grow(index.sparse_set_index() + 1);
3843
self.writes.grow(index.sparse_set_index() + 1);
3944
self.reads_and_writes.insert(index.sparse_set_index());
4045
self.writes.insert(index.sparse_set_index());
4146
}
4247

48+
/// Returns true if this `Access` contains a read access for the given index.
4349
pub fn has_read(&self, index: T) -> bool {
4450
if self.reads_all {
4551
true
@@ -48,30 +54,39 @@ impl<T: SparseSetIndex> Access<T> {
4854
}
4955
}
5056

57+
/// Returns true if this `Access` contains a write access for the given index.
5158
pub fn has_write(&self, index: T) -> bool {
5259
self.writes.contains(index.sparse_set_index())
5360
}
5461

62+
/// Sets this `Access` to having read access for all indices.
5563
pub fn read_all(&mut self) {
5664
self.reads_all = true;
5765
}
5866

67+
/// Returns true if this `Access` has read access to all indices.
5968
pub fn reads_all(&self) -> bool {
6069
self.reads_all
6170
}
6271

72+
/// Clears all recorded accesses.
6373
pub fn clear(&mut self) {
6474
self.reads_all = false;
6575
self.reads_and_writes.clear();
6676
self.writes.clear();
6777
}
6878

79+
/// Extends this `Access` with another, copying all accesses of `other` into this.
6980
pub fn extend(&mut self, other: &Access<T>) {
7081
self.reads_all = self.reads_all || other.reads_all;
7182
self.reads_and_writes.union_with(&other.reads_and_writes);
7283
self.writes.union_with(&other.writes);
7384
}
7485

86+
/// Returns true if this `Access` is compatible with `other`.
87+
///
88+
/// Two `Access` instances are incompatible with each other if one `Access` has a write for
89+
/// which the other also has a write or a read.
7590
pub fn is_compatible(&self, other: &Access<T>) -> bool {
7691
if self.reads_all {
7792
0 == other.writes.count_ones(..)
@@ -83,6 +98,7 @@ impl<T: SparseSetIndex> Access<T> {
8398
}
8499
}
85100

101+
/// Calculates conflicting accesses between this `Access` and `other`.
86102
pub fn get_conflicts(&self, other: &Access<T>) -> Vec<T> {
87103
let mut conflicts = FixedBitSet::default();
88104
if self.reads_all {

0 commit comments

Comments
 (0)