It is possible to make struct registration semi-automatic, using inventory crate. The solution simplifies registration a lot and does not require a centralized registration function with all the structs listed.
A feature (which may not be enabled by default) can be introduced to toggle this, since it requires an additional dependency which may not be wanted.
It can be achieved using these three things:
A helper struct:
pub struct Register(pub fn(&mut World));
An attribute #[register] for impl Trait for Struct, which generates:
inventory::submit!(bevy_trait_query::Register(|world| {
use bevy_trait_query::RegisterExt;
world.register_component_as::<dyn Trait, Struct>();
}));
A global registration function, which should be called manually (that's why the solution is semi-automatic):
fn register_traits(world: &mut World) {
inventory::collect!(Register);
for callback in inventory::iter::<Register> {
callback(world);
}
}
It is possible to make struct registration semi-automatic, using
inventory
crate. The solution simplifies registration a lot and does not require a centralized registration function with all the structs listed.A feature (which may not be enabled by default) can be introduced to toggle this, since it requires an additional dependency which may not be wanted.
It can be achieved using these three things:
#[register]
forimpl Trait for Struct
, which generates: