Diffstat (limited to 'tests/hook.rs')
| -rw-r--r-- | tests/hook.rs | 84 |
1 files changed, 20 insertions, 64 deletions
diff --git a/tests/hook.rs b/tests/hook.rs index a7f4509..2ad90d5 100644 --- a/tests/hook.rs +++ b/tests/hook.rs @@ -4,7 +4,7 @@ use treaty::{ any::{any_trait, static_wrapper::OwnedStatic, AnyTrait, IndirectLtAny, LtTypeId}, build_with, into_walker, protocol::{ - visitor::Value, AnyTraitSendObj, AsyncEffect, ControlFlowFor, Effect, SyncEffect, Visitor, + visitor::Value, AnyTraitSendObj, AsyncEffect, ControlFlowFor, Effect, SyncEffect, Visitor, any_t, any_trait_send_obj, }, Build, Builder, Walker, }; @@ -25,8 +25,8 @@ struct Hook<T, Effect> { _marker: PhantomData<fn() -> Effect>, } -struct VisitorHook<'a, 'ctx: 'a, E: Effect> { - inner: E::Visitor<'a, 'ctx>, +struct VisitorHook<'a, 'ctx: 'a, E: Effect<'ctx>> { + inner: any_trait_send_obj::T<'a, 'ctx, E::VisitorHkt>, } impl<'ctx, T: Walker<'ctx, Effect = AsyncEffect> + Send> Walker<'ctx> for Hook<T, AsyncEffect> @@ -43,68 +43,23 @@ where fn walk<'a>( self, visitor: Visitor<'a, 'ctx, T::Effect>, - ) -> ControlFlowFor<'a, Self::Effect, Self::Output, Self::Error> + ) -> ControlFlowFor<'a, 'ctx, Self::Effect, Self::Output, Self::Error> where Self: 'a, + 'ctx: 'a { - let visitor: &'a mut (dyn AnyTrait<'ctx> + Send + 'a) = is_send(visitor); - let inner: T = is_send(self.inner); - - let x = async move { - let mut visitor = is_send(VisitorHook::<Self::Effect> { - inner: is_send(visitor), - }); - let flow: Pin< - Box<dyn Future<Output = core::ops::ControlFlow<Self::Error, Self::Output>> + Send>, - > = is_send(is_send(inner).walk(is_send(&mut visitor))); - is_send(assert_send(is_send(flow)).await) - }; - - let y: Pin< - Box<dyn Future<Output = core::ops::ControlFlow<Self::Error, Self::Output>> + Send>, - > = Box::pin(assert_send(x)); - todo!(); - } -} + Box::pin(async move { + let mut visitor = VisitorHook::<Self::Effect> { + inner: visitor, + }; -pub trait Captures<U> {} -impl<T: ?Sized, U> Captures<U> for T {} + let flow = self.inner.walk(&mut visitor); -pub fn make_async<'a, 'ctx, T: Walker<'ctx, Effect = AsyncEffect> + Send + 'a>( - visitor: &'a mut (dyn AnyTrait<'ctx> + Send + 'a), - inner: T, -) -> impl futures::Future<Output = ()> + Send + Captures<&'ctx ()> + 'a -where - AsyncEffect: Send, - T::Error: Send, - T::Output: Send, - T::Effect: Send, -{ - async move { - let mut visitor = is_send(VisitorHook::<AsyncEffect> { - inner: is_send(visitor), - }); - // let flow: Pin<Box<dyn Future<Output = _> + Send>> = - if false { - let _: Pin<Box<dyn Future<Output = _> + Send>> = - is_send(is_send(inner).walk(is_send(todo!()))); - } - tokio::time::sleep(Duration::from_secs(1)).await; - // is_send(assert_send(is_send(flow)).await); - () + flow.await + }) } } -pub fn is_send<T: Send>(x: T) -> T { - x -} - -pub fn assert_send<T>( - s: impl futures::Future<Output = T> + Send, -) -> impl futures::Future<Output = T> + Send { - s -} - impl<'ctx, T: Walker<'ctx, Effect = SyncEffect>> Walker<'ctx> for Hook<T, SyncEffect> { type Effect = T::Effect; @@ -115,9 +70,10 @@ 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, Self::Effect, Self::Output, Self::Error> + ) -> ControlFlowFor<'a, 'ctx, Self::Effect, Self::Output, Self::Error> where Self: 'a, + 'ctx: 'a, { let mut visitor = VisitorHook::<Self::Effect> { inner: visitor }; let flow = self.inner.walk(&mut visitor); @@ -125,7 +81,7 @@ impl<'ctx, T: Walker<'ctx, Effect = SyncEffect>> Walker<'ctx> for Hook<T, SyncEf } } -impl<'b, 'ctx, E: Effect> 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>, @@ -146,10 +102,10 @@ impl<'b, 'ctx, E: Effect> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> { 'ctx: 'a, { match id { - id if id == LtTypeId::of::<dyn Value<'a, OwnedStatic<bool>> + 'a>() => { + 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() { Some(IndirectLtAny::<'a, 'ctx, _>::new::< - dyn Value<'a, OwnedStatic<bool>> + 'a, + dyn Value<'ctx, OwnedStatic<bool>, E> + 'a, >(self as _)) } else { None @@ -160,13 +116,13 @@ impl<'b, 'ctx, E: Effect> AnyTrait<'ctx> for VisitorHook<'b, 'ctx, E> { } } -impl<'a, 'b, 'ctx, E: Effect> Value<'a, OwnedStatic<bool>> for VisitorHook<'b, 'ctx, E> { +impl<'b, 'ctx: 'b, E: Effect<'ctx>> Value<'ctx, OwnedStatic<bool>, E> for VisitorHook<'b, 'ctx, E> { #[inline] - fn visit(&'a mut self, OwnedStatic(value): OwnedStatic<bool>) -> ControlFlowFor<'a> { + fn visit<'a>(&'a mut self, OwnedStatic(value): OwnedStatic<bool>) -> ControlFlowFor<'a, 'ctx, E> where 'ctx: 'a { let visitor = self .inner .as_obj_mut() - .upcast_mut::<dyn Value<'a, OwnedStatic<bool>> + 'a>() + .upcast_mut::<dyn Value<'ctx, OwnedStatic<bool>, E> + 'a>() .unwrap(); println!("Hooked bool: {}", value); |