ComponentHandler

Struct ComponentHandler 

Source
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>

Source

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);
});
Source

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, ());
}
Source

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"));
}

Trait Implementations§

Source§

impl<Base: Extractable, Args, Return> Debug for ComponentHandler<Base, Args, Return>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Base, Args, Return> Freeze for ComponentHandler<Base, Args, Return>

§

impl<Base, Args = (), Return = ()> !RefUnwindSafe for ComponentHandler<Base, Args, Return>

§

impl<Base, Args, Return> Send for ComponentHandler<Base, Args, Return>
where Base: Send,

§

impl<Base, Args, Return> Sync for ComponentHandler<Base, Args, Return>
where Base: Sync,

§

impl<Base, Args, Return> Unpin for ComponentHandler<Base, Args, Return>
where Base: Unpin,

§

impl<Base, Args = (), Return = ()> !UnwindSafe for ComponentHandler<Base, Args, Return>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.