Trait Handler

Source
pub trait Handler: 'static {
    // Required methods
    fn type_id(&self) -> Option<TypeId>;
    fn name(&self) -> Cow<'static, str>;
    fn init(
        &mut self,
        world: &mut World,
        config: &mut HandlerConfig,
    ) -> Result<(), InitError>;
    unsafe fn run(
        &mut self,
        info: &HandlerInfo,
        event_ptr: EventPtr<'_>,
        target_location: EntityLocation,
        world: UnsafeWorldCell<'_>,
    );
    fn refresh_archetype(&mut self, arch: &Archetype);
    fn remove_archetype(&mut self, arch: &Archetype);
}
Expand description

A callback function that listens for events.

handlers are added to a world using the World::add_handler method.

For more information about handlers, see the tutorial.

Required Methods§

Source

fn type_id(&self) -> Option<TypeId>

Returns the TypeId which uniquely identifies this handler, or None if there is none.

No two handlers with the same TypeId will exist in the World at the same time.

Source

fn name(&self) -> Cow<'static, str>

Returns the name of this handler for debugging purposes.

Source

fn init( &mut self, world: &mut World, config: &mut HandlerConfig, ) -> Result<(), InitError>

Initializes the handler. Returns InitError on failure.

Source

unsafe fn run( &mut self, info: &HandlerInfo, event_ptr: EventPtr<'_>, target_location: EntityLocation, world: UnsafeWorldCell<'_>, )

Execute the handler by passing in the handler’s metadata, a pointer to the received event of the configured type, the entity location of the event’s target, and an UnsafeWorldCell with permission to access the data described in the configuration.

§Safety
  • handler must be initialized via init.
  • info must be the correct information for this handler.
  • event_ptr must point to the correct type of event configured by this handler in init.
  • target_location must be a valid location to an entity matching the component access set by set_targeted_event_component_access, unless the event is not targeted.
  • world must have permission to access all data configured by this handler in init.
Source

fn refresh_archetype(&mut self, arch: &Archetype)

Notifies the handler that an archetype it might care about had its internal state updated.

This is invoked in the following scenarios:

  • The archetype was previously empty, but has now gained at least one entity.
  • The archetype’s columns were reallocated, and any pointers into the archetype’s columns need to be reacquired.

This method must not be called with empty archetypes.

Source

fn remove_archetype(&mut self, arch: &Archetype)

Notifies the handler that an archetype it might care about is no longer available.

This is invoked in the following scenarios:

  • The archetype was removed from the world.
  • The archetype previously had entities in it, but is now empty.

In either case, the handler must assume that the archetype is no longer available. Attempting to read the component data from a removed archetype is illegal.

Implementors§

Source§

impl<H> Handler for HighPriority<H>
where H: Handler,

Source§

impl<H> Handler for LowPriority<H>
where H: Handler,

Source§

impl<H> Handler for NoTypeId<H>
where H: Handler,

Source§

impl<Marker, F> Handler for FunctionHandler<Marker, F>
where F: HandlerParamFunction<Marker>, Marker: 'static,