Skip to content

Commit

Permalink
Impove documentation for Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed May 10, 2022
1 parent 2c267ef commit f3ba78b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
29 changes: 25 additions & 4 deletions ecs/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,35 @@ mod command;
mod command_buffer;

pub(crate) use command::Command;
pub(crate) use command_buffer::CommandBuffer;
pub use command_buffer::CommandBuffer;

use crate::{Entities, Entity};

/// Allows commands to be issued without exclusive access the world by deferring them to be run at
/// a later state, when `World::maintain` is called.
/// Issuing commands on a command buffer after calling `World::maintain` since creation will result
/// in a panic.
/// a later state, when exclusive access to the world is available.
/// # Examples
/// ```
/// # struct Position(f32, f32);
/// # use ecs::{CommandBuffer, Commands, World};
/// let mut world = World::default();
/// let e1 = world.spawn();
///
/// let mut command_buffer = CommandBuffer::new();
/// let mut commands = Commands::new(&mut command_buffer, world.entities());
///
/// let e2 = commands.spawn();
/// commands.add(e2, Position(0.0, 0.0));
/// commands.despawn(e1);
///
/// assert!(world.entities().exists(e2)); // NOTE: some commands, like spawning entities happen immediately
/// assert!(world.entities().exists(e1)); // but others happen when the command buffer is applied
/// assert!(world.get::<Position>(e2).is_none());
///
/// command_buffer.apply(&mut world);
///
/// assert!(!world.entities().exists(e1));
/// assert!(world.entities().exists(e2));
/// ```
pub struct Commands<'b, 'e> {
buffer: &'b mut CommandBuffer,
entities: &'e Entities,
Expand Down
7 changes: 2 additions & 5 deletions ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod error;
mod query;
mod world;

pub use commands::Commands;
pub use commands::{CommandBuffer, Commands};
pub use entity::{Entities, Entity};
pub use error::BorrowMutError;
pub use query::{as_mut_lt, as_ref_lt, ComponentQuery, Query};
Expand All @@ -26,10 +26,7 @@ mod tests {
time::{Duration, Instant},
};

use crate::{
commands::CommandBuffer,
component::{ComponentRegistry, Storage, StorageType},
};
use crate::component::{ComponentRegistry, Storage, StorageType};

use super::*;

Expand Down

0 comments on commit f3ba78b

Please sign in to comment.