Skip to content

Commit

Permalink
Add TupleRef, inspect_fn! (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
llehtahw authored May 10, 2024
1 parent 25ecaa9 commit 7478998
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 57 deletions.
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
workspace = { members = ["procs"] }
[package]
name = "statecs"
version = "0.3.3"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -14,3 +14,6 @@ procs = { path = "procs" }
[dev-dependencies]
tokio-test = "0.4.4"
tokio = { version = "1", features = ["full"] }

[lints.clippy]
multiple_bound_locations = "allow"
52 changes: 52 additions & 0 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,43 @@ pub trait TupleExtend {
fn extend<T>(self, v: T) -> Self::AfterExtend<T>;
}

pub trait TupleRef {
type RefType<'a>
where
Self: 'a;
type MutRefType<'a>
where
Self: 'a;
fn ref_tuple(&self) -> Self::RefType<'_>;
fn mut_tuple(&mut self) -> Self::MutRefType<'_>;
}

pub trait TupleRefRef<'a> {
type RefType: 'a;
fn _ref_tuple(self) -> Self::RefType;
}

impl<'a, T> TupleRefRef<'a> for &'a T
where
T: TupleRef,
{
type RefType = T::RefType<'a>;

fn _ref_tuple(self) -> Self::RefType {
self.ref_tuple()
}
}
impl<'a, T> TupleRefRef<'a> for &'a mut T
where
T: TupleRef,
{
type RefType = T::MutRefType<'a>;

fn _ref_tuple(self) -> Self::RefType {
self.mut_tuple()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -80,6 +117,21 @@ macro_rules! impl_extend_merge {
v$(.extend($ids))*
}
}
#[allow(non_snake_case)]
impl<$($ids,)*> $crate::entity::TupleRef for ($($ids,)*) {
type RefType<'a> = ($(&'a $ids,)*) where Self: 'a;
type MutRefType<'a> = ($(&'a mut $ids,)*) where Self: 'a;
#[allow(clippy::unused_unit)]
fn ref_tuple(&self) -> Self::RefType<'_> {
let ($($ids,)*) = self;
($($ids,)*)
}
#[allow(clippy::unused_unit)]
fn mut_tuple(&mut self) -> Self::MutRefType<'_> {
let ($($ids,)*) = self;
($($ids,)*)
}
}
};
(expand_trait_type, $tp:ty, ($first:ident $(,$ids:ident)*)) => {
impl_extend_merge!(expand_trait_type, <$tp as TupleExtend>::AfterExtend<$first>, ($($ids),*))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) mod tuple_proc;

pub mod prelude {
pub use crate::component::{ComponentGet, ComponentPut};
pub use crate::entity::{TupleExtend, TupleMerge};
pub use crate::entity::{TupleExtend, TupleMerge, TupleRefRef};
pub use crate::system::system;
pub use crate::system::system_wrap;

Expand Down
67 changes: 40 additions & 27 deletions src/tuple_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,50 @@ macro_rules! cascade_fn {
}

#[macro_export]
macro_rules! cascade_obj {
(($obj:expr, $val:expr) => $expr:expr) => {
$expr.into_tuple_processor().cascade_obj($obj, $val)
};
macro_rules! inspect {
($val:expr => $expr:expr) => {{
use $crate::IntoTupleProcessor;
use $crate::TupleRefRef;
$expr.into_tuple_processor().apply($val)
}};
}

#[macro_export]
macro_rules! cascade_obj_fn {
(ref $expr:expr) => {
|obj, x| cascade_obj!((obj, x) => $expr)
macro_rules! inspect_fn {
(once $expr:expr) => {{
let __e = $expr;
move |x| { $crate::inspect!(x => __e) }
}};
($expr:expr) => {{
let mut __e = $expr;
fn do_ref_tuple<'a, T: $crate::TupleRefRef<'a>>(a: T) -> T::RefType {
a._ref_tuple()
}
move |x| { $crate::inspect!(do_ref_tuple(x) => &__e) }
}};
}

#[macro_export]
macro_rules! mutate_fn {
(once $expr:expr) => {
let __e = $expr;
|x| { $crate::tuple_proc::mutate!(x => __e) }
};
($expr:expr) => {
move |obj, x| cascade_obj!((obj, x) => $expr)
let mut __e = $expr;
|x| { $crate::tuple_proc::mutate!(x => &mut __e) }
};
}

#[macro_export]
macro_rules! mutate {
($val:expr => $expr:expr) => {{
use $crate::entity::TupleRefRef;
use $crate::IntoTupleProcessor;
$expr.into_tuple_processor().apply($val.mut_tuple())
}};
}

#[cfg(test)]
mod tests {
use crate::*;
Expand Down Expand Up @@ -295,27 +323,12 @@ macro_rules! impl_tuple_proc {
let (r, b) = _E!(e => |($($ids,)*)| self.0($($ids,)*) => NoMerge);
r.cascade_merge(b.merge(()))
}
}
#[allow(non_snake_case)]
impl<Obj, F, OType, $($ids,)*> TupleProcessFn<F, ((Obj, ($($ids,)*)), OType)>
where
F: FnOnce(Obj, ($($ids,)*)) -> OType,
{
#[system_wrap(
E: ($($ids,)*)
_E: ($($ids,)*)
)]
pub fn cascade_obj<E, const I: usize, const J: usize>(
self,
obj: Obj,
e: E,
)
-> <OType as CascadeInto<I, J>>::Output<outof![E]>
where
outof![E]: TupleMerge,
OType: CascadeInto<I, J>
{
let (r, b) = E!(e => |($($ids,)*)| self.0(obj, ($($ids,)*)) => NoMerge);
r.cascade_merge(b.merge(()))
pub fn apply<_E>(self, e: _E) -> OType {
let (r, _) = _E!(e => |($($ids,)*)| self.0($($ids,)*) => NoMerge);
r
}
}
};
Expand Down

0 comments on commit 7478998

Please sign in to comment.