Skip to content

Commit faaed3a

Browse files
committed
documentation
1 parent 7b316af commit faaed3a

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

crates/bevy_ecs/src/entity/map_entities.rs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,30 @@ use crate::{
66

77
use super::EntityHashMap;
88

9-
/// Operation to map all contained [`Entity`] fields in a type to new values.
9+
/// Apply an operation to all entities in a read-only container.
1010
///
11-
/// As entity IDs are valid only for the [`World`] they're sourced from, using [`Entity`]
12-
/// as references in components copied from another world will be invalid. This trait
13-
/// allows defining custom mappings for these references via [`EntityMappers`](EntityMapper), which
14-
/// inject the entity mapping strategy between your `MapEntities` type and the current world
15-
/// (usually by using an [`EntityHashMap<Entity>`] between source entities and entities in the
16-
/// current world).
11+
/// This is implemented by default for types that implement [`IterEntities`] or
12+
/// where `&T` implements [`IntoIterator`].
1713
///
18-
/// Implementing this trait correctly is required for properly loading components
19-
/// with entity references from scenes.
20-
///
21-
/// ## Example
22-
///
23-
/// ```
24-
/// use bevy_ecs::prelude::*;
25-
/// use bevy_ecs::entity::MapEntities;
26-
///
27-
/// #[derive(Component)]
28-
/// struct Spring {
29-
/// a: Entity,
30-
/// b: Entity,
31-
/// }
32-
///
33-
/// impl MapEntities for Spring {
34-
/// fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
35-
/// self.a = entity_mapper.map_entity(self.a);
36-
/// self.b = entity_mapper.map_entity(self.b);
37-
/// }
38-
/// }
39-
/// ```
14+
/// It may be useful to implement directly for types that can't produce an
15+
/// iterator for lifetime reasons, such as those involving internal mutexes.
4016
pub trait MapEntities {
17+
/// Apply an operation to all contained entities.
4118
fn map_entities<F: FnMut(Entity)>(&self, f: F);
4219
}
4320

21+
/// Apply an operation to mutable references to all entities in a container.
22+
///
23+
/// This is implemented by default for types that implement [`IterEntitiesMut`]
24+
/// or where `&mut T` implements [`IntoIterator`].
25+
///
26+
/// It may be useful to implement directly for types that can't produce an
27+
/// iterator for lifetime reasons, such as those involving internal mutexes.
28+
///
29+
/// Because the operation receives mutable references, it may alter the
30+
/// contained entity IDs via a mechanism such as an [`EntityMapper`].
4431
pub trait MapEntitiesMut: MapEntities {
32+
/// Apply an operation to mutable references to all entities.
4533
fn map_entities_mut<F: FnMut(&mut Entity)>(&mut self, f: F);
4634
}
4735

@@ -63,7 +51,15 @@ where
6351
}
6452
}
6553

54+
/// Produce an iterator over all contained entities.
55+
///
56+
/// This is implemented by default for types where `&T` implements
57+
/// [`IntoIterator`].
58+
///
59+
/// It may be useful to implement directly for types that can't produce an
60+
/// iterator for lifetime reasons, such as those involving internal mutexes.
6661
pub trait IterEntities {
62+
/// Get an iterator over contained entities.
6763
fn iter_entities(&self) -> impl Iterator<Item = Entity>;
6864
}
6965

@@ -76,7 +72,18 @@ where
7672
}
7773
}
7874

75+
/// Apply an operation to mutable references to all entities in a container.
76+
///
77+
/// This is implemented by default for types where `&mut T` implements
78+
/// [`IntoIterator`].
79+
///
80+
/// It may be useful to implement directly for types that can't produce an
81+
/// iterator for lifetime reasons, such as those involving internal mutexes.
82+
///
83+
/// Because the iterator produces mutable references, consumers may alter the
84+
/// contained entity IDs via a mechanism such as an [`EntityMapper`].
7985
pub trait IterEntitiesMut: IterEntities {
86+
/// Get an iterator over mutable references to contained entities.
8087
fn iter_entities_mut(&mut self) -> impl Iterator<Item = &mut Entity>;
8188
}
8289

crates/bevy_ecs/src/reflect/map_entities.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ impl ReflectMapEntities {
2424
///
2525
/// Be mindful in its usage: Works best in situations where the entities in the [`EntityHashMap<Entity>`] are newly
2626
/// created, before systems have a chance to add new components. If some of the entities referred to
27-
/// by the [`EntityHashMap<Entity>`] might already contain valid entity references, you should use [`map_entities`](Self::map_entities).
27+
/// by the [`EntityHashMap<Entity>`] might already contain valid entity references, you should use
28+
/// [`map_world_entities`](Self::map_entities).
2829
///
2930
/// An example of this: A scene can be loaded with `Parent` components, but then a `Parent` component can be added
30-
/// to these entities after they have been loaded. If you reload the scene using [`map_all_entities`](Self::map_all_entities), those `Parent`
31+
/// to these entities after they have been loaded. If you reload the scene using [`map_all_world_entities`](Self::map_all_world_entities), those `Parent`
3132
/// components with already valid entity references could be updated to point at something else entirely.
3233
pub fn map_all_world_entities(
3334
&self,
@@ -38,11 +39,11 @@ impl ReflectMapEntities {
3839
}
3940

4041
/// A general method for applying [`MapEntities`] behavior to elements in an [`EntityHashMap<Entity>`]. Unlike
41-
/// [`map_all_entities`](Self::map_all_entities), this is applied to specific entities, not all values
42+
/// [`map_all_world_entities`](Self::map_all_world_entities), this is applied to specific entities, not all values
4243
/// in the [`EntityHashMap<Entity>`].
4344
///
4445
/// This is useful mostly for when you need to be careful not to update components that already contain valid entity
45-
/// values. See [`map_all_entities`](Self::map_all_entities) for more details.
46+
/// values. See [`map_all_world_entities`](Self::map_all_world_entities) for more details.
4647
pub fn map_world_entities(
4748
&self,
4849
world: &mut World,
@@ -54,10 +55,14 @@ impl ReflectMapEntities {
5455
});
5556
}
5657

58+
/// A general method for applying an operation to all entities in a
59+
/// reflected component.
5760
pub fn map_entities(&self, component: &dyn PartialReflect, f: &mut dyn FnMut(Entity)) {
5861
(self.map_entities)(component, f)
5962
}
6063

64+
/// A general method for applying an operation that may modify entities in a
65+
/// reflected component.
6166
pub fn map_entities_mut(
6267
&self,
6368
component: &mut dyn PartialReflect,

0 commit comments

Comments
 (0)