Skip to main content

Component

Trait Component 

Source
pub trait Component: Sized + Clone {
    // Required methods
    fn into_data(self) -> ComponentData;
    fn from_data(data: ComponentData) -> Option<Self>;
    fn from_data_ref(data: &ComponentData) -> Option<&Self>;
}
Expand description

Trait for types that can be converted to/from ComponentData.

This provides compile-time type safety for vanilla components while the actual storage uses the ABI-stable ComponentData enum.

§Example

impl Component for Damage {
    fn into_data(self) -> ComponentData {
        ComponentData::Damage(self)
    }

    fn from_data(data: ComponentData) -> Option<Self> {
        match data {
            ComponentData::Damage(d) => Some(d),
            _ => None,
        }
    }
}

Required Methods§

Source

fn into_data(self) -> ComponentData

Converts this component value into ComponentData.

Source

fn from_data(data: ComponentData) -> Option<Self>

Attempts to extract this component type from ComponentData. Returns None if the data is a different variant.

Source

fn from_data_ref(data: &ComponentData) -> Option<&Self>

Attempts to get a reference to this component type from ComponentData. Returns None if the data is a different variant or if the type cannot be referenced directly (e.g., needs conversion).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Component for ()

Source§

impl Component for TextComponent

Source§

impl Component for bool

Source§

impl Component for f32

Source§

impl Component for i32

Implementors§