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
use core::{marker::PhantomData, ops::ControlFlow};

use crate::{
    any::static_wrapper::OwnedStatic,
    any_trait,
    effect::{Effect, Future},
    protocol::{
        visitor::value::{DynValue, Value},
        Visitor,
    },
    Flow,
};

impl<'ctx, M, E: Effect<'ctx>> crate::Build<'ctx, M, E> for bool {
    type Builder = 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, E> for Builder<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> = [
        DynValue<'a, 'ctx, OwnedStatic<bool>, E>,
    ] where E: Effect<'ctx>
}

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