Diffstat (limited to 'src/protocol/visitor/value.rs')
| -rw-r--r-- | src/protocol/visitor/value.rs | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/protocol/visitor/value.rs b/src/protocol/visitor/value.rs index 419964b..43a845f 100644 --- a/src/protocol/visitor/value.rs +++ b/src/protocol/visitor/value.rs @@ -6,18 +6,17 @@ use crate::{ any::{TypeName, WithContextLt}, bijective_higher_ranked_type, effect::{Effect, Future}, - higher_ranked_type, - hkt::AnySend, + hkt::AnySizedSend, protocol::{walker::hint::HintMeta, Visitor}, Flow, }; -use super::{recoverable::Recoverable, Status}; +use super::Status; /// Trait object for the [`Value`] protocol. /// /// Types implementing the [`Value`] protocol will implement this trait. -pub trait Value<'ctx, T: WithContextLt::MemberType<'ctx>, E: Effect<'ctx>> { +pub trait Value<'ctx, T: WithContextLt::MemberType<'ctx>, E: Effect> { /// Visit a value of type `T`. /// /// Use this to give a value to a visitor. Its expected that a walker @@ -27,7 +26,9 @@ pub trait Value<'ctx, T: WithContextLt::MemberType<'ctx>, E: Effect<'ctx>> { /// If a [`ControlFlow::Break`] is returned then the walker /// should stop walking as soon as possible as there has likely been /// and error. - fn visit<'a>(&'a mut self, value: WithContextLt::T<'a, 'ctx, T>) -> Future<'a, 'ctx, Flow, E>; + fn visit<'a>(&'a mut self, value: WithContextLt::T<'a, 'ctx, T>) -> Future<'a, Flow, E> + where + 'ctx: 'a; } bijective_higher_ranked_type! { @@ -35,7 +36,7 @@ bijective_higher_ranked_type! { for<'a> (dyn Value<'ctx, T, E> + Send + 'a) where { - E: Effect<'ctx>, + E: Effect, T: ?Sized + WithContextLt::MemberType<'ctx> + 'ctx } } @@ -46,26 +47,28 @@ bijective_higher_ranked_type! { (DynValue<'ctx, TypeName::T<'ctx, T>, E>) (DynValue<'ctx, T, E>) where { - E: Effect<'ctx>, + E: Effect, T: ?Sized, } } -higher_ranked_type! { - pub type ValueKnownHkt['ctx]: (AnySend) = for<'lt> () +pub struct ValueKnown; + +bijective_higher_ranked_type! { + pub type ValueKnownHkt[][]: AnySizedSend[][] for<'lt> (ValueKnown) } // This enrolls the Value protocol into the walker hint system. -impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> HintMeta<'ctx> for DynValue<'ctx, T, E> { - type Known = ValueKnownHkt<'ctx>; +impl<'a, 'ctx: 'a, T, E: Effect> HintMeta<'ctx> for DynValue<'ctx, T, E> { + type Known = ValueKnownHkt; type Hint = (); } -pub fn visit_value<'a, 'ctx, T: WithContextLt::LowerType<'a, 'ctx> + 'ctx, E: Effect<'ctx>>( +pub fn visit_value<'a, 'ctx, T: WithContextLt::LowerType<'a, 'ctx> + 'ctx, E: Effect>( visitor: Visitor<'a, 'ctx>, value: T, -) -> Future<'a, 'ctx, Status, E> +) -> Future<'a, Status, E> where WithContextLt::HigherRanked<'a, 'ctx, T>: TypeName::LowerType<'ctx> + Sized, { @@ -115,12 +118,13 @@ mod test { impl<'ctx, E> Value<'ctx, DynOwnedStatic<i32>, E> for Visitor<E> where - E: Effect<'ctx>, + E: Effect, { fn visit<'a>( &'a mut self, OwnedStatic(value): OwnedStatic<i32>, - ) -> Future<'a, 'ctx, Flow, E> { + ) -> Future<'a, Flow, E> + where 'ctx: 'a{ E::wrap(async move { self.0 = Some(value); Flow::Continue @@ -130,12 +134,15 @@ mod test { impl<'ctx, E> Value<'ctx, DynBorrowedStatic<'ctx, i32>, E> for Visitor<E> where - E: Effect<'ctx>, + E: Effect, { fn visit<'a>( &'a mut self, BorrowedStatic(value): BorrowedStatic<'ctx, i32>, - ) -> Future<'a, 'ctx, Flow, E> { + ) -> Future<'a, Flow, E> + where + 'ctx: 'a + { E::wrap(async { self.0 = Some(*value); Flow::Continue @@ -147,12 +154,12 @@ mod test { impl['ctx, E] Visitor<E> = [ DynValue<'ctx, DynOwnedStatic<i32>, E>, DynValue<'ctx, DynBorrowedStatic<'ctx, i32>, E>, - ] where E: Effect<'ctx>, + ] where E: Effect, } let mut v = Visitor::<Blocking>(None, PhantomData); let object: &mut (dyn AnyTrait<'_> + Send) = &mut v; - Spin::block_on( + let _ = Spin::block_on( object .upcast_mut::<DynValue<'_, DynOwnedStatic<i32>, Blocking>>() .unwrap() @@ -162,7 +169,7 @@ mod test { assert_eq!(v.0, Some(42)); let object: &mut (dyn AnyTrait<'_> + Send) = &mut v; - Spin::block_on( + let _ = Spin::block_on( object .upcast_mut::<DynValue<'_, DynBorrowedStatic<'_, i32>, Blocking>>() .unwrap() @@ -178,12 +185,13 @@ mod test { impl<'ctx, E> Value<'ctx, DynBorrowedMutStatic<'ctx, String>, E> for Visitor<'ctx, E> where - E: Effect<'ctx>, + E: Effect, { fn visit<'a>( &'a mut self, BorrowedMutStatic(value): BorrowedMutStatic<'ctx, String>, - ) -> Future<'a, 'ctx, Flow, E> { + ) -> Future<'a, Flow, E> + where 'ctx: 'a{ E::wrap(async { self.0 = Some(value); @@ -195,14 +203,14 @@ mod test { any_trait! { impl['ctx, E] Visitor<'ctx, E> = [ DynValue<'ctx, DynBorrowedMutStatic<'ctx, String>, E>, - ] where E: Effect<'ctx> + ] where E: Effect } let mut v = Visitor::<Blocking>(None, PhantomData); let mut y = String::from("abc"); let object: &mut (dyn AnyTrait<'_> + Send) = &mut v; - Spin::block_on( + let _ = Spin::block_on( object .upcast_mut::<DynValue<'_, DynBorrowedMutStatic<'_, _>, Blocking>>() .unwrap() @@ -219,12 +227,13 @@ mod test { impl<'ctx, E> Value<'ctx, DynBorrowedStatic<'ctx, str>, E> for Visitor<'ctx, E> where - E: Effect<'ctx>, + E: Effect, { fn visit<'a>( &'a mut self, BorrowedStatic(value): BorrowedStatic<'ctx, str>, - ) -> Future<'a, 'ctx, Flow, E> { + ) -> Future<'a, Flow, E> + where 'ctx: 'a{ E::wrap(async { self.0 = Some(value); Flow::Continue @@ -235,14 +244,14 @@ mod test { any_trait! { impl['ctx, E] Visitor<'ctx, E> = [ DynValue<'ctx, DynBorrowedStatic<'ctx, str>, E>, - ] where E: Effect<'ctx> + ] where E: Effect } let mut v = Visitor::<Blocking>(None, PhantomData); let y = String::from("abc"); let object: &mut (dyn AnyTrait<'_> + Send) = &mut v; - Spin::block_on( + let _ = Spin::block_on( object .upcast_mut::<DynValue<'_, DynBorrowedStatic<'_, str>, Blocking>>() .unwrap() |