Diffstat (limited to 'tests/hook.rs')
| -rw-r--r-- | tests/hook.rs | 114 |
1 files changed, 46 insertions, 68 deletions
diff --git a/tests/hook.rs b/tests/hook.rs index 9dacb62..ebdf728 100644 --- a/tests/hook.rs +++ b/tests/hook.rs @@ -1,69 +1,72 @@ -use std::{ - future::Future, marker::PhantomData, ops::ControlFlow, pin::Pin, thread::yield_now, - time::Duration, -}; +use std::{marker::PhantomData, ops::ControlFlow, pin::Pin, thread::yield_now, time::Duration}; use treaty::{ any::{any_trait, static_wrapper::OwnedStatic, AnyTrait, IndirectLtAny, LtTypeId}, - build_with, - effect::{any_t, as_obj, AsObj, AsyncEffect, Effect, EffectAnyTrait, SyncEffect, Yield}, - into_walker, - protocol::{visitor::Value, Visitor}, - Build, Builder, Walker, + effect::{BlockOn, Blocking, Effect, Future, Spin}, + protocol::visitor::value::Value, + protocol::Visitor, + transform, Build, Builder, Walk, Walker, builders::core::option::IgnoreMissing, WalkerTypes, }; #[test] fn demo() { - let hook = Hook { - inner: into_walker(true), + let hook = Hook::<_, Blocking> { + inner: true.into_walker::<Blocking>(), _marker: PhantomData, }; // let x = build_with::<<bool as Build<_>>::Builder, _>(hook).unwrap(); // dbg!(x); - let x = build_with::<<Option<bool> as Build<_>>::Builder, _>(hook).unwrap(); + // let x = Spin::block_on(transform::<<Option<bool> as Build>::Builder<_>, _, Blocking>(IgnoreMissing::No, hook)); + let x = Spin::block_on(transform::<<bool as Build>::Builder<_>, _, Blocking>((), hook)); dbg!(x); todo!(); } #[no_mangle] fn invert(x: bool) -> bool { - let hook = Hook { - inner: into_walker(x), + let hook = Hook::<_, Blocking> { + inner: x.into_walker::<Blocking>(), _marker: PhantomData, }; - build_with::<<bool as Build<_>>::Builder, _>(hook).unwrap() + Spin::block_on(transform::<<bool as Build>::Builder<_>, _, Blocking>( + (), + hook, + )) + .0 + .unwrap() } -struct Hook<T, Effect> { +struct Hook<T, E> { inner: T, - _marker: PhantomData<fn() -> Effect>, + _marker: PhantomData<fn() -> E>, } -struct VisitorHook<'a, 'ctx: 'a, E: EffectAnyTrait<'ctx>> { - inner: as_obj::T<'a, 'ctx, E::AnyTrait>, +struct VisitorHook<'a, 'ctx: 'a, E> { + inner: Visitor<'a, 'ctx>, + _marker: PhantomData<fn() -> E>, } -impl<'ctx, T: Walker<'ctx, Effect = AsyncEffect> + Send> Walker<'ctx> for Hook<T, AsyncEffect> -where - <T as Walker<'ctx>>::Error: Send, - <T as Walker<'ctx>>::Output: Send, -{ - type Effect = T::Effect; - +impl<'ctx, T: Walker<'ctx, Effect = E>, E: Effect<'ctx>> WalkerTypes<'ctx> for Hook<T, E> { type Error = T::Error; type Output = T::Output; +} + +impl<'ctx, T: Walker<'ctx, Effect = E>, E: Effect<'ctx>> Walker<'ctx> for Hook<T, E> { + type Effect = E; fn walk<'a>( self, - visitor: Visitor<'a, 'ctx, T::Effect>, - ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, Self::Effect> + visitor: Visitor<'a, 'ctx>, + ) -> Future<'a, 'ctx, Result<Self::Output, Self::Error>, E> where Self: 'a, - 'ctx: 'a, { - Box::pin(async move { - let mut visitor = VisitorHook::<Self::Effect> { inner: visitor }; + E::wrap(async { + let mut visitor = VisitorHook::<E> { + inner: visitor, + _marker: PhantomData, + }; let flow = self.inner.walk(&mut visitor); @@ -72,30 +75,7 @@ where } } -impl<'ctx, T: Walker<'ctx, Effect = SyncEffect>> Walker<'ctx> for Hook<T, SyncEffect> { - type Effect = T::Effect; - - type Error = T::Error; - - type Output = T::Output; - - fn walk<'a>( - self, - visitor: Visitor<'a, 'ctx, T::Effect>, - ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, Self::Effect> - where - Self: 'a, - 'ctx: 'a, - { - let mut visitor = VisitorHook::<Self::Effect> { inner: visitor }; - let flow = self.inner.walk(&mut visitor); - flow - } -} - -impl<'b, 'ctx: 'b, E: Effect<'ctx, ControlFlow<(), ()>>> AnyTrait<'ctx> - for VisitorHook<'b, 'ctx, E> -{ +impl<'b, 'ctx: 'b, E: Effect<'ctx>> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> { fn upcast_to_id<'a>( &'a self, id: treaty::any::LtTypeId<'ctx>, @@ -104,7 +84,7 @@ impl<'b, 'ctx: 'b, E: Effect<'ctx, ControlFlow<(), ()>>> AnyTrait<'ctx> 'ctx: 'a, { match id { - id => self.inner.as_obj().upcast_to_id(id), + id => self.inner.upcast_to_id(id), } } @@ -117,34 +97,32 @@ impl<'b, 'ctx: 'b, E: Effect<'ctx, ControlFlow<(), ()>>> AnyTrait<'ctx> 'ctx: 'a, { match id { - id if id == LtTypeId::of::<dyn Value<'_, 'ctx, OwnedStatic<bool>, E> + 'a>() => { - if self.inner.as_obj_mut().upcast_to_id_mut(id).is_some() { + id if id == LtTypeId::of::<dyn Value<'a, 'ctx, OwnedStatic<bool>, E> + 'a>() => { + // println!("hook: {:?}", id); + if self.inner.upcast_to_id_mut(id).is_some() { Some(IndirectLtAny::<'a, 'ctx, _>::new::< - dyn Value<'_, 'ctx, OwnedStatic<bool>, E> + 'a, + dyn Value<'a, 'ctx, OwnedStatic<bool>, E> + 'a, >(self as _)) } else { None } } - id => self.inner.as_obj_mut().upcast_to_id_mut(id), + id => { + // println!("fallback: {:?}", id); + self.inner.upcast_to_id_mut(id) + }, } } } -impl<'a, 'b, 'ctx: 'b + 'a, E: Effect<'ctx, ControlFlow<(), ()>>> Value<'a, 'ctx, OwnedStatic<bool>, E> - for VisitorHook<'b, 'ctx, E> -{ +impl<'a, 'b, 'ctx: 'a + 'b, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<bool>, E> for VisitorHook<'b, 'ctx, E> { #[inline] fn visit( &'a mut self, OwnedStatic(value): OwnedStatic<bool>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, E> - where - 'ctx: 'a, - { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { let visitor = self .inner - .as_obj_mut() .upcast_mut::<dyn Value<'a, 'ctx, OwnedStatic<bool>, E> + 'a>() .unwrap(); |