Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,12 @@ impl Parse for Require {
let func = content.parse::<Path>()?;
Some(RequireFunc::Path(func))
}
} else if input.peek(Token![=]) {
let _t: syn::Token![=] = input.parse()?;
let label: Ident = input.parse()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could parse out an expression instead and support more ways to set the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but you would somehow need to extract the type from the expression, so it will be messy

let tokens: TokenStream = quote::quote! (|| #path::#label).into();
let func = syn::parse(tokens).unwrap();
Some(RequireFunc::Closure(func))
} else {
None
};
Expand Down
32 changes: 32 additions & 0 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,43 @@ use thiserror::Error;
/// }
///
/// # let mut world = World::default();
/// // This will implicitly also insert C with the init_c() constructor
/// let id = world.spawn(A).id();
/// assert_eq!(&C(10), world.entity(id).get::<C>().unwrap());
///
/// // This will implicitly also insert C with the `|| C(20)` constructor closure
/// let id = world.spawn(B).id();
/// assert_eq!(&C(20), world.entity(id).get::<C>().unwrap());
/// ```
///
/// For convenience sake, you can abbreviate enum labels or constant values, with the type inferred to match that of the component you are requiring:
///
/// ```
/// # use bevy_ecs::prelude::*;
/// #[derive(Component)]
/// #[require(B = One, C = ONE)]
/// struct A;
///
/// #[derive(Component, PartialEq, Eq, Debug)]
/// enum B {
/// Zero,
/// One,
/// Two
/// }
///
/// #[derive(Component, PartialEq, Eq, Debug)]
/// struct C(u8);
///
/// impl C {
/// pub const ONE: Self = Self(1);
/// }
///
/// # let mut world = World::default();
/// let id = world.spawn(A).id();
/// assert_eq!(&B::One, world.entity(id).get::<B>().unwrap());
/// assert_eq!(&C(1), world.entity(id).get::<C>().unwrap());
/// ````
///
/// Required components are _recursive_. This means, if a Required Component has required components,
/// those components will _also_ be inserted if they are missing:
///
Expand Down