Skip to content

Commit

Permalink
Add object (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
llehtahw committed Apr 7, 2024
1 parent fb8378b commit b0df49a
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 24 deletions.
109 changes: 104 additions & 5 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
workspace = { members = ["procs"] }
[package]
name = "statecs"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures = "0.3.30"
procs = { path = "procs" }

[dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions procs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn generic_one_for_macro(
input_operands
});
stmts.push(Stmt::Expr(
parse_quote!({let _res = _closure(#input_operands); (#after_take_id, _res)}),
parse_quote!({let mut _res = _closure(#input_operands); (#after_take_id, _res)}),
None,
));
let mut block: syn::ExprBlock = parse_quote!({});
Expand All @@ -237,15 +237,15 @@ fn generic_one_for_macro(
($expr:expr => $closure:expr) => {
{
let #after_take_id = $expr;
let _closure = $closure;
let mut _closure = $closure;
let (after_take, res) = #block;
after_take.merge(res)
}
};
($expr:expr => $closure:expr => Option) => {
{
let #after_take_id = $expr;
let _closure = $closure;
let mut _closure = $closure;
let (after_take, res) = #block;
if let Some(res) = res {
Ok(after_take.merge(res))
Expand All @@ -258,7 +258,7 @@ fn generic_one_for_macro(
($expr:expr => $closure:expr => NoMerge) => {
{
let #after_take_id = $expr;
let _closure = $closure;
let mut _closure = $closure;
let (after_take, res) = #block;
(res, after_take)
}
Expand Down
67 changes: 53 additions & 14 deletions src/tuple_proc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::apply_tuple;
use crate::ComponentGet;
use crate::TupleMerge;
use futures::future::FutureExt;
use std::{future::Future, marker::PhantomData};

use procs::system_wrap;
Expand Down Expand Up @@ -70,6 +71,20 @@ macro_rules! cascade_async_fn {
};
}

#[macro_export]
macro_rules! cascade_obj_async {
(($obj:expr, $val:expr) => $expr:expr) => {
$expr.into_tuple_processor().cascade_obj_fut($obj, $val)
};
}

#[macro_export]
macro_rules! cascade_obj_async_fn {
($expr:expr) => {
move |obj, x| cascade_obj_async!((obj, x) => $expr)
};
}

#[macro_export]
macro_rules! cascade_option_async {
($val:expr => $expr:expr) => {
Expand Down Expand Up @@ -183,7 +198,7 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<F, OType, $($ids,)*> IntoTupleProcessor<($($ids,)*), OType> for F
where
F: Fn($($ids,)*) -> OType,
F: FnOnce($($ids,)*) -> OType,
{
type IType = ($($ids,)*);

Expand All @@ -197,20 +212,20 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<_F, OType, $($ids,)*> TupleProcessFn<_F, (($($ids,)*), OType)>
where
_F: Fn($($ids,)*) -> OType,
_F: FnOnce($($ids,)*) -> OType,
OType: TupleExtend,
{
#[system_wrap(
_E: ($($ids,)*)
)]
pub fn operate<_E>(&self, e: _E) -> (OType, outof![_E]) {
pub fn operate<_E>(self, e: _E) -> (OType, outof![_E]) {
let (r, b) = _E!(e => |($($ids,)*)| self.0($($ids,)*) => NoMerge);
(r, b.merge(()))
}
#[system_wrap(
_E: ($($ids,)*)
)]
pub fn cascade<_E>(&self, e: _E) -> <outof![_E] as TupleMerge>::AfterMerge<OType>
pub fn cascade<_E>(self, e: _E) -> <outof![_E] as TupleMerge>::AfterMerge<OType>
where
outof![_E]: TupleMerge,
{
Expand All @@ -221,12 +236,12 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<F, OType, $($ids,)*> TupleProcessFn<F, (($($ids,)*), Option<OType>)>
where
F: Fn($($ids,)*) -> Option<OType>,
F: FnOnce($($ids,)*) -> Option<OType>,
{
#[system_wrap(
E: ($($ids,)*)
)]
pub fn cascade_option<E>(&self, e: E) -> Option<<outof![E] as TupleMerge>::AfterMerge<OType>>
pub fn cascade_option<E>(self, e: E) -> Option<<outof![E] as TupleMerge>::AfterMerge<OType>>
where
outof![E]: TupleMerge,
OType: TupleExtend,
Expand All @@ -238,13 +253,13 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<F, ErrType, OType, $($ids,)*> TupleProcessFn<F, (($($ids,)*), Result<OType, ErrType>)>
where
F: Fn($($ids,)*) -> Result<OType, ErrType>,
F: FnOnce($($ids,)*) -> Result<OType, ErrType>,
{
#[system_wrap(
E: ($($ids,)*)
)]
pub fn cascade_result<E>(
&self,
self,
e: E,
) -> Result<<outof![E] as TupleMerge>::AfterMerge<OType>, ErrType>
where
Expand All @@ -258,7 +273,7 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<F, OType, Fut, $($ids,)*> TupleProcessFn<F, (($($ids,)*), Fut)>
where
F: Fn($($ids,)*) -> Fut,
F: FnOnce($($ids,)*) -> Fut,
Fut: Future<Output = OType>,
OType: TupleExtend,
{
Expand All @@ -283,28 +298,29 @@ macro_rules! impl_tuple_proc {
#[allow(non_snake_case)]
impl<F, OType, Fut, $($ids,)*> TupleProcessFn<F, (($($ids,)*), Fut)>
where
F: Fn($($ids,)*) -> Fut,
F: FnOnce($($ids,)*) -> Fut,
Fut: Future<Output = Option<OType>>,
{
#[system_wrap(
E: ($($ids,)*)
)]
pub async fn cascade_option_fut<E>(
pub fn cascade_option_fut<E>(
self,
e: E,
) -> Option<<outof![E] as TupleMerge>::AfterMerge<OType>>
) -> impl Future<Output=Option<<outof![E] as TupleMerge>::AfterMerge<OType>>>
where
outof![E]: TupleMerge,
OType: TupleExtend,
{
let (r, b) = E!(e => |($($ids,)*)| self.0($($ids,)*) => NoMerge);
r.await.map(|r| b.merge(()).merge(r))
// r.await.map(|r| b.merge(()).merge(r))
r.map(|x| x.map(|r| b.merge(()).merge(r)))
}
}
#[allow(non_snake_case)]
impl<F, ErrType, OType, Fut, $($ids,)*> TupleProcessFn<F, (($($ids,)*), Fut)>
where
F: Fn($($ids,)*) -> Fut,
F: FnOnce($($ids,)*) -> Fut,
Fut: Future<Output = Result<OType, ErrType>>,
{
#[system_wrap(
Expand All @@ -322,6 +338,29 @@ macro_rules! impl_tuple_proc {
r.await.map(|r| b.merge(()).merge(r))
}
}
#[allow(non_snake_case)]
impl<Obj, F, OType, Fut, $($ids,)*> TupleProcessFn<F, ((Obj, ($($ids,)*)), Fut)>
where
F: FnOnce(Obj, ($($ids,)*)) -> Fut,
Fut: Future<Output = (Obj, Option<OType>)>,
{
#[system_wrap(
E: ($($ids,)*)
)]
pub async fn cascade_obj_fut<E>(
self,
obj: Obj,
e: E,
) -> (Obj, Option<<outof![E] as TupleMerge>::AfterMerge<OType>>)
where
outof![E]: TupleMerge,
OType: TupleExtend,
{
let (r, b) = E!(e => |($($ids,)*)| self.0(obj, ($($ids,)*)) => NoMerge);
let (obj, r) = r.await;
(obj, r.map(|r| b.merge(()).merge(r)))
}
}
};
}
apply_tuple!(
Expand Down

0 comments on commit b0df49a

Please sign in to comment.