From 1d2ded384e2669d8f48a6b138b232d0eecd214d0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 1 Dec 2026 16:45:58 +0100 Subject: refactor(engine-ecs): store component::Info in component::Parts --- engine-ecs/src/component.rs | 96 ++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 41 deletions(-) (limited to 'engine-ecs/src/component.rs') diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs index a548518..3320102 100644 --- a/engine-ecs/src/component.rs +++ b/engine-ecs/src/component.rs @@ -263,11 +263,9 @@ pub trait IntoParts pub struct Parts { pub id: Uid, - pub name: Cow<'static, str>, - pub type_name: &'static str, pub is_sole: bool, - pub type_reflection: Option<&'static TypeReflection>, pub data: Box, + pub component_info: Info, pub pair_relation_metadata: Option, pub pair_target_metadata: Option, } @@ -276,18 +274,12 @@ impl Parts { /// Creates components parts from the component information stored in /// `component_info`. - /// - /// # Panics - /// This function will panic if the type ID of the value in `data` does not equal - /// to the type ID stored in `component_info`. #[must_use] pub fn from_component_info(component_info: &Info, id: Uid, data: Box) -> Self { Self::builder() - .type_reflection(component_info.type_reflection) - .name(component_info.name.clone()) - .type_name(component_info.type_name) + .component_info(component_info.clone()) .is_sole(component_info.is_sole) .build_with_any_data(id, data) } @@ -310,10 +302,8 @@ impl IntoParts for Parts #[derive(Debug)] pub struct PartsBuilder { - name: Cow<'static, str>, - type_name: &'static str, is_sole: bool, - type_reflection: Option<&'static TypeReflection>, + component_info: Info, pair_relation_metadata: Option, pair_target_metadata: Option, } @@ -321,16 +311,9 @@ pub struct PartsBuilder impl PartsBuilder { #[must_use] - pub fn name(mut self, name: impl Into>) -> Self - { - self.name = name.into(); - self - } - - #[must_use] - pub fn type_name(mut self, type_name: &'static str) -> Self + pub fn component_info(mut self, component_info: Info) -> Self { - self.type_name = type_name.into(); + self.component_info = component_info; self } @@ -341,16 +324,6 @@ impl PartsBuilder self } - #[must_use] - pub fn type_reflection( - mut self, - type_reflection: Option<&'static TypeReflection>, - ) -> Self - { - self.type_reflection = type_reflection; - self - } - #[must_use] pub fn pair_relation_metadata(mut self, metadata: Option) -> Self @@ -371,11 +344,9 @@ impl PartsBuilder { Parts { id, - name: self.name, - type_name: self.type_name, is_sole: self.is_sole, - type_reflection: self.type_reflection, data: Box::new(data), + component_info: self.component_info, pair_relation_metadata: self.pair_relation_metadata, pair_target_metadata: self.pair_target_metadata, } @@ -386,11 +357,9 @@ impl PartsBuilder { Parts { id, - name: self.name, - type_name: self.type_name, is_sole: self.is_sole, - type_reflection: self.type_reflection, data, + component_info: self.component_info, pair_relation_metadata: self.pair_relation_metadata, pair_target_metadata: self.pair_target_metadata, } @@ -402,10 +371,8 @@ impl Default for PartsBuilder fn default() -> Self { Self { - name: "".into(), - type_name: "", is_sole: false, - type_reflection: None, + component_info: Info::default(), pair_relation_metadata: None, pair_target_metadata: None, } @@ -422,6 +389,53 @@ pub struct Info pub is_sole: bool, } +impl Default for Info +{ + fn default() -> Self + { + Self { + type_reflection: None, + name: "".into(), + type_name: "", + is_sole: false, + } + } +} + +impl Info +{ + #[must_use] + pub fn with_name(mut self, name: impl Into>) -> Self + { + self.name = name.into(); + self + } + + #[must_use] + pub fn with_type_name(mut self, type_name: &'static str) -> Self + { + self.type_name = type_name.into(); + self + } + + #[must_use] + pub fn with_is_sole(mut self, is_sole: bool) -> Self + { + self.is_sole = is_sole; + self + } + + #[must_use] + pub fn with_type_reflection( + mut self, + type_reflection: Option<&'static TypeReflection>, + ) -> Self + { + self.type_reflection = type_reflection; + self + } +} + mod sealed { pub trait Sealed {} -- cgit v1.2.3-18-g5258