1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use core::{marker::PhantomData, ops::ControlFlow};

use crate::{
    any::static_wrapper::OwnedStatic,
    any_trait,
    effect::{AsObj, AsyncEffect, Effect, SyncEffect, Yield},
    protocol::visitor::Value,
    AsVisitor,
};

impl<'ctx, E: Effect<'ctx, ControlFlow<(), ()>>> crate::Build<'ctx, E> for bool
where
    Builder<E>: AsVisitor<'ctx, E>,
{
    type Builder = Builder<E>;
}

#[derive(Debug)]
pub enum Error {
    Incomplete,
}

pub struct Builder<E>(Option<bool>, PhantomData<fn() -> E>);

impl<'ctx, E: Effect<'ctx, ControlFlow<(), ()>>> crate::Builder<'ctx, E> for Builder<E>
where
    Self: AsVisitor<'ctx, E>,
{
    type Error = Error;

    type Value = bool;

    #[inline]
    fn build<'a>(self) -> Result<Self::Value, Self::Error>
    where
        Self: 'a,
    {
        self.0.ok_or(Error::Incomplete)
    }

    type Seed = ();

    fn from_seed(_seed: Self::Seed) -> Self {
        Self(None, PhantomData)
    }

    type Effect = SyncEffect;
}

impl<'ctx> AsVisitor<'ctx, SyncEffect> for Builder<SyncEffect> {
    fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, SyncEffect> {
        self
    }
}

impl<'ctx> AsVisitor<'ctx, AsyncEffect> for Builder<AsyncEffect> {
    fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, AsyncEffect> {
        self
    }
}

any_trait! {
    impl['a, 'ctx, E] Builder<E> = [
        dyn Value<'a, 'ctx, OwnedStatic<bool>, SyncEffect> + 'a,
    ]
}

impl<'a, 'ctx: 'a, E> Value<'a, 'ctx, OwnedStatic<bool>, SyncEffect> for Builder<E> {
    #[inline]
    fn visit(
        &'a mut self,
        OwnedStatic(value): OwnedStatic<bool>,
    ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> {
        self.0 = Some(value);
        ControlFlow::Continue(())
    }
}