281 lines
37 KiB
HTML
281 lines
37 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A data type that can be used to store data for an entity."><title>Component in bevy_ecs::component - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="bevy_ecs" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.css"></noscript><link rel="icon" href="https://bevyengine.org/assets/icon.png"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../../bevy_ecs/index.html"><img src="https://bevyengine.org/assets/icon.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../bevy_ecs/index.html"><img src="https://bevyengine.org/assets/icon.png" alt="logo"></a><h2><a href="../../bevy_ecs/index.html">bevy_<wbr>ecs</a><span class="version">0.15.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Component</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#component-and-data-access" title="Component and data access">Component and data access</a></li><li><a href="#choosing-a-storage-type" title="Choosing a storage type">Choosing a storage type</a></li><li><a href="#required-components" title="Required Components">Required Components</a><ul><li><a href="#registering-required-components-at-runtime" title="Registering required components at runtime">Registering required components at runtime</a></li></ul></li><li><a href="#adding-components-hooks" title="Adding component’s hooks">Adding component’s hooks</a></li><li><a href="#implementing-the-trait-for-foreign-types" title="Implementing the trait for foreign types">Implementing the trait for foreign types</a></li><li><a href="#sync-components" title="`!Sync` Components"><code>!Sync</code> Components</a></li></ul><h3><a href="#required-associated-consts">Required Associated Constants</a></h3><ul class="block"><li><a href="#associatedconstant.STORAGE_TYPE" title="STORAGE_TYPE">STORAGE_TYPE</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.register_component_hooks" title="register_component_hooks">register_component_hooks</a></li><li><a href="#method.register_required_components" title="register_required_components">register_required_components</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In bevy_<wbr>ecs::<wbr>component</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../index.html">bevy_ecs</a>::<wbr><a href="index.html">component</a></span><h1>Trait <span class="trait">Component</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/bevy_ecs/component.rs.html#377-393">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Component:
|
||
<a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>
|
||
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a>
|
||
+ 'static {
|
||
const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a>;
|
||
|
||
// Provided methods
|
||
fn <a href="#method.register_component_hooks" class="fn">register_component_hooks</a>(_hooks: &mut <a class="struct" href="struct.ComponentHooks.html" title="struct bevy_ecs::component::ComponentHooks">ComponentHooks</a>) { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.register_required_components" class="fn">register_required_components</a>(
|
||
_component_id: <a class="struct" href="struct.ComponentId.html" title="struct bevy_ecs::component::ComponentId">ComponentId</a>,
|
||
_components: &mut <a class="struct" href="struct.Components.html" title="struct bevy_ecs::component::Components">Components</a>,
|
||
_storages: &mut <a class="struct" href="../storage/struct.Storages.html" title="struct bevy_ecs::storage::Storages">Storages</a>,
|
||
_required_components: &mut <a class="struct" href="struct.RequiredComponents.html" title="struct bevy_ecs::component::RequiredComponents">RequiredComponents</a>,
|
||
_inheritance_depth: <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u16.html">u16</a>,
|
||
) { ... }
|
||
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A data type that can be used to store data for an <a href="../entity/index.html" title="mod bevy_ecs::entity">entity</a>.</p>
|
||
<p><code>Component</code> is a <a href="https://doc.rust-lang.org/book/appendix-03-derivable-traits.html">derivable trait</a>: this means that a data type can implement it by applying a <code>#[derive(Component)]</code> attribute to it.
|
||
However, components must always satisfy the <code>Send + Sync + 'static</code> trait bounds.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<p>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.
|
||
The following examples show how components are laid out in code.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A component can contain data...
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>LicensePlate(String);
|
||
|
||
<span class="comment">// ... but it can also be a zero-sized marker.
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>Car;
|
||
|
||
<span class="comment">// Components can also be structs with named fields...
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>VehiclePerformance {
|
||
acceleration: f32,
|
||
top_speed: f32,
|
||
handling: f32,
|
||
}
|
||
|
||
<span class="comment">// ... or enums.
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">enum </span>WheelCount {
|
||
Two,
|
||
Three,
|
||
Four,
|
||
}</code></pre></div>
|
||
<h2 id="component-and-data-access"><a class="doc-anchor" href="#component-and-data-access">§</a>Component and data access</h2>
|
||
<p>See the <a href="../entity/index.html#usage" title="mod bevy_ecs::entity"><code>entity</code></a> module level documentation to learn how to add or remove components from an entity.</p>
|
||
<p>See the documentation for <a href="../system/struct.Query.html" title="struct bevy_ecs::system::Query"><code>Query</code></a> to learn how to access component data from a system.</p>
|
||
<h2 id="choosing-a-storage-type"><a class="doc-anchor" href="#choosing-a-storage-type">§</a>Choosing a storage type</h2>
|
||
<p>Components can be stored in the world using different strategies with their own performance implications.
|
||
By default, components are added to the <a href="../storage/struct.Table.html" title="struct bevy_ecs::storage::Table"><code>Table</code></a> storage, which is optimized for query iteration.</p>
|
||
<p>Alternatively, components can be added to the <a href="../storage/struct.SparseSet.html" title="struct bevy_ecs::storage::SparseSet"><code>SparseSet</code></a> storage, which is optimized for component insertion and removal.
|
||
This is achieved by adding an additional <code>#[component(storage = "SparseSet")]</code> attribute to the derive one:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[component(storage = <span class="string">"SparseSet"</span>)]
|
||
</span><span class="kw">struct </span>ComponentA;</code></pre></div>
|
||
<h2 id="required-components"><a class="doc-anchor" href="#required-components">§</a>Required Components</h2>
|
||
<p>Components can specify Required Components. If some <a href="trait.Component.html" title="trait bevy_ecs::component::Component"><code>Component</code></a> <code>A</code> requires <a href="trait.Component.html" title="trait bevy_ecs::component::Component"><code>Component</code></a> <code>B</code>, then when <code>A</code> is inserted,
|
||
<code>B</code> will <em>also</em> be initialized and inserted (if it was not manually specified).</p>
|
||
<p>The <a href="https://doc.rust-lang.org/1.84.0/core/default/trait.Default.html" title="trait core::default::Default"><code>Default</code></a> constructor will be used to initialize the component, by default:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[require(B)]
|
||
</span><span class="kw">struct </span>A;
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>B(usize);
|
||
|
||
<span class="comment">// This will implicitly also insert B with the Default constructor
|
||
</span><span class="kw">let </span>id = world.spawn(A).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>B(<span class="number">0</span>), world.entity(id).get::<B>().unwrap());
|
||
|
||
<span class="comment">// This will _not_ implicitly insert B, because it was already provided
|
||
</span>world.spawn((A, B(<span class="number">11</span>)));</code></pre></div>
|
||
<p>Components can have more than one required component:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[require(B, C)]
|
||
</span><span class="kw">struct </span>A;
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
#[require(C)]
|
||
</span><span class="kw">struct </span>B(usize);
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>C(u32);
|
||
|
||
<span class="comment">// This will implicitly also insert B and C with their Default constructors
|
||
</span><span class="kw">let </span>id = world.spawn(A).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>B(<span class="number">0</span>), world.entity(id).get::<B>().unwrap());
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>C(<span class="number">0</span>), world.entity(id).get::<C>().unwrap());</code></pre></div>
|
||
<p>You can also define a custom constructor function or closure:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[require(C(init_c))]
|
||
</span><span class="kw">struct </span>A;
|
||
|
||
<span class="attr">#[derive(Component, PartialEq, Eq, Debug)]
|
||
#[require(C(|| C(<span class="number">20</span>)))]
|
||
</span><span class="kw">struct </span>B;
|
||
|
||
<span class="attr">#[derive(Component, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>C(usize);
|
||
|
||
<span class="kw">fn </span>init_c() -> C {
|
||
C(<span class="number">10</span>)
|
||
}
|
||
|
||
<span class="comment">// This will implicitly also insert C with the init_c() constructor
|
||
</span><span class="kw">let </span>id = world.spawn(A).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>C(<span class="number">10</span>), world.entity(id).get::<C>().unwrap());
|
||
|
||
<span class="comment">// This will implicitly also insert C with the `|| C(20)` constructor closure
|
||
</span><span class="kw">let </span>id = world.spawn(B).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>C(<span class="number">20</span>), world.entity(id).get::<C>().unwrap());</code></pre></div>
|
||
<p>Required components are <em>recursive</em>. This means, if a Required Component has required components,
|
||
those components will <em>also</em> be inserted if they are missing:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[require(B)]
|
||
</span><span class="kw">struct </span>A;
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
#[require(C)]
|
||
</span><span class="kw">struct </span>B(usize);
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>C(u32);
|
||
|
||
<span class="comment">// This will implicitly also insert B and C with their Default constructors
|
||
</span><span class="kw">let </span>id = world.spawn(A).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>B(<span class="number">0</span>), world.entity(id).get::<B>().unwrap());
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>C(<span class="number">0</span>), world.entity(id).get::<C>().unwrap());</code></pre></div>
|
||
<p>Note that cycles in the “component require tree” will result in stack overflows when attempting to
|
||
insert a component.</p>
|
||
<p>This “multiple inheritance” pattern does mean that it is possible to have duplicate requires for a given type
|
||
at different levels of the inheritance tree:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>X(usize);
|
||
|
||
<span class="attr">#[derive(Component, Default)]
|
||
#[require(X(|| X(<span class="number">1</span>)))]
|
||
</span><span class="kw">struct </span>Y;
|
||
|
||
<span class="attr">#[derive(Component)]
|
||
#[require(
|
||
Y,
|
||
X(|| X(<span class="number">2</span>)),
|
||
)]
|
||
</span><span class="kw">struct </span>Z;
|
||
|
||
<span class="comment">// In this case, the x2 constructor is used for X
|
||
</span><span class="kw">let </span>id = world.spawn(Z).id();
|
||
<span class="macro">assert_eq!</span>(<span class="number">2</span>, world.entity(id).get::<X>().unwrap().<span class="number">0</span>);</code></pre></div>
|
||
<p>In general, this shouldn’t happen often, but when it does the algorithm for choosing the constructor from the tree is simple and predictable:</p>
|
||
<ol>
|
||
<li>A constructor from a direct <code>#[require()]</code>, if one exists, is selected with priority.</li>
|
||
<li>Otherwise, perform a Depth First Search on the tree of requirements and select the first one found.</li>
|
||
</ol>
|
||
<p>From a user perspective, just think about this as the following:</p>
|
||
<ol>
|
||
<li>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.</li>
|
||
<li>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.</li>
|
||
</ol>
|
||
<h3 id="registering-required-components-at-runtime"><a class="doc-anchor" href="#registering-required-components-at-runtime">§</a>Registering required components at runtime</h3>
|
||
<p>In most cases, required components should be registered using the <code>require</code> attribute as shown above.
|
||
However, in some cases, it may be useful to register required components at runtime.</p>
|
||
<p>This can be done through <a href="../world/struct.World.html#method.register_required_components" title="method bevy_ecs::world::World::register_required_components"><code>World::register_required_components</code></a> or <a href="../world/struct.World.html#method.register_required_components_with" title="method bevy_ecs::world::World::register_required_components_with"><code>World::register_required_components_with</code></a>
|
||
for the <a href="https://doc.rust-lang.org/1.84.0/core/default/trait.Default.html" title="trait core::default::Default"><code>Default</code></a> and custom constructors respectively:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>A;
|
||
|
||
<span class="attr">#[derive(Component, Default, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>B(usize);
|
||
|
||
<span class="attr">#[derive(Component, PartialEq, Eq, Debug)]
|
||
</span><span class="kw">struct </span>C(u32);
|
||
|
||
<span class="comment">// Register B as required by A and C as required by B.
|
||
</span>world.register_required_components::<A, B>();
|
||
world.register_required_components_with::<B, C>(|| C(<span class="number">2</span>));
|
||
|
||
<span class="comment">// This will implicitly also insert B with its Default constructor
|
||
// and C with the custom constructor defined by B.
|
||
</span><span class="kw">let </span>id = world.spawn(A).id();
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>B(<span class="number">0</span>), world.entity(id).get::<B>().unwrap());
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">&</span>C(<span class="number">2</span>), world.entity(id).get::<C>().unwrap());</code></pre></div>
|
||
<p>Similar rules as before apply to duplicate requires fer a given type at different levels
|
||
of the inheritance tree. <code>A</code> requiring <code>C</code> directly would take precedence over indirectly
|
||
requiring it through <code>A</code> requiring <code>B</code> and <code>B</code> requiring <code>C</code>.</p>
|
||
<p>Unlike with the <code>require</code> attribute, directly requiring the same component multiple times
|
||
for the same component will result in a panic. This is done to prevent conflicting constructors
|
||
and confusing ordering dependencies.</p>
|
||
<p>Note that requirements must currently be registered before the requiring component is inserted
|
||
into the world for the first time. Registering requirements after this will lead to a panic.</p>
|
||
<h2 id="adding-components-hooks"><a class="doc-anchor" href="#adding-components-hooks">§</a>Adding component’s hooks</h2>
|
||
<p>See <a href="struct.ComponentHooks.html" title="struct bevy_ecs::component::ComponentHooks"><code>ComponentHooks</code></a> for a detailed explanation of component’s hooks.</p>
|
||
<p>Alternatively to the example shown in <a href="struct.ComponentHooks.html" title="struct bevy_ecs::component::ComponentHooks"><code>ComponentHooks</code></a>’ documentation, hooks can be configured using following attributes:</p>
|
||
<ul>
|
||
<li><code>#[component(on_add = on_add_function)]</code></li>
|
||
<li><code>#[component(on_insert = on_insert_function)]</code></li>
|
||
<li><code>#[component(on_replace = on_replace_function)]</code></li>
|
||
<li><code>#[component(on_remove = on_remove_function)]</code></li>
|
||
</ul>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
#[component(on_add = my_on_add_hook)]
|
||
#[component(on_insert = my_on_insert_hook)]
|
||
</span><span class="comment">// Another possible way of configuring hooks:
|
||
// #[component(on_add = my_on_add_hook, on_insert = my_on_insert_hook)]
|
||
//
|
||
// We don't have a replace or remove hook, so we can leave them out:
|
||
// #[component(on_replace = my_on_replace_hook, on_remove = my_on_remove_hook)]
|
||
</span><span class="kw">struct </span>ComponentA;
|
||
|
||
<span class="kw">fn </span>my_on_add_hook(world: DeferredWorld, entity: Entity, id: ComponentId) {
|
||
<span class="comment">// ...
|
||
</span>}
|
||
|
||
<span class="comment">// You can also omit writing some types using generics.
|
||
</span><span class="kw">fn </span>my_on_insert_hook<T1, T2>(world: DeferredWorld, <span class="kw">_</span>: T1, <span class="kw">_</span>: T2) {
|
||
<span class="comment">// ...
|
||
</span>}</code></pre></div>
|
||
<h2 id="implementing-the-trait-for-foreign-types"><a class="doc-anchor" href="#implementing-the-trait-for-foreign-types">§</a>Implementing the trait for foreign types</h2>
|
||
<p>As a consequence of the <a href="https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type">orphan rule</a>, it is not possible to separate into two different crates the implementation of <code>Component</code> from the definition of a type.
|
||
This means that it is not possible to directly have a type defined in a third party library as a component.
|
||
This important limitation can be easily worked around using the <a href="https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types">newtype pattern</a>:
|
||
this makes it possible to locally define and implement <code>Component</code> for a tuple struct that wraps the foreign type.
|
||
The following example gives a demonstration of this pattern.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// `Component` is defined in the `bevy_ecs` crate.
|
||
</span><span class="kw">use </span>bevy_ecs::component::Component;
|
||
|
||
<span class="comment">// `Duration` is defined in the `std` crate.
|
||
</span><span class="kw">use </span>std::time::Duration;
|
||
|
||
<span class="comment">// It is not possible to implement `Component` for `Duration` from this position, as they are
|
||
// both foreign items, defined in an external crate. However, nothing prevents to define a new
|
||
// `Cooldown` type that wraps `Duration`. As `Cooldown` is defined in a local crate, it is
|
||
// possible to implement `Component` for it.
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>Cooldown(Duration);</code></pre></div>
|
||
<h2 id="sync-components"><a class="doc-anchor" href="#sync-components">§</a><code>!Sync</code> Components</h2>
|
||
<p>A <code>!Sync</code> type cannot implement <code>Component</code>. However, it is possible to wrap a <code>Send</code> but not <code>Sync</code>
|
||
type in <a href="../../bevy_utils/synccell/struct.SyncCell.html" title="struct bevy_utils::synccell::SyncCell"><code>SyncCell</code></a> or the currently unstable <a href="https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html"><code>Exclusive</code></a> to make it <code>Sync</code>. This forces only
|
||
having mutable access (<code>&mut T</code> only, never <code>&T</code>), but makes it safe to reference across multiple
|
||
threads.</p>
|
||
<p>This will fail to compile since <code>RefCell</code> is <code>!Sync</code>.</p>
|
||
|
||
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>NotSync {
|
||
counter: RefCell<usize>,
|
||
}</code></pre></div>
|
||
<p>This will compile since the <code>RefCell</code> is wrapped with <code>SyncCell</code>.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>bevy_utils::synccell::SyncCell;
|
||
|
||
<span class="comment">// This will compile.
|
||
</span><span class="attr">#[derive(Component)]
|
||
</span><span class="kw">struct </span>ActuallySync {
|
||
counter: SyncCell<RefCell<usize>>,
|
||
}</code></pre></div>
|
||
</div></details><h2 id="required-associated-consts" class="section-header">Required Associated Constants<a href="#required-associated-consts" class="anchor">§</a></h2><div class="methods"><details class="toggle" open><summary><section id="associatedconstant.STORAGE_TYPE" class="method"><a class="src rightside" href="../../src/bevy_ecs/component.rs.html#379">Source</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a></h4></section></summary><div class="docblock"><p>A constant indicating the storage type used for this component.</p>
|
||
</div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.register_component_hooks" class="method"><a class="src rightside" href="../../src/bevy_ecs/component.rs.html#382">Source</a><h4 class="code-header">fn <a href="#method.register_component_hooks" class="fn">register_component_hooks</a>(_hooks: &mut <a class="struct" href="struct.ComponentHooks.html" title="struct bevy_ecs::component::ComponentHooks">ComponentHooks</a>)</h4></section></summary><div class="docblock"><p>Called when registering this component, allowing mutable access to its <a href="struct.ComponentHooks.html" title="struct bevy_ecs::component::ComponentHooks"><code>ComponentHooks</code></a>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.register_required_components" class="method"><a class="src rightside" href="../../src/bevy_ecs/component.rs.html#385-392">Source</a><h4 class="code-header">fn <a href="#method.register_required_components" class="fn">register_required_components</a>(
|
||
_component_id: <a class="struct" href="struct.ComponentId.html" title="struct bevy_ecs::component::ComponentId">ComponentId</a>,
|
||
_components: &mut <a class="struct" href="struct.Components.html" title="struct bevy_ecs::component::Components">Components</a>,
|
||
_storages: &mut <a class="struct" href="../storage/struct.Storages.html" title="struct bevy_ecs::storage::Storages">Storages</a>,
|
||
_required_components: &mut <a class="struct" href="struct.RequiredComponents.html" title="struct bevy_ecs::component::RequiredComponents">RequiredComponents</a>,
|
||
_inheritance_depth: <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u16.html">u16</a>,
|
||
)</h4></section></summary><div class="docblock"><p>Registers required components.</p>
|
||
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.84.0/reference/items/traits.html#object-safety">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-Observer" class="impl"><a class="src rightside" href="../../src/bevy_ecs/observer/runner.rs.html#315-326">Source</a><a href="#impl-Component-for-Observer" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../observer/struct.Observer.html" title="struct bevy_ecs::observer::Observer">Observer</a></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-1" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/observer/runner.rs.html#316">Source</a><a href="#associatedconstant.STORAGE_TYPE-1" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-ObserverState" class="impl"><a class="src rightside" href="../../src/bevy_ecs/observer/runner.rs.html#63-86">Source</a><a href="#impl-Component-for-ObserverState" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../observer/struct.ObserverState.html" title="struct bevy_ecs::observer::ObserverState">ObserverState</a></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-2" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/observer/runner.rs.html#64">Source</a><a href="#associatedconstant.STORAGE_TYPE-2" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-RemovedComponentEntity" class="impl"><a class="src rightside" href="../../src/bevy_ecs/removal_detection.rs.html#28">Source</a><a href="#impl-Component-for-RemovedComponentEntity" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../removal_detection/struct.RemovedComponentEntity.html" title="struct bevy_ecs::removal_detection::RemovedComponentEntity">RemovedComponentEntity</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-3" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/removal_detection.rs.html#28">Source</a><a href="#associatedconstant.STORAGE_TYPE-3" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-SystemIdMarker" class="impl"><a class="src rightside" href="../../src/bevy_ecs/system/system_registry.rs.html#25">Source</a><a href="#impl-Component-for-SystemIdMarker" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../system/struct.SystemIdMarker.html" title="struct bevy_ecs::system::SystemIdMarker">SystemIdMarker</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-4" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/system/system_registry.rs.html#25">Source</a><a href="#associatedconstant.STORAGE_TYPE-4" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::Table</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-OnAdd" class="impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#19">Source</a><a href="#impl-Component-for-OnAdd" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../world/struct.OnAdd.html" title="struct bevy_ecs::world::OnAdd">OnAdd</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-5" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#19">Source</a><a href="#associatedconstant.STORAGE_TYPE-5" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-OnInsert" class="impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#26">Source</a><a href="#impl-Component-for-OnInsert" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../world/struct.OnInsert.html" title="struct bevy_ecs::world::OnInsert">OnInsert</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-6" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#26">Source</a><a href="#associatedconstant.STORAGE_TYPE-6" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-OnRemove" class="impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#40">Source</a><a href="#impl-Component-for-OnRemove" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../world/struct.OnRemove.html" title="struct bevy_ecs::world::OnRemove">OnRemove</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-7" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#40">Source</a><a href="#associatedconstant.STORAGE_TYPE-7" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::SparseSet</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Component-for-OnReplace" class="impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#33">Source</a><a href="#impl-Component-for-OnReplace" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Component.html" title="trait bevy_ecs::component::Component">Component</a> for <a class="struct" href="../world/struct.OnReplace.html" title="struct bevy_ecs::world::OnReplace">OnReplace</a><div class="where">where
|
||
Self: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="associatedconstant.STORAGE_TYPE-8" class="associatedconstant trait-impl"><a class="src rightside" href="../../src/bevy_ecs/world/component_constants.rs.html#33">Source</a><a href="#associatedconstant.STORAGE_TYPE-8" class="anchor">§</a><h4 class="code-header">const <a href="#associatedconstant.STORAGE_TYPE" class="constant">STORAGE_TYPE</a>: <a class="enum" href="enum.StorageType.html" title="enum bevy_ecs::component::StorageType">StorageType</a> = bevy_ecs::component::StorageType::SparseSet</h4></section></div></details></div><script src="../../trait.impl/bevy_ecs/component/trait.Component.js" async></script></section></div></main></body></html> |