Skip to content

Commit

Permalink
fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySpruce committed Feb 26, 2025
1 parent 34eaaa6 commit 17be013
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 61 deletions.
5 changes: 3 additions & 2 deletions crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ struct Explode;
let mut world = World::new();
let entity = world.spawn_empty().id();

world.add_observer(|trigger: Trigger<Explode>, mut commands: Commands| {
world.add_observer(|trigger: Trigger<Explode>, mut commands: Commands| -> Result {
println!("Entity {} goes BOOM!", trigger.target());
commands.entity(trigger.target()).despawn();
commands.entity(trigger.target())?.despawn();
Ok(())
});

world.flush();
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ type IdCursor = isize;
/// # #[derive(Component)]
/// # struct Expired;
/// #
/// fn dispose_expired_food(mut commands: Commands, query: Query<Entity, With<Expired>>) {
/// fn dispose_expired_food(mut commands: Commands, query: Query<Entity, With<Expired>>) -> Result {
/// for food_entity in &query {
/// commands.entity(food_entity).despawn();
/// commands.entity(food_entity)?.despawn();
/// }
/// Ok(())
/// }
/// #
/// # bevy_ecs::system::assert_is_system(dispose_expired_food);
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ impl<'w, E, B: Bundle> Trigger<'w, E, B> {
/// # Examples
///
/// ```rust
/// # use bevy_ecs::prelude::{Commands, Trigger};
/// # use bevy_ecs::prelude::{Commands, Result, Trigger};
/// #
/// # struct MyEvent {
/// # done: bool,
/// # }
/// #
/// /// Handle `MyEvent` and if it is done, stop observation.
/// fn my_observer(trigger: Trigger<MyEvent>, mut commands: Commands) {
/// fn my_observer(trigger: Trigger<MyEvent>, mut commands: Commands) -> Result {
/// if trigger.event().done {
/// commands.entity(trigger.observer()).despawn();
/// return;
/// commands.entity(trigger.observer())?.despawn();
/// return Ok(());
/// }
///
/// // ...
/// Ok(())
/// }
/// ```
pub fn observer(&self) -> Entity {
Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_ecs/src/observer/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ pub type ObserverRunner = fn(DeferredWorld, ObserverTrigger, PtrMut, propagate:
/// #[derive(Event)]
/// struct Explode;
///
/// world.add_observer(|trigger: Trigger<Explode>, mut commands: Commands| {
/// world.add_observer(|trigger: Trigger<Explode>, mut commands: Commands| -> Result {
/// println!("Entity {} goes BOOM!", trigger.target());
/// commands.entity(trigger.target()).despawn();
/// commands.entity(trigger.target())?.despawn();
/// Ok(())
/// });
///
/// world.flush();
Expand Down Expand Up @@ -234,9 +235,10 @@ pub type ObserverRunner = fn(DeferredWorld, ObserverTrigger, PtrMut, propagate:
/// # let e2 = world.spawn_empty().id();
/// # #[derive(Event)]
/// # struct Explode;
/// world.entity_mut(e1).observe(|trigger: Trigger<Explode>, mut commands: Commands| {
/// world.entity_mut(e1).observe(|trigger: Trigger<Explode>, mut commands: Commands| -> Result {
/// println!("Boom!");
/// commands.entity(trigger.target()).despawn();
/// commands.entity(trigger.target())?.despawn();
/// Ok(())
/// });
///
/// world.entity_mut(e2).observe(|trigger: Trigger<Explode>, mut commands: Commands| {
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ pub trait ReflectCommandExt {
/// fn remove_reflect_component(
/// mut commands: Commands,
/// prefab: Res<Prefab>
/// ) {
/// ) -> Result {
/// // Prefab can hold any boxed reflect component or bundle. In this case either
/// // ComponentA, ComponentB, or BundleA. No matter which component or bundle is in the resource though,
/// // we can attempt to remove any component (or set of components in the case of a bundle)
/// // of that same type from an entity.
/// commands.entity(prefab.entity)
/// commands.entity(prefab.entity)?
/// .remove_reflect(prefab.data.reflect_type_path().to_owned());
/// Ok(())
/// }
/// ```
fn remove_reflect(&mut self, component_type_name: impl Into<Cow<'static, str>>) -> &mut Self;
Expand Down
73 changes: 43 additions & 30 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ impl<'w, 's> Commands<'w, 's> {
/// .insert((Strength(1), Agility(2)))
/// // adds a single component to the entity
/// .insert(Label("hello world"));
///
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(example_system);
Expand Down Expand Up @@ -1202,14 +1201,15 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Component)]
/// struct Level(u32);
///
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// .entry::<Level>()
/// // Modify the component if it exists
/// .and_modify(|mut lvl| lvl.0 += 1)
/// // Otherwise insert a default value
/// .or_insert(Level(0));
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(level_up_system);
/// ```
Expand Down Expand Up @@ -1250,9 +1250,9 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength,
/// }
///
/// fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// // You can insert individual components:
/// .insert(Defense(10))
/// // You can also insert pre-defined bundles of components:
Expand All @@ -1269,6 +1269,7 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength(40),
/// },
/// ));
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
/// ```
Expand Down Expand Up @@ -1298,11 +1299,12 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Component)]
/// struct Health(u32);
///
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// .insert_if(Health(10), || !player.is_spectator())
/// .remove::<StillLoadingStats>();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
Expand Down Expand Up @@ -1433,8 +1435,8 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength,
/// }
///
/// fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// commands.entity(player.entity)
/// fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands.entity(player.entity)?
/// // You can try_insert individual components:
/// .try_insert(Defense(10))
///
Expand All @@ -1445,12 +1447,14 @@ impl<'a> EntityCommands<'a> {
/// });
///
/// // Suppose this occurs in a parallel adjacent system or process
/// commands.entity(player.entity)
/// commands.entity(player.entity)?
/// .despawn();
///
/// commands.entity(player.entity)
/// commands.entity(player.entity)?
/// // This will not panic nor will it add the component
/// .try_insert(Defense(5));
///
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
/// ```
Expand All @@ -1474,14 +1478,16 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Component)]
/// struct Health(u32);
///
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// commands.entity(player.entity)
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands.entity(player.entity)?
/// .try_insert_if(Health(10), || !player.is_spectator())
/// .remove::<StillLoadingStats>();
///
/// commands.entity(player.entity)
/// commands.entity(player.entity)?
/// // This will not panic nor will it add the component
/// .try_insert_if(Health(5), || !player.is_spectator());
///
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
Expand Down Expand Up @@ -1521,14 +1527,16 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Component)]
/// struct Health(u32);
///
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// commands.entity(player.entity)
/// fn add_health_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands.entity(player.entity)?
/// .try_insert_if(Health(10), || player.is_spectator())
/// .remove::<StillLoadingStats>();
///
/// commands.entity(player.entity)
/// commands.entity(player.entity)?
/// // This will not panic nor will it overwrite the component
/// .try_insert_if_new_and(Health(5), || player.is_spectator());
///
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
Expand Down Expand Up @@ -1583,16 +1591,17 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength,
/// }
///
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// // You can remove individual components:
/// .remove::<Defense>()
/// // You can also remove pre-defined Bundles of components:
/// .remove::<CombatBundle>()
/// // You can also remove tuples of components and bundles.
/// // This is equivalent to the calls above:
/// .remove::<(Defense, CombatBundle)>();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(remove_combat_stats_system);
/// ```
Expand Down Expand Up @@ -1630,16 +1639,17 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength,
/// }
///
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// // You can remove individual components:
/// .try_remove::<Defense>()
/// // You can also remove pre-defined Bundles of components:
/// .try_remove::<CombatBundle>()
/// // You can also remove tuples of components and bundles.
/// // This is equivalent to the calls above:
/// .try_remove::<(Defense, CombatBundle)>();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(remove_combat_stats_system);
/// ```
Expand All @@ -1666,11 +1676,12 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Resource)]
/// struct PlayerEntity { entity: Entity }
///
/// fn remove_with_requires_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn remove_with_requires_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// // Remove both A and B components from the entity, because B is required by A
/// .remove_with_requires::<A>();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(remove_with_requires_system);
/// ```
Expand Down Expand Up @@ -1717,9 +1728,9 @@ impl<'a> EntityCommands<'a> {
/// fn remove_character_system(
/// mut commands: Commands,
/// character_to_remove: Res<CharacterToRemove>
/// )
/// {
/// commands.entity(character_to_remove.entity).despawn();
/// ) -> Result {
/// commands.entity(character_to_remove.entity)?.despawn();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(remove_character_system);
/// ```
Expand Down Expand Up @@ -1853,16 +1864,17 @@ impl<'a> EntityCommands<'a> {
/// strength: Strength,
/// }
///
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// // You can retain a pre-defined Bundle of components,
/// // with this removing only the Defense component
/// .retain::<CombatBundle>()
/// // You can also retain only a single component
/// .retain::<Health>()
/// // And you can remove all the components by passing in an empty Bundle
/// .retain::<()>();
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(remove_combat_stats_system);
/// ```
Expand Down Expand Up @@ -2178,9 +2190,9 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
/// #[derive(Component)]
/// struct Level(u32);
///
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) -> Result {
/// commands
/// .entity(player.entity)
/// .entity(player.entity)?
/// .entry::<Level>()
/// // Modify the component if it exists
/// .and_modify(|mut lvl| lvl.0 += 1)
Expand All @@ -2190,6 +2202,7 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
/// .entity()
/// // And continue chaining method calls
/// .insert(Name::new("Player"));
/// Ok(())
/// }
/// # bevy_ecs::system::assert_is_system(level_up_system);
/// ```
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/commands/parallel_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct ParallelCommandQueue {
/// query.par_iter().for_each(|(entity, velocity)| {
/// if velocity.magnitude() > 10.0 {
/// par_commands.command_scope(|mut commands| {
/// commands.entity(entity).despawn();
/// commands.entity(entity).unwrap().despawn();
/// });
/// }
/// });
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_picking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
//! let drag = trigger.event();
//! transform.rotate_local_y(drag.delta.x / 50.0);
//! })
//! .observe(|trigger: Trigger<Pointer<Click>>, mut commands: Commands| {
//! .observe(|trigger: Trigger<Pointer<Click>>, mut commands: Commands| -> Result {
//! println!("Entity {} goes BOOM!", trigger.target());
//! commands.entity(trigger.target()).despawn();
//! commands.entity(trigger.target())?.despawn();
//! Ok(())
//! })
//! .observe(|trigger: Trigger<Pointer<Over>>, mut events: EventWriter<Greeting>| {
//! events.write(Greeting);
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/extract_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ use core::ops::{Deref, DerefMut};
/// # #[derive(Component)]
/// // Do make sure to sync the cloud entities before extracting them.
/// # struct Cloud;
/// fn extract_clouds(mut commands: Commands, clouds: Extract<Query<RenderEntity, With<Cloud>>>) {
/// fn extract_clouds(mut commands: Commands, clouds: Extract<Query<RenderEntity, With<Cloud>>>) -> Result {
/// for cloud in &clouds {
/// commands.entity(cloud).insert(Cloud);
/// commands.entity(cloud)?.insert(Cloud);
/// }
/// Ok(())
/// }
/// ```
///
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl GlobalTransform {
///
/// ```
/// # use bevy_transform::prelude::{GlobalTransform, Transform};
/// # use bevy_ecs::prelude::{Entity, Query, Component, Commands};
/// # use bevy_ecs::prelude::{Entity, Query, Component, Commands, Result};
/// #[derive(Component)]
/// struct ToReparent {
/// new_parent: Entity,
Expand All @@ -168,15 +168,16 @@ impl GlobalTransform {
/// mut commands: Commands,
/// mut targets: Query<(&mut Transform, Entity, &GlobalTransform, &ToReparent)>,
/// transforms: Query<&GlobalTransform>,
/// ) {
/// ) -> Result {
/// for (mut transform, entity, initial, to_reparent) in targets.iter_mut() {
/// if let Ok(parent_transform) = transforms.get(to_reparent.new_parent) {
/// *transform = initial.reparented_to(parent_transform);
/// commands.entity(entity)
/// commands.entity(entity)?
/// .remove::<ToReparent>()
/// .set_parent(to_reparent.new_parent);
/// }
/// }
/// Ok(())
/// }
/// ```
///
Expand Down
Loading

0 comments on commit 17be013

Please sign in to comment.