use crate::component::Sequence as ComponentSequence; use crate::event::component::ComponentToAddedEvent; use crate::event::{Event, Sequence as EventSequence}; use crate::sole::Sole; use crate::system::System; use crate::tuple::Reduce as TupleReduce; use crate::{SoleAlreadyExistsError, World}; /// A collection of systems, entities & soles that can be added to a [`World`]. pub trait Extension { fn collect(self, collector: Collector<'_>); } /// Passed to a [`Extension`] to collects it's systems, entities & soles. pub struct Collector<'world> { world: &'world mut World, } impl<'world> Collector<'world> { /// Returns a new `Collector` for the given [`World`]. pub fn new(world: &'world mut World) -> Self { Self { world } } /// Adds a system to the [`World`]. pub fn add_system<'this, EventT, SystemImpl>( &'this mut self, event: EventT, system: impl System<'this, SystemImpl>, ) where EventT: Event, { self.world.register_system(event, system); } /// Adds a entity to the [`World`]. pub fn add_entity(&mut self, components: Comps) where Comps: ComponentSequence + TupleReduce, Comps::Out: EventSequence, { self.world.create_entity(components); } /// Adds a globally shared singleton value to the [`World`]. /// /// # Errors /// Returns `Err` if this [`Sole`] has already been added. pub fn add_sole(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError> where SoleT: Sole, { self.world.add_sole(sole) } }