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
78
79
80
81
82
83
84
85
86
87
88
89
90
use core::{fmt::Display, marker::PhantomData};

use crate::{
    any::OwnedStatic,
    any_trait,
    effect::{Effect, ErasedEffective},
    protocol::{
        visitor::{Value, ValueProto, VisitResult},
        DynVisitor,
    },
    Flow,
};

impl<'ctx, M: 'ctx, E: Effect> crate::Build<'ctx, M, E> for bool {
    type Builder = Builder<E>;
}

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

impl Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Error::Incomplete => write!(f, "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> crate::Builder<'ctx, E> for Builder<E> {
    #[inline(always)]
    fn build<'a>(self) -> ErasedEffective<'a, Result<Self::Value, Self::Error>, E>
    where
        Self: 'a,
    {
        E::ready(self.0.ok_or(Error::Incomplete))
    }

    #[inline(always)]
    fn from_seed<'a>(_seed: Self::Seed) -> ErasedEffective<'a, Self, E>
    where
        Self: 'a,
    {
        E::ready(Self(None, PhantomData))
    }

    #[inline(always)]
    fn as_visitor(&mut self) -> DynVisitor<'_, 'ctx> {
        DynVisitor(self)
    }
}

any_trait! {
    impl['ctx, E] Builder<E> = [
        ValueProto<OwnedStatic<bool>, E>,
    ] where E: Effect
}

impl<'ctx, E: Effect> Value<'ctx, OwnedStatic<bool>, E> for Builder<E> {
    #[inline(always)]
    fn visit<'a>(
        &'a mut self,
        OwnedStatic(value): OwnedStatic<bool>,
    ) -> ErasedEffective<'a, VisitResult<OwnedStatic<bool>>, E>
    where
        'ctx: 'a,
    {
        self.0 = Some(value);
        E::ready(Flow::Done.into())
    }
}