Diffstat (limited to 'tests/hook.rs')
| -rw-r--r-- | tests/hook.rs | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/tests/hook.rs b/tests/hook.rs index 2ad90d5..7746ab1 100644 --- a/tests/hook.rs +++ b/tests/hook.rs @@ -1,10 +1,11 @@ -use std::{future::Future, marker::PhantomData, pin::Pin, thread::yield_now, time::Duration}; +use std::{future::Future, marker::PhantomData, pin::Pin, thread::yield_now, time::Duration, ops::ControlFlow}; use treaty::{ any::{any_trait, static_wrapper::OwnedStatic, AnyTrait, IndirectLtAny, LtTypeId}, build_with, into_walker, protocol::{ - visitor::Value, AnyTraitSendObj, AsyncEffect, ControlFlowFor, Effect, SyncEffect, Visitor, any_t, any_trait_send_obj, + any_t, as_obj, visitor::Value, AsObj, AsyncEffect, Yield, Effect, SyncEffect, + Visitor, EffectAnyTrait, }, Build, Builder, Walker, }; @@ -20,13 +21,22 @@ fn demo() { todo!(); } +#[no_mangle] +fn invert(x: bool) -> bool { + let hook = Hook { + inner: into_walker(x), + _marker: PhantomData, + }; + build_with::<<bool as Build<_>>::Builder, _>(hook).unwrap() +} + struct Hook<T, Effect> { inner: T, _marker: PhantomData<fn() -> Effect>, } -struct VisitorHook<'a, 'ctx: 'a, E: Effect<'ctx>> { - inner: any_trait_send_obj::T<'a, 'ctx, E::VisitorHkt>, +struct VisitorHook<'a, 'ctx: 'a, E: EffectAnyTrait<'ctx>> { + inner: as_obj::T<'a, 'ctx, E::AnyTrait>, } impl<'ctx, T: Walker<'ctx, Effect = AsyncEffect> + Send> Walker<'ctx> for Hook<T, AsyncEffect> @@ -43,15 +53,13 @@ where fn walk<'a>( self, visitor: Visitor<'a, 'ctx, T::Effect>, - ) -> ControlFlowFor<'a, 'ctx, Self::Effect, Self::Output, Self::Error> + ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, Self::Effect> where Self: 'a, - 'ctx: 'a + 'ctx: 'a, { Box::pin(async move { - let mut visitor = VisitorHook::<Self::Effect> { - inner: visitor, - }; + let mut visitor = VisitorHook::<Self::Effect> { inner: visitor }; let flow = self.inner.walk(&mut visitor); @@ -70,7 +78,7 @@ impl<'ctx, T: Walker<'ctx, Effect = SyncEffect>> Walker<'ctx> for Hook<T, SyncEf fn walk<'a>( self, visitor: Visitor<'a, 'ctx, T::Effect>, - ) -> ControlFlowFor<'a, 'ctx, Self::Effect, Self::Output, Self::Error> + ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, Self::Effect> where Self: 'a, 'ctx: 'a, @@ -81,7 +89,7 @@ impl<'ctx, T: Walker<'ctx, Effect = SyncEffect>> Walker<'ctx> for Hook<T, SyncEf } } -impl<'b, 'ctx: 'b, E: Effect<'ctx>> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> { +impl<'b, 'ctx: 'b, E: Effect<'ctx, ControlFlow<(), ()>>> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> { fn upcast_to_id<'a>( &'a self, id: treaty::any::LtTypeId<'ctx>, @@ -94,6 +102,7 @@ impl<'b, 'ctx: 'b, E: Effect<'ctx>> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> } } + #[inline] fn upcast_to_id_mut<'a>( &'a mut self, id: treaty::any::LtTypeId<'ctx>, @@ -116,16 +125,19 @@ impl<'b, 'ctx: 'b, E: Effect<'ctx>> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> } } -impl<'b, 'ctx: 'b, E: Effect<'ctx>> Value<'ctx, OwnedStatic<bool>, E> for VisitorHook<'b, 'ctx, E> { +impl<'b, 'ctx: 'b, E: Effect<'ctx, ControlFlow<(), ()>>> Value<'ctx, OwnedStatic<bool>, E> for VisitorHook<'b, 'ctx, E> { #[inline] - fn visit<'a>(&'a mut self, OwnedStatic(value): OwnedStatic<bool>) -> ControlFlowFor<'a, 'ctx, E> where 'ctx: 'a { + fn visit<'a>(&'a mut self, OwnedStatic(value): OwnedStatic<bool>) -> Yield<'a, 'ctx, ControlFlow<(), ()>, E> + where + 'ctx: 'a, + { let visitor = self .inner .as_obj_mut() .upcast_mut::<dyn Value<'ctx, OwnedStatic<bool>, E> + 'a>() .unwrap(); - println!("Hooked bool: {}", value); + // println!("Hooked bool: {}", value); visitor.visit(OwnedStatic(!value)) } } |