use core::{marker::PhantomData, ops::ControlFlow};
use crate::{
any::static_wrapper::OwnedStatic,
any_trait,
effect::{Effect, Future},
protocol::{visitor::value::Value, Visitor},
};
impl<'ctx> crate::Build<'ctx> for bool {
type Builder<E: Effect<'ctx>> = Builder<E>;
}
#[derive(Debug)]
pub enum Error {
Incomplete,
}
pub struct Builder<E>(Option<bool>, PhantomData<fn() -> E>);
impl crate::BuilderTypes for bool {
type Seed = ();
type Error = Error;
type Value = bool;
}
impl<E> crate::BuilderTypes for Builder<E> {
type Error = Error;
type Value = bool;
type Seed = ();
}
impl<'ctx, E: Effect<'ctx>> crate::Builder<'ctx> for Builder<E> {
type Effect = E;
#[inline]
fn build<'a>(self) -> Future<'a, 'ctx, Result<Self::Value, Self::Error>, E>
where
Self: 'a,
{
E::wrap(core::future::ready(self.0.ok_or(Error::Incomplete)))
}
fn from_seed<'a>(_seed: Self::Seed) -> Future<'a, 'ctx, Self, E>
where
Self: 'a,
{
E::wrap(core::future::ready(Self(None, PhantomData)))
}
fn as_visitor(&mut self) -> Visitor<'_, 'ctx> {
self
}
}
any_trait! {
impl['a, 'ctx, E] Builder<E> = [
dyn Value<'a, 'ctx, OwnedStatic<bool>, Effect = E> + 'a,
] where E: Effect<'ctx>
}
impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<bool>> for Builder<E> {
type Effect = E;
#[inline]
fn visit(
&'a mut self,
OwnedStatic(value): OwnedStatic<bool>,
) -> Future<'a, 'ctx, ControlFlow<(), ()>, E> {
self.0 = Some(value);
E::wrap(core::future::ready(ControlFlow::Continue(())))
}
}