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§
Sourcefn into_data(self) -> ComponentData
fn into_data(self) -> ComponentData
Converts this component value into ComponentData.
Sourcefn from_data(data: ComponentData) -> Option<Self>
fn from_data(data: ComponentData) -> Option<Self>
Attempts to extract this component type from ComponentData.
Returns None if the data is a different variant.
Sourcefn from_data_ref(data: &ComponentData) -> Option<&Self>
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".