From 21c582507ae2dc9d264c80719e39ac47d3b0122b Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 28 Feb 2024 22:30:38 +0100 Subject: refactor(ecs): use better system input type filtering solution --- ecs/src/lib.rs | 4 +- ecs/src/system.rs | 83 ++------------------------- ecs/src/system/stateful.rs | 31 +++++----- ecs/src/tuple.rs | 137 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 93 deletions(-) create mode 100644 ecs/src/tuple.rs (limited to 'ecs/src') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 573aa41..4e3e4a1 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -14,9 +14,11 @@ use crate::system::{ System, TypeErased as TypeErasedSystem, }; +use crate::tuple::FilterExclude as TupleFilterExclude; pub mod component; pub mod system; +pub mod tuple; pub use ecs_macros::Component; @@ -154,7 +156,7 @@ where Comps: ComponentSequence, { type Flags = NoInitSystemParamFlag; - type Input = (); + type Input = TupleFilterExclude; fn initialize(_system: &mut impl System, _input: Self::Input) { diff --git a/ecs/src/system.rs b/ecs/src/system.rs index d90e0a2..cbf004f 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -1,13 +1,13 @@ -use std::any::{Any, TypeId}; +use std::any::Any; use std::convert::Infallible; use std::fmt::Debug; -use std::mem::{transmute_copy, ManuallyDrop}; use std::ptr::addr_of_mut; use seq_macro::seq; use crate::component::Component; use crate::system::util::check_params_are_compatible; +use crate::tuple::{FilterElement as TupleFilterElement, With as TupleWith}; use crate::ComponentStorage; pub mod stateful; @@ -166,80 +166,9 @@ pub struct NoInitParamFlag {} /// A type which can be used as input to a [`System`]. pub trait Input: 'static {} -pub trait InputFilter +impl TupleFilterElement for InputT +where + Tup: TupleWith, { - type Filtered: FilteredInputs; + type Tuple = Tup::With; } - -pub trait FilteredInputs -{ - type InOptions: OptionInputs; - - fn into_in_options(self) -> Self::InOptions; -} - -macro_rules! impl_filtered_inputs { - ($cnt: tt) => { - seq!(I in 0..$cnt { - impl<#(Input~I: Input,)*> FilteredInputs for (#(Input~I,)*) { - type InOptions = (#(Option,)*); - - fn into_in_options(self) -> Self::InOptions { - #![allow(clippy::unused_unit)] - (#(Some(self.I),)*) - } - } - }); - }; -} - -seq!(N in 0..4 { - impl_filtered_inputs!(N); -}); - -pub trait OptionInputs -{ - fn take(&mut self) -> TakeOptionInputResult; -} - -macro_rules! impl_option_inputs { - ($cnt: tt) => { - seq!(I in 0..$cnt { - impl<#(Input~I: 'static,)*> OptionInputs for (#(Option,)*) { - fn take(&mut self) -> TakeOptionInputResult { - #( - if TypeId::of::() == TypeId::of::() { - let input = match self.I.take() { - Some(input) => ManuallyDrop::new(input), - None => { - return TakeOptionInputResult::AlreadyTaken; - } - }; - - return TakeOptionInputResult::Found( - // SAFETY: It can be transmuted safely since it is the - // same type and the type is 'static - unsafe { transmute_copy(&input) } - ); - } - )* - - TakeOptionInputResult::NotFound - } - } - }); - }; -} - -seq!(N in 0..4 { - impl_option_inputs!(N); -}); - -pub enum TakeOptionInputResult -{ - Found(Input), - NotFound, - AlreadyTaken, -} - -include!(concat!(env!("OUT_DIR"), "/system_input_impls.rs")); diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs index 9b2f279..54f9807 100644 --- a/ecs/src/system/stateful.rs +++ b/ecs/src/system/stateful.rs @@ -6,15 +6,13 @@ use seq_macro::seq; use crate::component::Component; use crate::system::util::check_params_are_compatible; -use crate::system::{ - FilteredInputs, - InputFilter, - Into as IntoSystem, - OptionInputs, - Param, - System, - TakeOptionInputResult, - TypeErased, +use crate::system::{Into as IntoSystem, Param, System, TypeErased}; +use crate::tuple::{ + Filter as TupleFilter, + FilterExclude as TupleFilterExclude, + IntoInOptions as TupleIntoInOptions, + TakeOptionElementResult as TupleTakeOptionElementResult, + WithOptionElements as TupleWithOptionElements, }; use crate::ComponentStorage; @@ -34,25 +32,28 @@ macro_rules! impl_system { Func: Fn(#(TParam~I,)*) + Copy + 'static, #(TParam~I: Param<'world>,)* #(TParam~I::Input: 'static,)* - (#(TParam~I::Input,)*): InputFilter + (#(TParam~I::Input,)*): TupleFilter, + <(#(TParam~I::Input,)*) as TupleFilter>::Out: TupleIntoInOptions { - type Input = <(#(TParam~I::Input,)*) as InputFilter>::Filtered; + type Input = <(#(TParam~I::Input,)*) as TupleFilter>::Out; fn initialize(mut self, input: Self::Input) -> Self { let mut option_input = input.into_in_options(); #( - if TypeId::of::() != TypeId::of::<()>() { + if TypeId::of::() != + TypeId::of::() + { let input = match option_input.take::() { - TakeOptionInputResult::Found(input) => input, - TakeOptionInputResult::NotFound => { + TupleTakeOptionElementResult::Found(input) => input, + TupleTakeOptionElementResult::NotFound => { panic!( "Parameter input {} not found", type_name::() ); } - TakeOptionInputResult::AlreadyTaken => { + TupleTakeOptionElementResult::AlreadyTaken => { panic!( concat!( "Parameter {} is already initialized. ", diff --git a/ecs/src/tuple.rs b/ecs/src/tuple.rs new file mode 100644 index 0000000..41ca4b3 --- /dev/null +++ b/ecs/src/tuple.rs @@ -0,0 +1,137 @@ +use std::any::TypeId; +use std::mem::{transmute_copy, ManuallyDrop}; + +use paste::paste; +use seq_macro::seq; +use util_macros::sub; + +/// Used to append a element to a tuple type. +pub trait With +{ + type With; +} + +/// Used to make all elements of a tuple type wrapped in [`Option`]. +pub trait IntoInOptions +{ + type InOptions: WithOptionElements; + + fn into_in_options(self) -> Self::InOptions; +} + +/// A tuple with all elements wrapped in [`Option`]. +pub trait WithOptionElements +{ + fn take(&mut self) -> TakeOptionElementResult; +} + +/// Used to filter the elements of a tuple type. +pub trait Filter +{ + type Out; +} + +/// Used by implementations of [`Filter`] to know whether this element should be +/// filtered out or not. +pub trait FilterElement +{ + type Tuple; +} + +/// The result of trying to [`take`] a element from a implementation of +/// [`WithOptionElements`]. +/// +/// [`take`]: WithOptionElements::take +pub enum TakeOptionElementResult +{ + /// The element was succesfully taken. + Found(Element), + + /// The element is not a element of the tuple. + NotFound, + + /// The element has already been taken + AlreadyTaken, +} + +/// Zero-sized struct excluded when filtering the elements of a tuple type using +/// implementations of [`Filter`]. +#[derive(Debug, Clone, Copy)] +pub struct FilterExclude; + +impl FilterElement for FilterExclude +{ + type Tuple = Tup; +} + +macro_rules! tuple_filter_elem_tuple { + (overflow) => { + () + }; + + ($index: tt) => { + paste! { + []::Tuple + } + }; +} + +macro_rules! impl_tuple_traits { + ($cnt: tt) => { + seq!(I in 0..$cnt { + impl With for (#(Elem~I,)*) { + type With = (#(Elem~I,)* OtherElem,); + } + + impl<#(Element~I: 'static,)*> IntoInOptions for (#(Element~I,)*) + { + type InOptions = (#(Option,)*); + + fn into_in_options(self) -> Self::InOptions + { + #![allow(clippy::unused_unit)] + (#(Some(self.I),)*) + } + } + + impl<#(Element~I: 'static,)*> WithOptionElements for (#(Option,)*) + { + fn take(&mut self) + -> TakeOptionElementResult + { + #( + if TypeId::of::() == TypeId::of::() { + let input = match self.I.take() { + Some(input) => ManuallyDrop::new(input), + None => { + return TakeOptionElementResult::AlreadyTaken; + } + }; + + return TakeOptionElementResult::Found( + // SAFETY: It can be transmuted safely since it is the + // same type and the type is 'static + unsafe { transmute_copy(&input) } + ); + } + )* + + TakeOptionElementResult::NotFound + } + } + + paste! { + impl<#(Elem~I,)*> Filter for (#(Elem~I,)*) + where + #(Elem~I: FilterElement,)* + { + type Out = sub!($cnt - 1, tuple_filter_elem_tuple); + } + } + }); + }; +} + +seq!(N in 0..4 { + impl_tuple_traits!(N); +}); -- cgit v1.2.3-18-g5258