Skip to content

Commit 615d43b

Browse files
committed
Improve bevy_ecs and bevy_app API docs where referenced by the new Bevy Book (#2365)
## Objective The upcoming Bevy Book makes many references to the API documentation of bevy. Most references belong to the first two chapters of the Bevy Book: - bevyengine/bevy-website#176 - bevyengine/bevy-website#182 This PR attempts to improve the documentation of `bevy_ecs` and `bevy_app` in order to help readers of the Book who want to delve deeper into technical details. ## Solution - Add crate and level module documentation - Document the most important items (basically those included in the preludes), with the following style, where applicable: - **Summary.** Short description of the item. - **Second paragraph.** Detailed description of the item, without going too much in the implementation. - **Code example(s).** - **Safety or panic notes.** ## Collaboration Any kind of collaboration is welcome, especially corrections, wording, new ideas and guidelines on where the focus should be put in. --- ### Related issues - Fixes #2246
1 parent 064af63 commit 615d43b

File tree

18 files changed

+1348
-202
lines changed

18 files changed

+1348
-202
lines changed

crates/bevy_app/src/app.rs

Lines changed: 314 additions & 59 deletions
Large diffs are not rendered by default.

crates/bevy_app/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! This crate is about everything concerning the highest-level, application layer of a Bevy
2+
//! app.
3+
14
mod app;
25
mod plugin;
36
mod plugin_group;
@@ -21,9 +24,14 @@ pub mod prelude {
2124
use bevy_ecs::schedule::StageLabel;
2225

2326
/// The names of the default App stages
27+
///
28+
/// The relative stages are added by [`App::add_default_stages`].
2429
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
2530
pub enum CoreStage {
26-
/// Runs once at the beginning of the app.
31+
/// Runs only once at the beginning of the app.
32+
///
33+
/// Consists of the sub-stages defined in [`StartupStage`]. Systems added here are
34+
/// referred to as "startup systems".
2735
Startup,
2836
/// Name of app stage that runs before all other app stages
2937
First,

crates/bevy_ecs/src/archetype.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Types for defining [`Archetype`]s, collections of entities that have the same set of
2+
//! components.
3+
14
use crate::{
25
bundle::BundleId,
36
component::{ComponentId, StorageType},

crates/bevy_ecs/src/bundle.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Types for handling [`Bundle`]s.
2+
//!
3+
//! This module contains the `Bundle` trait and some other helper types.
4+
15
pub use bevy_ecs_macros::Bundle;
26

37
use crate::{
@@ -9,15 +13,35 @@ use crate::{
913
use bevy_ecs_macros::all_tuples;
1014
use std::{any::TypeId, collections::HashMap};
1115

12-
/// An ordered collection of components, commonly used for spawning entities, and adding and
13-
/// removing components in bulk.
16+
/// An ordered collection of components.
17+
///
18+
/// Commonly used for spawning entities and adding and removing components in bulk. This
19+
/// trait is automatically implemented for tuples of components: `(ComponentA, ComponentB)`
20+
/// is a very convenient shorthand when working with one-off collections of components. Note
21+
/// that both the unit type `()` and `(ComponentA, )` are valid bundles. The unit bundle is
22+
/// particularly useful for spawning multiple empty entities by using
23+
/// [`Commands::spawn_batch`](crate::system::Commands::spawn_batch).
24+
///
25+
/// # Examples
1426
///
15-
/// Typically, you will simply use `#[derive(Bundle)]` when creating your own `Bundle`.
16-
/// The `Bundle` trait is automatically implemented for tuples of components:
17-
/// `(ComponentA, ComponentB)` is a very convenient shorthand when working with one-off collections
18-
/// of components. Note that both `()` and `(ComponentA, )` are valid tuples.
27+
/// Typically, you will simply use `#[derive(Bundle)]` when creating your own `Bundle`. Each
28+
/// struct field is a component:
1929
///
20-
/// You can nest bundles like so:
30+
/// ```
31+
/// # use bevy_ecs::prelude::*;
32+
/// # struct ComponentA;
33+
/// # struct ComponentB;
34+
/// # struct ComponentC;
35+
/// #
36+
/// #[derive(Bundle)]
37+
/// struct MyBundle {
38+
/// a: ComponentA,
39+
/// b: ComponentB,
40+
/// c: ComponentC,
41+
/// }
42+
/// ```
43+
///
44+
/// You can nest bundles using the `#[bundle]` attribute:
2145
/// ```
2246
/// # use bevy_ecs::bundle::Bundle;
2347
///
@@ -36,10 +60,11 @@ use std::{any::TypeId, collections::HashMap};
3660
/// ```
3761
///
3862
/// # Safety
39-
/// [Bundle::component_ids] must return the ComponentId for each component type in the bundle, in the
40-
/// _exact_ order that [Bundle::get_components] is called.
41-
/// [Bundle::from_components] must call `func` exactly once for each [ComponentId] returned by
42-
/// [Bundle::component_ids]
63+
///
64+
/// - [Bundle::component_ids] must return the ComponentId for each component type in the bundle, in the
65+
/// _exact_ order that [Bundle::get_components] is called.
66+
/// - [Bundle::from_components] must call `func` exactly once for each [ComponentId] returned by
67+
/// [Bundle::component_ids].
4368
pub unsafe trait Bundle: Send + Sync + 'static {
4469
/// Gets this [Bundle]'s component ids, in the order of this bundle's Components
4570
fn component_ids(components: &mut Components) -> Vec<ComponentId>;

crates/bevy_ecs/src/change_detection.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Types that detect when their internal data mutate.
2+
13
use crate::component::{Component, ComponentTicks};
24
use bevy_reflect::Reflect;
35
use std::ops::{Deref, DerefMut};
@@ -136,9 +138,13 @@ pub(crate) struct Ticks<'a> {
136138

137139
/// Unique mutable borrow of a resource.
138140
///
141+
/// See the [`World`](crate::world::World) documentation to see the usage of a resource.
142+
///
143+
/// If you need a shared borrow, use [`Res`](crate::system::Res) instead.
144+
///
139145
/// # Panics
140146
///
141-
/// Panics when used as a [`SystemParameter`](crate::system::SystemParam) if the resource does not exist.
147+
/// Panics when used as a [`SystemParam`](crate::system::SystemParam) if the resource does not exist.
142148
///
143149
/// Use `Option<ResMut<T>>` instead if the resource might not always exist.
144150
pub struct ResMut<'a, T: Component> {
@@ -152,7 +158,7 @@ impl_debug!(ResMut<'a, T>, Component);
152158

153159
/// Unique borrow of a non-[`Send`] resource.
154160
///
155-
/// Only [`Send`] resources may be accessed with the [`ResMut`] [`crate::system::SystemParam`]. In case that the
161+
/// Only [`Send`] resources may be accessed with the [`ResMut`] [`SystemParam`](crate::system::SystemParam). In case that the
156162
/// resource does not implement `Send`, this `SystemParam` wrapper can be used. This will instruct
157163
/// the scheduler to instead run the system on the main thread so that it doesn't send the resource
158164
/// over to another thread.

crates/bevy_ecs/src/component.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Types for declaring and storing [`Component`]s.
2+
13
use crate::storage::SparseSetIndex;
24
use std::{
35
alloc::Layout,

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
//! Entity handling types.
2+
//!
3+
//! In Bevy ECS, there is no monolithic data structure for an entity. Instead, the [`Entity`]
4+
//! `struct` is just a *generational index* (a combination of an ID and a generation). Then,
5+
//! the `Entity` maps to the specific [`Component`s](crate::component::Component). This way,
6+
//! entities can have meaningful data attached to it. This is a fundamental design choice
7+
//! that has been taken to enhance performance and usability.
8+
//!
9+
//! # Usage
10+
//!
11+
//! Here are links to the methods used to perform common operations
12+
//! involving entities:
13+
//!
14+
//! - **Spawning an empty entity:** use [`Commands::spawn`](crate::system::Commands::spawn).
15+
//! - **Spawning an entity with components:** use
16+
//! [`Commands::spawn_bundle`](crate::system::Commands::spawn_bundle).
17+
//! - **Despawning an entity:** use
18+
//! [`EntityCommands::despawn`](crate::system::EntityCommands::despawn).
19+
//! - **Inserting a component to an entity:** use
20+
//! [`EntityCommands::insert`](crate::system::EntityCommands::insert).
21+
//! - **Adding multiple components to an entity:** use
22+
//! [`EntityCommands::insert_bundle`](crate::system::EntityCommands::insert_bundle).
23+
//! - **Removing a component to an entity:** use
24+
//! [`EntityCommands::remove`](crate::system::EntityCommands::remove).
125
mod map_entities;
226
mod serde;
327

@@ -36,9 +60,11 @@ impl Entity {
3660
/// Creates a new entity reference with a generation of 0.
3761
///
3862
/// # Note
39-
/// Spawning a specific `entity` value is rarely the right choice. Most apps should favor [`Commands::spawn`].
40-
/// This method should generally only be used for sharing entities across apps, and only when they have a
41-
/// scheme worked out to share an ID space (which doesn't happen by default).
63+
///
64+
/// Spawning a specific `entity` value is rarely the right choice. Most apps should favor
65+
/// [`Commands::spawn`](crate::system::Commands::spawn). This method should generally
66+
/// only be used for sharing entities across apps, and only when they have a scheme
67+
/// worked out to share an ID space (which doesn't happen by default).
4268
pub fn new(id: u32) -> Entity {
4369
Entity { id, generation: 0 }
4470
}

crates/bevy_ecs/src/event.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Event handling types.
2+
13
use crate as bevy_ecs;
24
use crate::{
35
component::Component,

crates/bevy_ecs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod storage;
1212
pub mod system;
1313
pub mod world;
1414

15+
/// Most commonly used re-exported types.
1516
pub mod prelude {
1617
#[doc(hidden)]
1718
#[cfg(feature = "bevy_reflect")]

crates/bevy_ecs/src/reflect.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Types that enable reflection support.
2+
13
pub use crate::change_detection::ReflectMut;
24
use crate::{
35
component::Component,

0 commit comments

Comments
 (0)