@@ -146,25 +146,33 @@ use thiserror::Error;
146146/// assert_eq!(&C(0), world.entity(id).get::<C>().unwrap());
147147/// ```
148148///
149- /// You can also define a custom constructor:
149+ /// You can also define a custom constructor function or closure :
150150///
151151/// ```
152152/// # use bevy_ecs::prelude::*;
153153/// #[derive(Component)]
154- /// #[require(B(init_b ))]
154+ /// #[require(C(init_c ))]
155155/// struct A;
156- ///
156+ ///
157157/// #[derive(Component, PartialEq, Eq, Debug)]
158- /// struct B(usize);
158+ /// #[require(C(|| C(20)))]
159+ /// struct B;
159160///
160- /// fn init_b() -> B {
161- /// B(10)
161+ /// #[derive(Component, PartialEq, Eq, Debug)]
162+ /// struct C(usize);
163+ ///
164+ /// fn init_c() -> C {
165+ /// C(10)
162166/// }
163167///
164168/// # let mut world = World::default();
165- /// // This will implicitly also insert B with the init_b () constructor
169+ /// // This will implicitly also insert C with the init_c () constructor
166170/// let id = world.spawn(A).id();
167- /// assert_eq!(&B(10), world.entity(id).get::<B>().unwrap());
171+ /// assert_eq!(&C(10), world.entity(id).get::<C>().unwrap());
172+ ///
173+ /// // This will implicitly also insert C with the `|| C(20)` constructor closure
174+ /// let id = world.spawn(B).id();
175+ /// assert_eq!(&C(20), world.entity(id).get::<C>().unwrap());
168176/// ```
169177///
170178/// Required components are _recursive_. This means, if a Required Component has required components,
@@ -202,24 +210,16 @@ use thiserror::Error;
202210/// struct X(usize);
203211///
204212/// #[derive(Component, Default)]
205- /// #[require(X(x1 ))]
213+ /// #[require(X(|| X(1) ))]
206214/// struct Y;
207215///
208- /// fn x1() -> X {
209- /// X(1)
210- /// }
211- ///
212216/// #[derive(Component)]
213217/// #[require(
214218/// Y,
215- /// X(x2 ),
219+ /// X(|| X(2) ),
216220/// )]
217221/// struct Z;
218222///
219- /// fn x2() -> X {
220- /// X(2)
221- /// }
222- ///
223223/// # let mut world = World::default();
224224/// // In this case, the x2 constructor is used for X
225225/// let id = world.spawn(Z).id();
0 commit comments