bevy_ecs/component/mod.rs
1//! Types for declaring and storing [`Component`]s.
2
3mod clone;
4mod constants;
5mod info;
6mod register;
7mod required;
8
9pub use clone::*;
10pub use constants::*;
11pub use info::*;
12pub use register::*;
13pub use required::*;
14
15use crate::{
16 entity::EntityMapper,
17 lifecycle::ComponentHook,
18 relationship::ComponentRelationshipAccessor,
19 system::{Local, SystemParam},
20 world::{FromWorld, World},
21};
22pub use bevy_ecs_macros::Component;
23use core::{fmt::Debug, marker::PhantomData, ops::Deref};
24
25/// A data type that can be used to store data for an [entity].
26///
27/// `Component` is a [derivable trait]: this means that a data type can implement it by applying a `#[derive(Component)]` attribute to it.
28/// However, components must always satisfy the `Send + Sync + 'static` trait bounds.
29///
30/// [entity]: crate::entity
31/// [derivable trait]: https://doc.rust-lang.org/book/appendix-03-derivable-traits.html
32///
33/// # Examples
34///
35/// Components can take many forms: they are usually structs, but can also be of every other kind of data type, like enums or zero sized types.
36/// The following examples show how components are laid out in code.
37///
38/// ```
39/// # use bevy_ecs::component::Component;
40/// # struct Color;
41/// #
42/// // A component can contain data...
43/// #[derive(Component)]
44/// struct LicensePlate(String);
45///
46/// // ... but it can also be a zero-sized marker.
47/// #[derive(Component)]
48/// struct Car;
49///
50/// // Components can also be structs with named fields...
51/// #[derive(Component)]
52/// struct VehiclePerformance {
53/// acceleration: f32,
54/// top_speed: f32,
55/// handling: f32,
56/// }
57///
58/// // ... or enums.
59/// #[derive(Component)]
60/// enum WheelCount {
61/// Two,
62/// Three,
63/// Four,
64/// }
65/// ```
66///
67/// # Component and data access
68///
69/// Components can be marked as immutable by adding the `#[component(immutable)]`
70/// attribute when using the derive macro.
71/// See the documentation for [`ComponentMutability`] for more details around this
72/// feature.
73///
74/// See the [`entity`] module level documentation to learn how to add or remove components from an entity.
75///
76/// See the documentation for [`Query`] to learn how to access component data from a system.
77///
78/// [`entity`]: crate::entity#usage
79/// [`Query`]: crate::system::Query
80/// [`ComponentMutability`]: crate::component::ComponentMutability
81///
82/// # Choosing a storage type
83///
84/// Components can be stored in the world using different strategies with their own performance implications.
85/// By default, components are added to the [`Table`] storage, which is optimized for query iteration.
86///
87/// Alternatively, components can be added to the [`SparseSet`] storage, which is optimized for component insertion and removal.
88/// This is achieved by adding an additional `#[component(storage = "SparseSet")]` attribute to the derive one:
89///
90/// ```
91/// # use bevy_ecs::component::Component;
92/// #
93/// #[derive(Component)]
94/// #[component(storage = "SparseSet")]
95/// struct ComponentA;
96/// ```
97///
98/// [`Table`]: crate::storage::Table
99/// [`SparseSet`]: crate::storage::SparseSet
100///
101/// # Required Components
102///
103/// Components can specify Required Components. If some [`Component`] `A` requires [`Component`] `B`, then when `A` is inserted,
104/// `B` will _also_ be initialized and inserted (if it was not manually specified).
105///
106/// The [`Default`] constructor will be used to initialize the component, by default:
107///
108/// ```
109/// # use bevy_ecs::prelude::*;
110/// #[derive(Component)]
111/// #[require(B)]
112/// struct A;
113///
114/// #[derive(Component, Default, PartialEq, Eq, Debug)]
115/// struct B(usize);
116///
117/// # let mut world = World::default();
118/// // This will implicitly also insert B with the Default constructor
119/// let id = world.spawn(A).id();
120/// assert_eq!(&B(0), world.entity(id).get::<B>().unwrap());
121///
122/// // This will _not_ implicitly insert B, because it was already provided
123/// world.spawn((A, B(11)));
124/// ```
125///
126/// Components can have more than one required component:
127///
128/// ```
129/// # use bevy_ecs::prelude::*;
130/// #[derive(Component)]
131/// #[require(B, C)]
132/// struct A;
133///
134/// #[derive(Component, Default, PartialEq, Eq, Debug)]
135/// #[require(C)]
136/// struct B(usize);
137///
138/// #[derive(Component, Default, PartialEq, Eq, Debug)]
139/// struct C(u32);
140///
141/// # let mut world = World::default();
142/// // This will implicitly also insert B and C with their Default constructors
143/// let id = world.spawn(A).id();
144/// assert_eq!(&B(0), world.entity(id).get::<B>().unwrap());
145/// assert_eq!(&C(0), world.entity(id).get::<C>().unwrap());
146/// ```
147///
148/// You can define inline component values that take the following forms:
149/// ```
150/// # use bevy_ecs::prelude::*;
151/// #[derive(Component)]
152/// #[require(
153/// B(1), // tuple structs
154/// C { // named-field structs
155/// x: 1,
156/// ..Default::default()
157/// },
158/// D::One, // enum variants
159/// E::ONE, // associated consts
160/// F::new(1) // constructors
161/// )]
162/// struct A;
163///
164/// #[derive(Component, PartialEq, Eq, Debug)]
165/// struct B(u8);
166///
167/// #[derive(Component, PartialEq, Eq, Debug, Default)]
168/// struct C {
169/// x: u8,
170/// y: u8,
171/// }
172///
173/// #[derive(Component, PartialEq, Eq, Debug)]
174/// enum D {
175/// Zero,
176/// One,
177/// }
178///
179/// #[derive(Component, PartialEq, Eq, Debug)]
180/// struct E(u8);
181///
182/// impl E {
183/// pub const ONE: Self = Self(1);
184/// }
185///
186/// #[derive(Component, PartialEq, Eq, Debug)]
187/// struct F(u8);
188///
189/// impl F {
190/// fn new(value: u8) -> Self {
191/// Self(value)
192/// }
193/// }
194///
195/// # let mut world = World::default();
196/// let id = world.spawn(A).id();
197/// assert_eq!(&B(1), world.entity(id).get::<B>().unwrap());
198/// assert_eq!(&C { x: 1, y: 0 }, world.entity(id).get::<C>().unwrap());
199/// assert_eq!(&D::One, world.entity(id).get::<D>().unwrap());
200/// assert_eq!(&E(1), world.entity(id).get::<E>().unwrap());
201/// assert_eq!(&F(1), world.entity(id).get::<F>().unwrap());
202/// ````
203///
204///
205/// You can also define arbitrary expressions by using `=`
206///
207/// ```
208/// # use bevy_ecs::prelude::*;
209/// #[derive(Component)]
210/// #[require(C = init_c())]
211/// struct A;
212///
213/// #[derive(Component, PartialEq, Eq, Debug)]
214/// #[require(C = C(20))]
215/// struct B;
216///
217/// #[derive(Component, PartialEq, Eq, Debug)]
218/// struct C(usize);
219///
220/// fn init_c() -> C {
221/// C(10)
222/// }
223///
224/// # let mut world = World::default();
225/// // This will implicitly also insert C with the init_c() constructor
226/// let id = world.spawn(A).id();
227/// assert_eq!(&C(10), world.entity(id).get::<C>().unwrap());
228///
229/// // This will implicitly also insert C with the `|| C(20)` constructor closure
230/// let id = world.spawn(B).id();
231/// assert_eq!(&C(20), world.entity(id).get::<C>().unwrap());
232/// ```
233///
234/// Required components are _recursive_. This means, if a Required Component has required components,
235/// those components will _also_ be inserted if they are missing:
236///
237/// ```
238/// # use bevy_ecs::prelude::*;
239/// #[derive(Component)]
240/// #[require(B)]
241/// struct A;
242///
243/// #[derive(Component, Default, PartialEq, Eq, Debug)]
244/// #[require(C)]
245/// struct B(usize);
246///
247/// #[derive(Component, Default, PartialEq, Eq, Debug)]
248/// struct C(u32);
249///
250/// # let mut world = World::default();
251/// // This will implicitly also insert B and C with their Default constructors
252/// let id = world.spawn(A).id();
253/// assert_eq!(&B(0), world.entity(id).get::<B>().unwrap());
254/// assert_eq!(&C(0), world.entity(id).get::<C>().unwrap());
255/// ```
256///
257/// Note that cycles in the "component require tree" will result in stack overflows when attempting to
258/// insert a component.
259///
260/// This "multiple inheritance" pattern does mean that it is possible to have duplicate `require`s for a given type
261/// at different levels of the inheritance tree:
262///
263/// ```
264/// # use bevy_ecs::prelude::*;
265/// #[derive(Component)]
266/// struct X(usize);
267///
268/// #[derive(Component, Default)]
269/// #[require(X(1))]
270/// struct Y;
271///
272/// #[derive(Component)]
273/// #[require(
274/// Y,
275/// X(2),
276/// )]
277/// struct Z;
278///
279/// # let mut world = World::default();
280/// // In this case, the x2 constructor is used for X
281/// let id = world.spawn(Z).id();
282/// assert_eq!(2, world.entity(id).get::<X>().unwrap().0);
283/// ```
284///
285/// In general, this shouldn't happen often, but when it does the algorithm for choosing the constructor from the tree is simple and predictable:
286/// 1. A constructor from a direct `#[require()]`, if one exists, is selected with priority.
287/// 2. Otherwise, perform a Depth First Search on the tree of requirements and select the first one found.
288///
289/// From a user perspective, just think about this as the following:
290/// 1. Specifying a required component constructor for Foo directly on a spawned component Bar will result in that constructor being used (and overriding existing constructors lower in the inheritance tree). This is the classic "inheritance override" behavior people expect.
291/// 2. For cases where "multiple inheritance" results in constructor clashes, Components should be listed in "importance order". List a component earlier in the requirement list to initialize its inheritance tree earlier.
292///
293/// ## Registering required components at runtime
294///
295/// In most cases, required components should be registered using the `require` attribute as shown above.
296/// However, in some cases, it may be useful to register required components at runtime.
297///
298/// This can be done through [`World::register_required_components`] or [`World::register_required_components_with`]
299/// for the [`Default`] and custom constructors respectively:
300///
301/// ```
302/// # use bevy_ecs::prelude::*;
303/// #[derive(Component)]
304/// struct A;
305///
306/// #[derive(Component, Default, PartialEq, Eq, Debug)]
307/// struct B(usize);
308///
309/// #[derive(Component, PartialEq, Eq, Debug)]
310/// struct C(u32);
311///
312/// # let mut world = World::default();
313/// // Register B as required by A and C as required by B.
314/// world.register_required_components::<A, B>();
315/// world.register_required_components_with::<B, C>(|| C(2));
316///
317/// // This will implicitly also insert B with its Default constructor
318/// // and C with the custom constructor defined by B.
319/// let id = world.spawn(A).id();
320/// assert_eq!(&B(0), world.entity(id).get::<B>().unwrap());
321/// assert_eq!(&C(2), world.entity(id).get::<C>().unwrap());
322/// ```
323///
324/// Similar rules as before apply to duplicate `require`s for a given type at different levels
325/// of the inheritance tree. `A` requiring `C` directly would take precedence over indirectly
326/// requiring it through `A` requiring `B` and `B` requiring `C`.
327///
328/// Unlike with the `require` attribute, directly requiring the same component multiple times
329/// for the same component will result in a panic. This is done to prevent conflicting constructors
330/// and confusing ordering dependencies.
331///
332/// Note that requirements must currently be registered before the requiring component is inserted
333/// into the world for the first time. Registering requirements after this will lead to a panic.
334///
335/// # Relationships between Entities
336///
337/// Sometimes it is useful to define relationships between entities. A common example is the
338/// parent / child relationship. Since Components are how data is stored for Entities, one might
339/// naturally think to create a Component which has a field of type [`Entity`].
340///
341/// To facilitate this pattern, Bevy provides the [`Relationship`](`crate::relationship::Relationship`)
342/// trait. You can derive the [`Relationship`](`crate::relationship::Relationship`) and
343/// [`RelationshipTarget`](`crate::relationship::RelationshipTarget`) traits in addition to the
344/// Component trait in order to implement data driven relationships between entities, see the trait
345/// docs for more details.
346///
347/// In addition, Bevy provides canonical implementations of the parent / child relationship via the
348/// [`ChildOf`](crate::hierarchy::ChildOf) [`Relationship`](crate::relationship::Relationship) and
349/// the [`Children`](crate::hierarchy::Children)
350/// [`RelationshipTarget`](crate::relationship::RelationshipTarget).
351///
352/// # Adding component's hooks
353///
354/// See [`ComponentHooks`] for a detailed explanation of component's hooks.
355///
356/// Alternatively to the example shown in [`ComponentHooks`]' documentation, hooks can be configured using following attributes:
357/// - `#[component(on_add = on_add_function)]`
358/// - `#[component(on_insert = on_insert_function)]`
359/// - `#[component(on_discard = on_discard_function)]`
360/// - `#[component(on_remove = on_remove_function)]`
361///
362/// ```
363/// # use bevy_ecs::component::Component;
364/// # use bevy_ecs::lifecycle::HookContext;
365/// # use bevy_ecs::world::DeferredWorld;
366/// # use bevy_ecs::entity::Entity;
367/// # use bevy_ecs::component::ComponentId;
368/// # use core::panic::Location;
369/// #
370/// #[derive(Component)]
371/// #[component(on_add = my_on_add_hook)]
372/// #[component(on_insert = my_on_insert_hook)]
373/// // Another possible way of configuring hooks:
374/// // #[component(on_add = my_on_add_hook, on_insert = my_on_insert_hook)]
375/// //
376/// // We don't have a discard or remove hook, so we can leave them out:
377/// // #[component(on_discard = my_on_discard_hook, on_remove = my_on_remove_hook)]
378/// struct ComponentA;
379///
380/// fn my_on_add_hook(world: DeferredWorld, context: HookContext) {
381/// // ...
382/// }
383///
384/// // You can also destructure items directly in the signature
385/// fn my_on_insert_hook(world: DeferredWorld, HookContext { caller, .. }: HookContext) {
386/// // ...
387/// }
388/// ```
389///
390/// This also supports function calls that yield closures
391///
392/// ```
393/// # use bevy_ecs::component::Component;
394/// # use bevy_ecs::lifecycle::HookContext;
395/// # use bevy_ecs::world::DeferredWorld;
396/// #
397/// #[derive(Component)]
398/// #[component(on_add = my_msg_hook("hello"))]
399/// #[component(on_despawn = my_msg_hook("yoink"))]
400/// struct ComponentA;
401///
402/// // a hook closure generating function
403/// fn my_msg_hook(message: &'static str) -> impl Fn(DeferredWorld, HookContext) {
404/// move |_world, _ctx| {
405/// println!("{message}");
406/// }
407/// }
408///
409/// ```
410///
411/// A hook's function path can be elided if it is `Self::on_add`, `Self::on_insert` etc.
412/// ```
413/// # use bevy_ecs::lifecycle::HookContext;
414/// # use bevy_ecs::prelude::*;
415/// # use bevy_ecs::world::DeferredWorld;
416/// #
417/// #[derive(Component, Debug)]
418/// #[component(on_add)]
419/// struct DoubleOnSpawn(usize);
420///
421/// impl DoubleOnSpawn {
422/// fn on_add(mut world: DeferredWorld, context: HookContext) {
423/// let mut entity = world.get_mut::<Self>(context.entity).unwrap();
424/// entity.0 *= 2;
425/// }
426/// }
427/// #
428/// # let mut world = World::new();
429/// # let entity = world.spawn(DoubleOnSpawn(2));
430/// # assert_eq!(entity.get::<DoubleOnSpawn>().unwrap().0, 4);
431/// ```
432///
433/// # Setting the clone behavior
434///
435/// You can specify how the [`Component`] is cloned when deriving it.
436///
437/// Your options are the functions and variants of [`ComponentCloneBehavior`]
438/// See [Clone Behaviors section of `EntityCloner`](crate::entity::EntityCloner#clone-behaviors) to understand how this affects handler priority.
439/// ```
440/// # use bevy_ecs::prelude::*;
441///
442/// #[derive(Component)]
443/// #[component(clone_behavior = Ignore)]
444/// struct MyComponent;
445///
446/// ```
447///
448/// # Implementing the trait for foreign types
449///
450/// As a consequence of the [orphan rule], it is not possible to separate into two different crates the implementation of `Component` from the definition of a type.
451/// This means that it is not possible to directly have a type defined in a third party library as a component.
452/// This important limitation can be easily worked around using the [newtype pattern]:
453/// this makes it possible to locally define and implement `Component` for a tuple struct that wraps the foreign type.
454/// The following example gives a demonstration of this pattern.
455///
456/// ```
457/// // `Component` is defined in the `bevy_ecs` crate.
458/// use bevy_ecs::component::Component;
459///
460/// // `Duration` is defined in the `std` crate.
461/// use std::time::Duration;
462///
463/// // It is not possible to implement `Component` for `Duration` from this position, as they are
464/// // both foreign items, defined in an external crate. However, nothing prevents to define a new
465/// // `Cooldown` type that wraps `Duration`. As `Cooldown` is defined in a local crate, it is
466/// // possible to implement `Component` for it.
467/// #[derive(Component)]
468/// struct Cooldown(Duration);
469/// ```
470///
471/// [orphan rule]: https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type
472/// [newtype pattern]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types
473///
474/// # `!Sync` Components
475/// A `!Sync` type cannot implement `Component`. However, it is possible to wrap a `Send` but not `Sync`
476/// type in [`SyncCell`] or the currently unstable [`Exclusive`] to make it `Sync`. This forces only
477/// having mutable access (`&mut T` only, never `&T`), but makes it safe to reference across multiple
478/// threads.
479///
480/// This will fail to compile since `RefCell` is `!Sync`.
481/// ```compile_fail
482/// # use std::cell::RefCell;
483/// # use bevy_ecs::component::Component;
484/// #[derive(Component)]
485/// struct NotSync {
486/// counter: RefCell<usize>,
487/// }
488/// ```
489///
490/// This will compile since the `RefCell` is wrapped with `SyncCell`.
491/// ```
492/// # use std::cell::RefCell;
493/// # use bevy_ecs::component::Component;
494/// use bevy_platform::cell::SyncCell;
495///
496/// // This will compile.
497/// #[derive(Component)]
498/// struct ActuallySync {
499/// counter: SyncCell<RefCell<usize>>,
500/// }
501/// ```
502///
503/// # Summary ticks
504/// You can request that Bevy track a *summary tick* for a component like so:
505/// ```
506/// # use bevy_ecs::prelude::*;
507///
508/// #[derive(Component)]
509/// #[component(summary_tick)]
510/// struct MyComponent;
511///
512/// ```
513///
514/// A summary tick allows systems that use [contiguous iteration] to skip entire
515/// tables if none of the components that those systems care about have changed.
516/// The downside is that performance of updating those components decreases, as
517/// the summary tick must be updated. Summary ticks are only valid for
518/// components with table storage; components that have sparse set storage may
519/// not use summary ticks.
520///
521/// [`SyncCell`]: bevy_platform::cell::SyncCell
522/// [`Exclusive`]: https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html
523/// [`ComponentHooks`]: crate::lifecycle::ComponentHooks
524/// [contiguous iteration]: crate::system::Query::contiguous_iter
525#[diagnostic::on_unimplemented(
526 message = "`{Self}` is not a `Component`",
527 label = "invalid `Component`",
528 note = "consider annotating `{Self}` with `#[derive(Component)]`"
529)]
530pub trait Component: Send + Sync + 'static {
531 /// A constant indicating the storage type used for this component.
532 const STORAGE_TYPE: StorageType;
533
534 /// A marker type to assist Bevy with determining if this component is
535 /// mutable, or immutable. Mutable components will have [`Component<Mutability = Mutable>`],
536 /// while immutable components will instead have [`Component<Mutability = Immutable>`].
537 ///
538 /// * For a component to be mutable, this type must be [`Mutable`].
539 /// * For a component to be immutable, this type must be [`Immutable`].
540 type Mutability: ComponentMutability;
541
542 /// Gets the `on_add` [`ComponentHook`] for this [`Component`] if one is defined.
543 fn on_add() -> Option<ComponentHook> {
544 None
545 }
546
547 /// Gets the `on_insert` [`ComponentHook`] for this [`Component`] if one is defined.
548 fn on_insert() -> Option<ComponentHook> {
549 None
550 }
551
552 /// Gets the `on_discard` [`ComponentHook`] for this [`Component`] if one is defined.
553 fn on_discard() -> Option<ComponentHook> {
554 None
555 }
556
557 /// Gets the `on_remove` [`ComponentHook`] for this [`Component`] if one is defined.
558 fn on_remove() -> Option<ComponentHook> {
559 None
560 }
561
562 /// Gets the `on_despawn` [`ComponentHook`] for this [`Component`] if one is defined.
563 fn on_despawn() -> Option<ComponentHook> {
564 None
565 }
566
567 /// Registers required components.
568 ///
569 /// # Safety
570 ///
571 /// - `_required_components` must only contain components valid in `_components`.
572 fn register_required_components(
573 _component_id: ComponentId,
574 _required_components: &mut RequiredComponentsRegistrator,
575 ) {
576 }
577
578 /// Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component.
579 ///
580 /// See [Clone Behaviors section of `EntityCloner`](crate::entity::EntityCloner#clone-behaviors) to understand how this affects handler priority.
581 #[inline]
582 fn clone_behavior() -> ComponentCloneBehavior {
583 ComponentCloneBehavior::Default
584 }
585
586 /// Maps the entities on this component using the given [`EntityMapper`]. This is used to remap entities in contexts like scenes and entity cloning.
587 /// When deriving [`Component`], this is populated by annotating fields containing entities with `#[entities]`
588 ///
589 /// ```
590 /// # use bevy_ecs::{component::Component, entity::Entity};
591 /// #[derive(Component)]
592 /// struct Inventory {
593 /// #[entities]
594 /// items: Vec<Entity>
595 /// }
596 /// ```
597 ///
598 /// Fields with `#[entities]` must implement [`MapEntities`](crate::entity::MapEntities).
599 ///
600 /// Bevy provides various implementations of [`MapEntities`](crate::entity::MapEntities), so that arbitrary combinations like these are supported with `#[entities]`:
601 ///
602 /// ```rust
603 /// # use bevy_ecs::{component::Component, entity::Entity};
604 /// #[derive(Component)]
605 /// struct Inventory {
606 /// #[entities]
607 /// items: Vec<Option<Entity>>
608 /// }
609 /// ```
610 ///
611 /// You might need more specialized logic. A likely cause of this is your component contains collections of entities that
612 /// don't implement [`MapEntities`](crate::entity::MapEntities). In that case, you can annotate your component with
613 /// `#[component(map_entities)]`. Using this attribute, you must implement `MapEntities` for the
614 /// component itself, and this method will simply call that implementation.
615 ///
616 /// ```
617 /// # use bevy_ecs::{component::Component, entity::{Entity, MapEntities, EntityMapper, EntityHashMap}};
618 /// #[derive(Component)]
619 /// #[component(map_entities)]
620 /// struct Inventory {
621 /// items: EntityHashMap<usize>
622 /// }
623 ///
624 /// impl MapEntities for Inventory {
625 /// fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
626 /// self.items = self.items
627 /// .drain()
628 /// .map(|(id, count)|(entity_mapper.get_mapped(id), count))
629 /// .collect();
630 /// }
631 /// }
632 /// # let a = Entity::from_bits(0x1_0000_0001);
633 /// # let b = Entity::from_bits(0x1_0000_0002);
634 /// # let mut inv = Inventory { items: Default::default() };
635 /// # inv.items.insert(a, 10);
636 /// # <Inventory as Component>::map_entities(&mut inv, &mut (a,b));
637 /// # assert_eq!(inv.items.get(&b), Some(&10));
638 /// ````
639 ///
640 /// Alternatively, you can specify the path to a function with `#[component(map_entities = function_path)]`, similar to component hooks.
641 /// In this case, the inputs of the function should mirror the inputs to this method, with the second parameter being generic.
642 ///
643 /// ```
644 /// # use bevy_ecs::{component::Component, entity::{Entity, MapEntities, EntityMapper, EntityHashMap}};
645 /// #[derive(Component)]
646 /// #[component(map_entities = map_the_map)]
647 /// // Also works: map_the_map::<M> or map_the_map::<_>
648 /// struct Inventory {
649 /// items: EntityHashMap<usize>
650 /// }
651 ///
652 /// fn map_the_map<M: EntityMapper>(inv: &mut Inventory, entity_mapper: &mut M) {
653 /// inv.items = inv.items
654 /// .drain()
655 /// .map(|(id, count)|(entity_mapper.get_mapped(id), count))
656 /// .collect();
657 /// }
658 /// # let a = Entity::from_bits(0x1_0000_0001);
659 /// # let b = Entity::from_bits(0x1_0000_0002);
660 /// # let mut inv = Inventory { items: Default::default() };
661 /// # inv.items.insert(a, 10);
662 /// # <Inventory as Component>::map_entities(&mut inv, &mut (a,b));
663 /// # assert_eq!(inv.items.get(&b), Some(&10));
664 /// ````
665 ///
666 /// You can use the turbofish (`::<A,B,C>`) to specify parameters when a function is generic, using either M or _ for the type of the mapper parameter.
667 #[inline]
668 fn map_entities<E: EntityMapper>(_this: &mut Self, _mapper: &mut E) {}
669
670 /// Returns [`ComponentRelationshipAccessor`] required for working with relationships in dynamic contexts.
671 ///
672 /// If component is not a [`Relationship`](crate::relationship::Relationship) or [`RelationshipTarget`](crate::relationship::RelationshipTarget), this should return `None`.
673 fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>> {
674 None
675 }
676
677 /// Set this constant to true if the component should track a summary
678 /// tick.
679 ///
680 /// Summary ticks allow users of contiguous iteration queries to skip
681 /// entire tables if the components that those users are interested in
682 /// haven't changed since the last time they ran the query. Tracking a
683 /// summary tick enables this functionality but adds a small amount of
684 /// overhead to mutations of the component, because the summary tick must be
685 /// updated on each such mutation.
686 ///
687 /// Summary ticks are only valid for table components. If the component is a
688 /// sparse set component, this method must return false.
689 ///
690 /// By default, this constant is set to false.
691 const HAS_SUMMARY_TICK: bool = false;
692}
693
694mod private {
695 pub trait Seal {}
696}
697
698/// The mutability option for a [`Component`]. This can either be:
699/// * [`Mutable`]
700/// * [`Immutable`]
701///
702/// This is controlled through either [`Component::Mutability`] or `#[component(immutable)]`
703/// when using the derive macro.
704///
705/// Immutable components are guaranteed to never have an exclusive reference,
706/// `&mut ...`, created while inserted onto an entity.
707/// In all other ways, they are identical to mutable components.
708/// This restriction allows hooks to observe all changes made to an immutable
709/// component, effectively turning the `Insert` and `Discard` hooks into a
710/// `OnMutate` hook.
711/// This is not practical for mutable components, as the runtime cost of invoking
712/// a hook for every exclusive reference created would be far too high.
713///
714/// # Examples
715///
716/// ```rust
717/// # use bevy_ecs::component::Component;
718/// #
719/// #[derive(Component)]
720/// #[component(immutable)]
721/// struct ImmutableFoo;
722/// ```
723pub trait ComponentMutability: private::Seal + 'static {
724 /// Boolean to indicate if this mutability setting implies a mutable or immutable
725 /// component.
726 const MUTABLE: bool;
727}
728
729/// Parameter indicating a [`Component`] is immutable.
730///
731/// See [`ComponentMutability`] for details.
732pub struct Immutable;
733
734impl private::Seal for Immutable {}
735
736impl ComponentMutability for Immutable {
737 const MUTABLE: bool = false;
738}
739
740/// Parameter indicating a [`Component`] is mutable.
741///
742/// See [`ComponentMutability`] for details.
743pub struct Mutable;
744
745impl private::Seal for Mutable {}
746
747impl ComponentMutability for Mutable {
748 const MUTABLE: bool = true;
749}
750
751/// The storage used for a specific component type.
752///
753/// # Examples
754/// The [`StorageType`] for a component is configured via the derive attribute
755///
756/// ```
757/// # use bevy_ecs::{prelude::*, component::*};
758/// #[derive(Component)]
759/// #[component(storage = "SparseSet")]
760/// struct A;
761/// ```
762#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
763pub enum StorageType {
764 /// Provides fast and cache-friendly iteration, but slower addition and removal of components.
765 /// This is the default storage type.
766 #[default]
767 Table,
768 /// Provides fast addition and removal of components, but slower iteration.
769 SparseSet,
770}
771
772/// A [`SystemParam`] that provides access to the [`ComponentId`] for a specific component type.
773///
774/// # Example
775/// ```
776/// # use bevy_ecs::{system::Local, component::{Component, ComponentId, ComponentIdFor}};
777/// #[derive(Component)]
778/// struct Player;
779/// fn my_system(component_id: ComponentIdFor<Player>) {
780/// let component_id: ComponentId = component_id.get();
781/// // ...
782/// }
783/// ```
784#[derive(SystemParam)]
785pub struct ComponentIdFor<'s, T: Component>(Local<'s, InitComponentId<T>>);
786
787impl<T: Component> ComponentIdFor<'_, T> {
788 /// Gets the [`ComponentId`] for the type `T`.
789 #[inline]
790 pub fn get(&self) -> ComponentId {
791 **self
792 }
793}
794
795impl<T: Component> Deref for ComponentIdFor<'_, T> {
796 type Target = ComponentId;
797 fn deref(&self) -> &Self::Target {
798 &self.0.component_id
799 }
800}
801
802impl<T: Component> From<ComponentIdFor<'_, T>> for ComponentId {
803 #[inline]
804 fn from(to_component_id: ComponentIdFor<T>) -> ComponentId {
805 *to_component_id
806 }
807}
808
809/// Initializes the [`ComponentId`] for a specific type when used with [`FromWorld`].
810struct InitComponentId<T: Component> {
811 component_id: ComponentId,
812 marker: PhantomData<T>,
813}
814
815impl<T: Component> FromWorld for InitComponentId<T> {
816 fn from_world(world: &mut World) -> Self {
817 Self {
818 component_id: world.register_component::<T>(),
819 marker: PhantomData,
820 }
821 }
822}