Skip to main content

Component

Trait Component 

Source
pub trait Component:
    Send
    + Sync
    + 'static { }
Expand description

Marker trait for types that can be used as ECS components.

Components must be:

  • Send: Can be transferred between threads
  • Sync: Can be shared between threads via references
  • 'static: No borrowed data (required for type erasure and storage)

§Thread Safety

The Send + Sync bounds enable parallel system execution. Systems can safely access components from multiple threads when access patterns don’t conflict (multiple readers or single writer).

§Implementation

Components require explicit opt-in via implementation:

use goud_engine::ecs::Component;

struct Health(pub f32);
impl Component for Health {}

This is intentional - not all Send + Sync + 'static types should automatically be components. The explicit implementation:

  1. Documents intent that this type is meant for ECS use
  2. Allows future derive macro to add behavior
  3. Prevents accidental use of inappropriate types as components

§What Makes a Good Component?

  • Data-only: Components should be pure data, no behavior
  • Small: Prefer many small components over few large ones
  • Focused: Each component represents one aspect of an entity

Good:

struct Position { x: f32, y: f32, z: f32 }
impl Component for Position {}

struct Velocity { x: f32, y: f32, z: f32 }
impl Component for Velocity {}

Avoid:

// Too large - combines unrelated data
struct Entity {
    position: (f32, f32, f32),
    velocity: (f32, f32, f32),
    health: f32,
    name: String,
}

Implementors§