Diffstat (limited to 'src/walk/walkers/core/struct.rs')
| -rw-r--r-- | src/walk/walkers/core/struct.rs | 139 |
1 files changed, 73 insertions, 66 deletions
diff --git a/src/walk/walkers/core/struct.rs b/src/walk/walkers/core/struct.rs index 93e8bba..8fe0ce0 100644 --- a/src/walk/walkers/core/struct.rs +++ b/src/walk/walkers/core/struct.rs @@ -12,10 +12,7 @@ use crate::{ RecoverableKnown, RecoverableProto, RecoverableScope, SequenceKnown, SequenceProto, SequenceScope, TagConst, TagDyn, TagError, TagHint, TagKnown, TagProto, ValueKnown, ValueProto, VisitResult, - }, - walker::hint::{Hint, Known}, - walker::hint::{HintMeta, HintProto}, - Visitor, + }, walker::hint::{Hint, MetaKnown}, walker::hint::{HintMeta, HintProto, MetaHint}, DynVisitor, DynWalker }, Flow, Status, WalkerTypes, TAG_FIELD_NAMES, TAG_MAP, TAG_STRUCT, TAG_TYPE_ID, TAG_TYPE_NAME, }; @@ -58,7 +55,7 @@ pub trait StructTypeInfo<'ctx, M>: 'static { fn walk_field<'a, E: Effect>( index: usize, value: &'ctx Self::T, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, ) -> Future<'a, Result<Flow, Self::FieldError>, E>; } @@ -117,14 +114,14 @@ where { fn walk<'a>( mut self, - visitor: Visitor<'a, 'ctx>, + mut visitor: DynVisitor<'a, 'ctx>, ) -> Future<'a, Result<Self::Output, Self::Error>, E> where Self: 'a, { - E::wrap(async { + E::wrap(async move { // Use the recoverable walk to not duplicate the code. - let _ = RecoverableScope::<'ctx, E>::new_walk(&mut self, visitor).await; + let _ = RecoverableScope::<'ctx, E>::new_walk(&mut self, visitor.cast()).await; // Get the error if there was one. match self.error { @@ -137,22 +134,22 @@ where any_trait! { impl['ctx, T, I, M, E] StructWalker<'ctx, T, I, M, E> = [ - HintProto<RecoverableProto<E>, E>, - HintProto<SequenceProto<E>, E>, - HintProto<ValueProto<BorrowedStaticHrt<T>, E>, E>, - HintProto<TagProto<TagDyn, E>, E>, - HintProto<TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>, E>, - HintProto<TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>, E>, - HintProto<TagProto<TagConst<{ TAG_MAP.to_int() }>, E>, E>, - HintProto<TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>, E>, - HintProto<TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>, E>, + HintProto<RecoverableProto<E>>, + HintProto<SequenceProto<E>>, + HintProto<ValueProto<BorrowedStaticHrt<T>, E>>, + HintProto<TagProto<TagDyn, E>>, + HintProto<TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>>, + HintProto<TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>>, + HintProto<TagProto<TagConst<{ TAG_MAP.to_int() }>, E>>, + HintProto<TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>>, + HintProto<TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>>, ] where E: Effect, T: Send + Sync + 'static, I: StructTypeInfo<'ctx, M, T = T> } -impl<'ctx, T, I, M, E> Hint<'ctx, RecoverableProto<E>, E> for StructWalker<'ctx, T, I, M, E> +impl<'ctx, T, I, M, E> Hint<'ctx, RecoverableProto<E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, I: StructTypeInfo<'ctx, M, T = T>, @@ -160,7 +157,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <RecoverableProto<E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map( @@ -175,12 +172,12 @@ where fn known<'a>( &'a mut self, _hint: &'a <RecoverableProto<E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, RecoverableProto<E>>, ()>, E> { + ) -> Future<'a, Result<MetaKnown<'a, 'ctx, RecoverableProto<E>>, ()>, E> { E::ready(Ok(RecoverableKnown)) } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, @@ -189,8 +186,8 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, - _hint: <TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E> as HintMeta>::Hint, + visitor: DynVisitor<'a, 'ctx>, + _hint: MetaHint<'a, 'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>>, ) -> Future<'a, Flow, E> { E::map( visit_tag::<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E, _>( @@ -211,10 +208,10 @@ where fn known<'a>( &'a mut self, - _hint: &'a <TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E> as HintMeta>::Hint, + _hint: &'a MetaHint<'a, 'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>>, ) -> Future< 'a, - Result<Known<'a, 'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>>, ()>, + Result<MetaKnown<'a, 'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>>, ()>, E, > { E::ready(Ok(TagKnown { @@ -223,7 +220,7 @@ where } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, @@ -232,7 +229,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map( @@ -255,15 +252,18 @@ where fn known<'a>( &'a mut self, _hint: &'a <TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>>, ()>, E> - { + ) -> Future< + 'a, + Result<MetaKnown<'a, 'ctx, TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>>, ()>, + E, + > { E::ready(Ok(TagKnown { kind_available: Some(true), })) } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, @@ -272,7 +272,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <TagProto<TagConst<{ TAG_MAP.to_int() }>, E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map( @@ -291,7 +291,7 @@ where fn known<'a>( &'a mut self, _hint: &'a <TagProto<TagConst<{ TAG_MAP.to_int() }>, E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>>, ()>, E> + ) -> Future<'a, Result<MetaKnown<'a, 'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>>, ()>, E> { E::ready(Ok(TagKnown { kind_available: Some(true), @@ -299,7 +299,7 @@ where } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, @@ -308,7 +308,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map( @@ -331,15 +331,18 @@ where fn known<'a>( &'a mut self, _hint: &'a <TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>>, ()>, E> - { + ) -> Future< + 'a, + Result<MetaKnown<'a, 'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>>, ()>, + E, + > { E::ready(Ok(TagKnown { kind_available: Some(true), })) } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, @@ -348,7 +351,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map( @@ -371,15 +374,18 @@ where fn known<'a>( &'a mut self, _hint: &'a <TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>>, ()>, E> - { + ) -> Future< + 'a, + Result<MetaKnown<'a, 'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>>, ()>, + E, + > { E::ready(Ok(TagKnown { kind_available: Some(true), })) } } -impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagDyn, E>, E> for StructWalker<'ctx, T, I, M, E> +impl<'ctx, T, I, M, E> Hint<'ctx, TagProto<TagDyn, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, I: StructTypeInfo<'ctx, M>, @@ -387,38 +393,39 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, hint: <TagProto<TagDyn, E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { match hint.kind.0 { crate::TAG_TYPE_ID => { - Hint::<'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>, E>::hint( + Hint::<'ctx, TagProto<TagConst<{ TAG_TYPE_ID.to_int() }>, E>>::hint( self, visitor, TagHint { kind: TagConst }, ) } crate::TAG_STRUCT => { - Hint::<'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>, E>::hint( + Hint::<'ctx, TagProto<TagConst<{ TAG_STRUCT.to_int() }>, E>>::hint( self, visitor, TagHint { kind: TagConst }, ) } - crate::TAG_MAP => Hint::<'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>, E>::hint( + crate::TAG_MAP => Hint::<'ctx, TagProto<TagConst<{ TAG_MAP.to_int() }>, E>>::hint( self, visitor, TagHint { kind: TagConst }, ), - crate::TAG_TYPE_NAME => Hint::< - 'ctx, - TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>, - E, - >::hint(self, visitor, TagHint { kind: TagConst }), + crate::TAG_TYPE_NAME => { + Hint::<'ctx, TagProto<TagConst<{ TAG_TYPE_NAME.to_int() }>, E>>::hint( + self, + visitor, + TagHint { kind: TagConst }, + ) + } crate::TAG_FIELD_NAMES => Hint::< 'ctx, TagProto<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E>, - E, >::hint(self, visitor, TagHint { kind: TagConst }), _ => E::ready(Flow::Continue), } @@ -427,7 +434,7 @@ where fn known<'a>( &'a mut self, hint: &'a <TagProto<TagDyn, E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, TagProto<TagDyn, E>>, ()>, E> { + ) -> Future<'a, Result<MetaKnown<'a, 'ctx, TagProto<TagDyn, E>>, ()>, E> { E::ready(match hint.kind { TagDyn(crate::TAG_TYPE_ID) | TagDyn(crate::TAG_STRUCT) => Ok(TagKnown { kind_available: Some(true), @@ -439,14 +446,14 @@ where } } -impl<'ctx, T, I, M, E> Hint<'ctx, ValueProto<BorrowedStaticHrt<T>, E>, E> +impl<'ctx, T, I, M, E> Hint<'ctx, ValueProto<BorrowedStaticHrt<T>, E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, I: StructTypeInfo<'ctx, M>, T: Send + Sync + 'static, { - fn hint<'a>(&'a mut self, visitor: Visitor<'a, 'ctx>, _hint: ()) -> Future<'a, Flow, E> { + fn hint<'a>(&'a mut self, visitor: DynVisitor<'a, 'ctx>, _hint: ()) -> Future<'a, Flow, E> { E::map( visit_value::<_, E>(visitor, BorrowedStatic(self.value)), |status| match status { @@ -464,7 +471,7 @@ where } } -impl<'ctx, T, I, M, E> Hint<'ctx, SequenceProto<E>, E> for StructWalker<'ctx, T, I, M, E> +impl<'ctx, T, I, M, E> Hint<'ctx, SequenceProto<E>> for StructWalker<'ctx, T, I, M, E> where E: Effect, I: StructTypeInfo<'ctx, M, T = T>, @@ -472,7 +479,7 @@ where { fn hint<'a>( &'a mut self, - visitor: Visitor<'a, 'ctx>, + visitor: DynVisitor<'a, 'ctx>, _hint: <SequenceProto<E> as HintMeta>::Hint, ) -> Future<'a, Flow, E> { E::map(visit_sequence::<E>(visitor, self), |status| match status { @@ -484,7 +491,7 @@ where fn known<'a>( &'a mut self, _hint: &'a <SequenceProto<E> as HintMeta>::Hint, - ) -> Future<'a, Result<Known<'a, 'ctx, SequenceProto<E>>, ()>, E> { + ) -> Future<'a, Result<MetaKnown<'a, 'ctx, SequenceProto<E>>, ()>, E> { let len = I::FIELDS.len(); E::ready(Ok(SequenceKnown { @@ -504,7 +511,7 @@ where E::ready((len, Some(len))) } - fn next<'a>(&'a mut self, visitor: Visitor<'a, 'ctx>) -> Future<'a, Flow, E> { + fn next<'a>(&'a mut self, visitor: DynVisitor<'a, 'ctx>) -> Future<'a, Flow, E> { if self.index >= I::FIELDS.len() { return E::ready(Flow::Done); } @@ -532,7 +539,7 @@ where I: StructTypeInfo<'ctx, M, T = T>, T: Send + Sync + 'static, { - fn new_walk<'a>(&'a mut self, visitor: Visitor<'a, 'ctx>) -> Future<'a, Status, E> { + fn new_walk<'a>(&'a mut self, mut visitor: DynVisitor<'a, 'ctx>) -> Future<'a, Status, E> { // Reset the errors to default state. self.error = None; @@ -541,14 +548,14 @@ where E::wrap(async move { // We should check if the visitor wants something specific. - match visit_request_hint::<E>(visitor, self).await { + match visit_request_hint::<E>(visitor.cast(), DynWalker(self)).await { VisitResult::Skipped(_) | VisitResult::Control(Flow::Continue) => {} VisitResult::Control(Flow::Done) => return Ok(()), VisitResult::Control(Flow::Err) => return Err(()), } // Attempt to visit the value directly. - match visit_value::<_, E>(visitor, BorrowedStatic(self.value)).await { + match visit_value::<_, E>(visitor.cast(), BorrowedStatic(self.value)).await { VisitResult::Skipped(_) | VisitResult::Control(Flow::Continue) => {} VisitResult::Control(Flow::Done) => return Ok(()), VisitResult::Control(Flow::Err) => return Err(()), @@ -563,7 +570,7 @@ where match visit_tag::<TagConst<{ TAG_TYPE_ID.to_int() }>, E, _>( TagConst, - visitor, + visitor.cast(), ValueWalker::new(TypeId::of::<T>()), ) .await @@ -579,7 +586,7 @@ where match visit_tag::<TagConst<{ TAG_STRUCT.to_int() }>, E, _>( TagConst, - visitor, + visitor.cast(), NoopWalker::new(), ) .await @@ -587,7 +594,7 @@ where Ok(VisitResult::Skipped(_)) => { match visit_tag::<TagConst<{ TAG_MAP.to_int() }>, E, _>( TagConst, - visitor, + visitor.cast(), NoopWalker::new(), ) .await @@ -612,7 +619,7 @@ where match visit_tag::<TagConst<{ TAG_TYPE_NAME.to_int() }>, E, _>( TagConst, - visitor, + visitor.cast(), ValueWalker::new(I::NAME), ) .await @@ -628,7 +635,7 @@ where match visit_tag::<TagConst<{ TAG_FIELD_NAMES.to_int() }>, E, _>( TagConst, - visitor, + visitor.cast(), StaticSliceWalker::<_, ValueWalker<&'static str>>::new(I::FIELDS), ) .await |