From b2a130a6e30b82a7e0f80347f916265a467a4238 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 1 Dec 2026 17:42:12 +0100 Subject: refactor(engine-ecs): replace component::IntoParts with std From/Into traits --- engine-ecs/src/actions.rs | 14 ++++++-------- engine-ecs/src/bundle.rs | 6 +++--- engine-ecs/src/component.rs | 15 +-------------- engine-ecs/src/component/local.rs | 8 ++------ engine-ecs/src/lib.rs | 23 ++++++++++------------- engine-ecs/src/pair.rs | 35 +++++++++++++++++------------------ engine-ecs/src/sole.rs | 4 ++-- 7 files changed, 41 insertions(+), 64 deletions(-) (limited to 'engine-ecs/src') diff --git a/engine-ecs/src/actions.rs b/engine-ecs/src/actions.rs index f4967df..21122c8 100644 --- a/engine-ecs/src/actions.rs +++ b/engine-ecs/src/actions.rs @@ -3,7 +3,6 @@ use std::borrow::Cow; use crate::bundle::Bundle; use crate::component::{ Component, - IntoParts, Parts as ComponentParts, RemovableTuple as RemovableComponentTuple, }; @@ -67,7 +66,7 @@ impl Actions<'_> let components_parts = bundle .iter_component_parts() - .chain([EntityName { name: name.into() }.into_parts()]) + .chain([EntityName { name: name.into() }.into()]) .collect::>(); if let Some(ComponentParts { id: sole_id, .. }) = components_parts @@ -211,7 +210,7 @@ impl Actions<'_> .relation_id(pair_id.relation()) .target_id(new_target_id) .build() - .into_parts(), + .into(), }); } @@ -241,7 +240,7 @@ impl Actions<'_> .relation_id(pair_id.relation()) .target_as_data(new_target) .build() - .into_parts(), + .into(), }); } @@ -271,7 +270,7 @@ impl Actions<'_> .relation_id(new_relation_id) .target_id(pair_id.target()) .build() - .into_parts(), + .into(), }); } @@ -301,7 +300,7 @@ impl Actions<'_> .relation_as_data(new_relation) .target_id(pair_id.target()) .build() - .into_parts(), + .into(), }); } @@ -309,8 +308,7 @@ impl Actions<'_> /// of the sole. pub fn create_sole(&mut self, sole: SoleT) -> Uid { - self.action_queue - .push(Action::CreateSole(sole.into_parts())); + self.action_queue.push(Action::CreateSole(sole.into())); SoleT::id() } diff --git a/engine-ecs/src/bundle.rs b/engine-ecs/src/bundle.rs index 4c13599..b0c0b50 100644 --- a/engine-ecs/src/bundle.rs +++ b/engine-ecs/src/bundle.rs @@ -1,6 +1,6 @@ use seq_macro::seq; -use crate::component::{IntoParts as IntoComponentParts, Parts as ComponentParts}; +use crate::component::Parts as ComponentParts; pub trait Bundle: 'static { @@ -31,10 +31,10 @@ seq!(C in 0..17 { gen_tuple_impls!(C); }); -impl Bundle for T +impl + 'static> Bundle for T { fn iter_component_parts(self) -> impl Iterator { - [self.into_parts()].into_iter() + [self.into()].into_iter() } } diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs index df495ce..71608b1 100644 --- a/engine-ecs/src/component.rs +++ b/engine-ecs/src/component.rs @@ -26,7 +26,7 @@ pub mod local; pub(crate) mod storage; /// A component that is not a pair and is not a sole. -pub trait Component: WithAssociatedId + SystemInput + Any + IntoParts +pub trait Component: WithAssociatedId + SystemInput + Any + Into { fn type_reflection() -> Option<&'static TypeReflection> where @@ -252,11 +252,6 @@ pub enum HandleError #[error("Failed to acquire component lock")] pub struct AcquireLockError(#[source] LockError); -pub trait IntoParts -{ - fn into_parts(self) -> Parts; -} - /// The parts of a component. #[derive(Debug)] #[non_exhaustive] @@ -279,14 +274,6 @@ impl Parts } } -impl IntoParts for Parts -{ - fn into_parts(self) -> Parts - { - self - } -} - #[derive(Debug)] pub struct PartsBuilder { diff --git a/engine-ecs/src/component/local.rs b/engine-ecs/src/component/local.rs index 29afcee..c4c7083 100644 --- a/engine-ecs/src/component/local.rs +++ b/engine-ecs/src/component/local.rs @@ -1,11 +1,7 @@ use std::any::type_name; use std::ops::{Deref, DerefMut}; -use crate::component::{ - HandleMut as ComponentHandleMut, - IntoParts as _, - Parts as ComponentParts, -}; +use crate::component::{HandleMut as ComponentHandleMut, Parts as ComponentParts}; use crate::pair::Pair; use crate::system::initializable::Param as InitializableParam; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; @@ -65,7 +61,7 @@ where .relation::() .target_as_data(input) .build() - .into_parts(), + .into(), ); } } diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index ee2dca6..44c7aa0 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -20,7 +20,6 @@ use crate::component::storage::{ use crate::component::{ Component, Info as ComponentInfo, - IntoParts as IntoComponentParts, Parts as ComponentParts, WithAssociatedId, }; @@ -198,7 +197,7 @@ impl World self.create_ent( entity_uid, - components_parts.chain([EntityName { name: name.into() }.into_parts()]), + components_parts.chain([EntityName { name: name.into() }.into()]), &ComponentAddingFlags { allow_soles: false }, ); } @@ -249,9 +248,9 @@ impl World self.create_ent( SoleT::id(), [ - sole.into_parts(), - EntityName { name: SoleT::name() }.into_parts(), - crate::sole::Is.into_parts(), + sole.into(), + EntityName { name: SoleT::name() }.into(), + crate::sole::Is.into(), ], &ComponentAddingFlags { allow_soles: true }, ); @@ -271,11 +270,9 @@ impl World self.create_ent( ent_id, - [SystemFunction { system }.into_parts()].into_iter().chain( - ObserverT::observed_events() - .into_iter() - .map(IntoComponentParts::into_parts), - ), + [SystemFunction { system }.into()] + .into_iter() + .chain(ObserverT::observed_events().into_iter().map(Into::into)), &ComponentAddingFlags { allow_soles: false }, ); @@ -636,8 +633,8 @@ impl World sole_parts.id, [ sole_parts, - EntityName { name: name.into() }.into_parts(), - crate::sole::Is.into_parts(), + EntityName { name: name.into() }.into(), + crate::sole::Is.into(), ], &mut self.data.component_storage, &EventSubmitter::new(&self.data.new_events), @@ -753,7 +750,7 @@ impl World unreachable!(); } - let comp_info_parts = comp_info.into_parts(); + let comp_info_parts = ComponentParts::from(comp_info); match component_storage .add_entity_component(comp_id, (comp_info_parts.id, comp_info_parts.data)) diff --git a/engine-ecs/src/pair.rs b/engine-ecs/src/pair.rs index d0b9179..d6c3b3d 100644 --- a/engine-ecs/src/pair.rs +++ b/engine-ecs/src/pair.rs @@ -8,7 +8,6 @@ use crate::component::{ HandleError as ComponentHandleError, HandleMut as ComponentHandleMut, Info as ComponentInfo, - IntoParts as IntoComponentParts, Parts as ComponentParts, Removable as RemovableComponent, WithAssociatedId as ComponentWithAssociatedId, @@ -209,55 +208,55 @@ impl Pair } } -impl IntoComponentParts for Pair +impl From> for ComponentParts { - fn into_parts(self) -> ComponentParts + fn from(pair: Pair) -> Self { - let id = self.id(); + let id = pair.id(); ComponentParts::builder() .component_info(ComponentInfo::default().with_name("Pair")) - .pair_relation_metadata(self.relation_metadata) - .pair_target_metadata(self.target_metadata) + .pair_relation_metadata(pair.relation_metadata) + .pair_target_metadata(pair.target_metadata) .build(id, ()) } } -impl IntoComponentParts for Pair +impl From> for ComponentParts where Target: Component, { - fn into_parts(self) -> ComponentParts + fn from(pair: Pair) -> Self { let id = Uid::new_pair(&UidPairParams { - relation: self.relation, + relation: pair.relation, target: Target::id(), }); ComponentParts::builder() .component_info(ComponentInfo::default().with_name("Pair")) - .pair_relation_metadata(self.relation_metadata) - .pair_target_metadata(self.target_metadata) - .build(id, self.target) + .pair_relation_metadata(pair.relation_metadata) + .pair_target_metadata(pair.target_metadata) + .build(id, pair.target) } } -impl IntoComponentParts for Pair +impl From> for ComponentParts where Relation: Component, { - fn into_parts(self) -> ComponentParts + fn from(pair: Pair) -> Self { let id = Uid::new_pair(&UidPairParams { relation: Relation::id(), - target: self.target, + target: pair.target, }); ComponentParts::builder() .component_info(ComponentInfo::default().with_name("Pair")) - .pair_relation_metadata(self.relation_metadata) - .pair_target_metadata(self.target_metadata) - .build(id, self.relation) + .pair_relation_metadata(pair.relation_metadata) + .pair_target_metadata(pair.target_metadata) + .build(id, pair.relation) } } diff --git a/engine-ecs/src/sole.rs b/engine-ecs/src/sole.rs index ee1da7a..568be3e 100644 --- a/engine-ecs/src/sole.rs +++ b/engine-ecs/src/sole.rs @@ -4,14 +4,14 @@ use std::fmt::Debug; use crate::component::{ HandleMut as ComponentHandleMut, - IntoParts as IntoComponentParts, + Parts as ComponentParts, WithAssociatedId as ComponentWithAssociatedId, }; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; use crate::{Component, World}; /// A component which has a single instance in the [`World`]. -pub trait Sole: Any + IntoComponentParts + ComponentWithAssociatedId +pub trait Sole: Any + ComponentWithAssociatedId + Into { fn type_reflection() -> Option<&'static crate::reflection::Type> where -- cgit v1.2.3-18-g5258