Diffstat (limited to 'src/build/builders/core/option.rs')
| -rw-r--r-- | src/build/builders/core/option.rs | 264 |
1 files changed, 132 insertions, 132 deletions
diff --git a/src/build/builders/core/option.rs b/src/build/builders/core/option.rs index 00c79c9..cb2e624 100644 --- a/src/build/builders/core/option.rs +++ b/src/build/builders/core/option.rs @@ -3,24 +3,37 @@ use core::{marker::PhantomData, ops::ControlFlow}; use crate::{ any::static_wrapper::{BorrowedStatic, BorrowedStaticValue, OwnedStatic, TempBorrowedStatic}, any_trait, - effect::{AsyncEffect, AsyncSendEffect, EffectAnyTrait, SyncEffect, Yield}, - protocol::visitor::{Tagged, TaggedScope, Value}, - AsVisitor, + effect::{Effect, Future}, + protocol::visitor::{ + tagged::{Tagged, TaggedScope}, + value::Value, + }, }; -impl<'ctx, T, E> crate::Build<'ctx, E> for Option<T> +impl<'ctx, T> crate::Build<'ctx> for Option<T> where - E: EffectAnyTrait<'ctx>, - Builder<'ctx, T::Builder, E>: AsVisitor<'ctx, E>, - T: crate::Build<'ctx, E>, + T: crate::Build<'ctx>, + <T as crate::BuilderTypes<'ctx>>::Seed: Default, { - type Builder = Builder<'ctx, T::Builder, E>; + type Builder<E: Effect<'ctx>> = Builder<'ctx, T::Builder<E>, E>; } -pub struct Builder<'ctx, B: crate::Builder<'ctx, E>, E: EffectAnyTrait<'ctx>> { +impl<'ctx, T> crate::BuilderTypes<'ctx> for Option<T> +where + T: crate::Build<'ctx>, + <T as crate::BuilderTypes<'ctx>>::Seed: Default, +{ + type Error = Error<'ctx, T::Error>; + + type Value = Option<T::Value>; + + type Seed = IgnoreMissing; +} + +pub struct Builder<'ctx, B: crate::Builder<'ctx>, E> { value: Option<Result<Option<B::Value>, Error<'ctx, B::Error>>>, ignore_missing: bool, - _marker: PhantomData<fn() -> (&'ctx (), E)>, + _marker: PhantomData<fn() -> E>, } #[derive(Default)] @@ -40,84 +53,58 @@ pub enum Error<'ctx, T> { NoVariantGiven, } -impl<'ctx, B, E> crate::Builder<'ctx, E> for Builder<'ctx, B, E> +impl<'ctx, B, E: Effect<'ctx>> crate::BuilderTypes<'ctx> for Builder<'ctx, B, E> where - E: EffectAnyTrait<'ctx>, - Self: AsVisitor<'ctx, E>, - B: crate::Builder<'ctx, E>, + B: crate::Builder<'ctx, Effect = E>, + <B as crate::BuilderTypes<'ctx>>::Seed: Default, { type Error = Error<'ctx, B::Error>; type Value = Option<B::Value>; + type Seed = IgnoreMissing; +} + +impl<'ctx, B, E: Effect<'ctx>> crate::Builder<'ctx> for Builder<'ctx, B, E> +where + B: crate::Builder<'ctx, Effect = E>, + <B as crate::BuilderTypes<'ctx>>::Seed: Default, +{ + type Effect = E; + #[inline] - fn build<'a>(self) -> Result<Self::Value, Self::Error> + fn build<'a>(self) -> Future<'a, 'ctx, Result<Self::Value, Self::Error>, E> where Self: 'a, { - match self.value { + E::wrap(core::future::ready(match self.value { Some(value) => value, None if self.ignore_missing => Ok(None), None => Err(Error::Missing), - } + })) } - type Seed = IgnoreMissing; - - fn from_seed(seed: Self::Seed) -> Self { - Self { + fn from_seed<'a>(seed: Self::Seed) -> Future<'a, 'ctx, Self, E> { + E::wrap(core::future::ready(Self { value: None, ignore_missing: match seed { IgnoreMissing::Yes => true, IgnoreMissing::No => false, }, _marker: PhantomData, - } - } - - type Effect = SyncEffect; -} - -impl<'ctx, B: crate::Builder<'ctx, SyncEffect>> AsVisitor<'ctx, SyncEffect> - for Builder<'ctx, B, SyncEffect> -{ - fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, SyncEffect> { - self + })) } -} -impl<'ctx, B: crate::Builder<'ctx, AsyncEffect>> AsVisitor<'ctx, AsyncEffect> - for Builder<'ctx, B, AsyncEffect> -{ - fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, AsyncEffect> { + fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx> { self } } -impl<'ctx, B> AsVisitor<'ctx, AsyncSendEffect> for Builder<'ctx, B, AsyncSendEffect> -where - B: crate::Builder<'ctx, AsyncSendEffect>, - <B as crate::Builder<'ctx, AsyncSendEffect>>::Value: Send, - <B as crate::Builder<'ctx, AsyncSendEffect>>::Error: Send, -{ - fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, AsyncSendEffect> { - self - } -} - -any_trait! { - impl['a, 'ctx, B] Builder<'ctx, B, SyncEffect> = [ - ] where B: crate::Builder<'ctx, SyncEffect> -} - -any_trait! { - impl['a, 'ctx, B] Builder<'ctx, B, AsyncEffect> = [ - ] where B: crate::Builder<'ctx, AsyncEffect> -} - any_trait! { - impl['a, 'ctx, B] Builder<'ctx, B, AsyncSendEffect> = [ - ] where B: crate::Builder<'ctx, AsyncSendEffect> + impl['a, 'ctx, B, E] Builder<'ctx, B, E> = [ + dyn Tagged<'ctx, E> + 'a, + ] where B: crate::Builder<'ctx, Effect = E>, E: Effect<'ctx>, + <B as crate::BuilderTypes<'ctx>>::Seed: Default, } pub mod symbol { @@ -128,62 +115,65 @@ pub mod symbol { pub const VARIANT: Symbol = Symbol::new("Variant"); } -impl<'ctx, B> Tagged<'ctx, SyncEffect> for Builder<'ctx, B, SyncEffect> +impl<'ctx, B, E: Effect<'ctx>> Tagged<'ctx, E> for Builder<'ctx, B, E> where - B: crate::DefaultBuilder<'ctx, SyncEffect, Effect = SyncEffect>, + B: crate::Builder<'ctx, Effect = E>, + <B as crate::BuilderTypes<'ctx>>::Seed: Default, { fn visit<'a>( &'a mut self, - scope: &'a mut dyn TaggedScope<'ctx, SyncEffect>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> - where - 'ctx: 'a, - { - match scope.kind() { - symbol::KEY | symbol::VARIANT => { - // This tag is the variant name/number. - let mut variant = VariantVisitor::<'ctx, B::Error> { value: None }; - scope.tag(&mut variant)?; - let variant = match variant.value { - Some(Ok(value)) => value, - Some(Err(error)) => { - self.value = Some(Err(error)); - return ControlFlow::Break(()); - } - None => { - self.value = Some(Err(Error::NoVariantGiven)); - return ControlFlow::Break(()); - } - }; + scope: &'a mut (dyn TaggedScope<'ctx, E> + Send), + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(async { + match scope.kind().await { + symbol::KEY | symbol::VARIANT => { + // This tag is the variant name/number. + let mut variant = VariantVisitor::<'ctx, B::Error, E> { + value: None, + _marker: PhantomData, + }; + scope.tag(&mut variant).await?; + let variant = match variant.value { + Some(Ok(value)) => value, + Some(Err(error)) => { + self.value = Some(Err(error)); + return ControlFlow::Break(()); + } + None => { + self.value = Some(Err(Error::NoVariantGiven)); + return ControlFlow::Break(()); + } + }; - match variant { - Variant::None => { - // Nothing more needs to be done. - self.value = Some(Ok(None)); - ControlFlow::Continue(()) - } - Variant::Some => { - // Now build a T. - let mut builder = B::default(); - scope.value(builder.as_visitor())?; - match builder.build() { - Ok(value) => { - self.value = Some(Ok(Some(value))); - ControlFlow::Continue(()) - } - Err(error) => { - self.value = Some(Err(Error::VariantSome(error))); - ControlFlow::Break(()) + match variant { + Variant::None => { + // Nothing more needs to be done. + self.value = Some(Ok(None)); + ControlFlow::Continue(()) + } + Variant::Some => { + // Now build a T. + let mut builder = B::from_seed(Default::default()).await; + scope.value(builder.as_visitor()).await?; + match builder.build().await { + Ok(value) => { + self.value = Some(Ok(Some(value))); + ControlFlow::Continue(()) + } + Err(error) => { + self.value = Some(Err(Error::VariantSome(error))); + ControlFlow::Break(()) + } } } } } + _ => { + // Ignore any other tags and just use the value. + scope.value(self).await + } } - _ => { - // Ignore any other tags and just use the value. - scope.value(self) - } - } + }) } } @@ -192,27 +182,29 @@ enum Variant { Some, } -pub struct VariantVisitor<'ctx, T> { +pub struct VariantVisitor<'ctx, T, E> { value: Option<Result<Variant, Error<'ctx, T>>>, + _marker: PhantomData<fn() -> E>, } any_trait! { - impl['a, 'ctx, T] VariantVisitor<'ctx, T> = [ - dyn Value<'a, 'ctx, TempBorrowedStatic<'a, str>, SyncEffect> + 'a, - dyn Value<'a, 'ctx, BorrowedStatic<'ctx, str>, SyncEffect> + 'a, - dyn Value<'a, 'ctx, OwnedStatic<&'static str>, SyncEffect> + 'a, - dyn Value<'a, 'ctx, OwnedStatic<u8>, SyncEffect> + 'a, - dyn Value<'a, 'ctx, OwnedStatic<u16>, SyncEffect> + 'a, - ] + impl['a, 'ctx, T, E] VariantVisitor<'ctx, T, E> = [ + dyn Value<'a, 'ctx, TempBorrowedStatic<'a, str>, E> + 'a, + dyn Value<'a, 'ctx, BorrowedStatic<'ctx, str>, E> + 'a, + dyn Value<'a, 'ctx, OwnedStatic<&'static str>, E> + 'a, + dyn Value<'a, 'ctx, OwnedStatic<u8>, E> + 'a, + dyn Value<'a, 'ctx, OwnedStatic<u16>, E> + 'a, + ] where E: Effect<'ctx> } -impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, TempBorrowedStatic<'a, str>, SyncEffect> - for VariantVisitor<'ctx, T> +impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> Value<'a, 'ctx, TempBorrowedStatic<'a, str>, E> + for VariantVisitor<'ctx, T, E> { fn visit( &'a mut self, TempBorrowedStatic(value): TempBorrowedStatic<'a, str>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(core::future::ready( match value { "None" => { self.value = Some(Ok(Variant::None)); @@ -226,17 +218,18 @@ impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, TempBorrowedStatic<'a, str>, SyncEffect> self.value = Some(Err(Error::UnknownVariantName(None))); ControlFlow::Break(()) } - } + })) } } -impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, BorrowedStatic<'ctx, str>, SyncEffect> - for VariantVisitor<'ctx, T> +impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> Value<'a, 'ctx, BorrowedStatic<'ctx, str>, E> + for VariantVisitor<'ctx, T, E> { fn visit( &'a mut self, BorrowedStatic(value): BorrowedStatic<'ctx, str>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(core::future::ready( match value { "None" => { self.value = Some(Ok(Variant::None)); @@ -252,17 +245,18 @@ impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, BorrowedStatic<'ctx, str>, SyncEffect> )))); ControlFlow::Break(()) } - } + })) } } -impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<&'static str>, SyncEffect> - for VariantVisitor<'ctx, T> +impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<&'static str>, E> + for VariantVisitor<'ctx, T, E> { fn visit( &'a mut self, OwnedStatic(value): OwnedStatic<&'static str>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(core::future::ready( match value { "None" => { self.value = Some(Ok(Variant::None)); @@ -278,15 +272,18 @@ impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<&'static str>, SyncEffect> )))); ControlFlow::Break(()) } - } + })) } } -impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<u8>, SyncEffect> for VariantVisitor<'ctx, T> { +impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<u8>, E> + for VariantVisitor<'ctx, T, E> +{ fn visit( &'a mut self, OwnedStatic(value): OwnedStatic<u8>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(core::future::ready( match value { 0 => { self.value = Some(Ok(Variant::None)); @@ -300,15 +297,18 @@ impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<u8>, SyncEffect> for VariantVi self.value = Some(Err(Error::UnknownVariantNum(Some(value)))); ControlFlow::Break(()) } - } + })) } } -impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<u16>, SyncEffect> for VariantVisitor<'ctx, T> { +impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<u16>, E> + for VariantVisitor<'ctx, T, E> +{ fn visit( &'a mut self, OwnedStatic(value): OwnedStatic<u16>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { + ) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> { + E::wrap(core::future::ready( match value { 0 => { self.value = Some(Ok(Variant::None)); @@ -322,6 +322,6 @@ impl<'a, 'ctx: 'a, T> Value<'a, 'ctx, OwnedStatic<u16>, SyncEffect> for VariantV self.value = Some(Err(Error::UnknownVariantNum(value.try_into().ok()))); ControlFlow::Break(()) } - } + })) } } |