pub struct ComponentHandler<Base: Extractable, Args = (), Return = ()> { /* private fields */ }Expand description
A component handler that enables polymorphic behavior on entity hierarchies.
This handler allows you to define behavior for concrete entity types (like Player or Zombie)
while storing the handler in a base type (like Entity). When querying for the base type,
the handler will execute the concrete type’s implementation.
§Type Parameters
Base: The base struct type used for queries (e.g.,Entity)Args: The argument tuple type for the handler function (default:())Return: The return type of the handler function (default:())
§Example
use structecs::*;
#[derive(Debug, Extractable)]
pub struct Player {
pub name: String,
pub level: u32,
}
let world = World::new();
let player_id = world.add_entity(Player {
name: "Hero".to_string(),
level: 10,
});
// Create a handler for Player entities
let player_handler = ComponentHandler::<Player>::for_type::<Player>(|player, ()| {
println!("Level {} player {} died!", player.level, player.name);
});
// Query for Player and call handler
for (id, player) in world.query::<Player>() {
player_handler.call(&player, ()); // Calls Player-specific logic
}Implementations§
Source§impl<Base: Extractable, Args, Return> ComponentHandler<Base, Args, Return>
impl<Base: Extractable, Args, Return> ComponentHandler<Base, Args, Return>
Sourcepub fn for_type<Concrete: Extractable>(
func: impl Fn(&Acquirable<Concrete>, Args) -> Return + Send + Sync + 'static,
) -> Self
pub fn for_type<Concrete: Extractable>( func: impl Fn(&Acquirable<Concrete>, Args) -> Return + Send + Sync + 'static, ) -> Self
Create a handler for entities of type Concrete that can be extracted as Base.
§Type Parameters
Concrete: The actual entity type (e.g.,Player,Zombie)
The Concrete type must contain the Base type in its extraction metadata.
This is typically achieved using #[extractable(field_name)] on the concrete type.
§Panics
In debug builds, this function will panic if Concrete does not contain Base
in its extraction metadata. This helps catch type mismatches early during development.
§Example
use structecs::*;
#[derive(Debug, Extractable)]
pub struct Player {
pub name: String,
pub level: u32,
}
// Handler for Player entities
let handler = ComponentHandler::<Player>::for_type::<Player>(|player, ()| {
println!("Player {} died", player.name);
});Sourcepub fn call<E: Extractable>(&self, entity: &Acquirable<E>, args: Args) -> Return
pub fn call<E: Extractable>(&self, entity: &Acquirable<E>, args: Args) -> Return
Call the handler with an entity.
§Type Parameters
E: The entity type being passed to the handler
The entity type E must be extractable as Base. The handler will then
extract the concrete type it was created with and call the appropriate function.
§Panics
In debug builds, this function will panic if E cannot be extracted as Base.
§Example
use structecs::*;
#[derive(Debug, Extractable)]
pub struct Player {
pub name: String,
pub level: u32,
}
let world = World::new();
let player_id = world.add_entity(Player {
name: "Hero".to_string(),
level: 10,
});
let handler = ComponentHandler::<Player>::for_type::<Player>(|player, ()| {
println!("Player died");
});
for (id, player) in world.query::<Player>() {
handler.call(&player, ());
}Sourcepub fn debug_info(&self) -> String
pub fn debug_info(&self) -> String
Get debug information about this handler (debug builds only).
Returns a string containing the base type, concrete type, and function signature.
§Example
use structecs::*;
#[derive(Debug, Extractable)]
pub struct Player {
pub name: String,
pub level: u32,
}
let handler = ComponentHandler::<Player>::for_type::<Player>(|player, ()| {
println!("Player died");
});
#[cfg(debug_assertions)]
{
let info = handler.debug_info();
assert!(info.contains("ComponentHandler"));
assert!(info.contains("Player"));
}