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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use core::fmt::{Debug, Display};
use effectful::{
    bound::{Bool, IsSend, IsSync},
    environment::{DynBind, Environment, NativeForm},
    effective::Effective,
    forward_send_sync,
};
use mockall::mock;
use treaty::{
    any::{indirect, AnyTrait, AnyTraitObject, TypeNameId},
    protocol::{AsVisitor, DynVisitor},
    Builder, BuilderTypes,
};

use crate::common::{ContextLock, StaticTypeMap};

use self::__mock_MockBuilder::__from_seed::Context;

use super::ContextGuard;

#[derive(Debug)]
pub struct EmptyError;

impl Display for EmptyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "EmptyError")
    }
}

mock! {
    pub Builder<Seed: 'static, Value: 'static, Error: 'static, E: Environment> {
        pub fn from_seed(seed: Seed) -> Self;
        pub fn build(self) -> Result<Value, Error>;

        pub fn traits(&self, id: TypeNameId) -> &Option<Box<dyn for<'ctx> AnyTrait<'ctx, E>>>;
        pub fn traits_mut(&mut self, id: TypeNameId) -> &mut Option<Box<dyn for<'ctx> AnyTrait<'ctx, E>>>;
    }
}

// forward_send_sync!({Seed: ('static), Value: ('static), Error: ('static)} {} {E: (Environment)} MockBuilder<Seed, Value, Error, E>);

unsafe impl<Seed: 'static, Value: 'static, Error: 'static, E: Environment, F: Bool> IsSend<F>
    for MockBuilder<Seed, Value, Error, E>
{
}
unsafe impl<Seed: 'static, Value: 'static, Error: 'static, E: Environment, F: Bool> IsSync<F>
    for MockBuilder<Seed, Value, Error, E>
{
}

impl<Seed, Value, Error: Display + Debug, E: Environment> BuilderTypes<E>
    for MockBuilder<Seed, Value, Error, E>
where
    Seed: DynBind<E>,
    Value: DynBind<E>,
    Error: DynBind<E>,
{
    type Seed = Seed;

    type Error = Error;

    type Value = Value;
}

impl<Seed: 'static, Value: 'static, Error: 'static, E: Environment>
    MockBuilder<Seed, Value, Error, E>
{
    pub fn lock_from_seed_context<'a>() -> ContextGuard<'a, Context<Seed, Value, Error, E>> {
        static LOCKS: StaticTypeMap = StaticTypeMap::new();

        LOCKS
            .get_or_init(|| {
                ContextLock::new(MockBuilder::from_seed_context(), |context| {
                    context.checkpoint()
                })
            })
            .lock()
    }
}

impl<
        'ctx,
        Seed: DynBind<E>,
        Value: DynBind<E>,
        Error: DynBind<E> + Display + Debug,
        E: Environment,
    > Builder<'ctx, E> for MockBuilder<Seed, Value, Error, E>
{
    fn from_seed<'a>(seed: Self::Seed) -> NativeForm<'a, Self, E>
    where
        Self: 'a,
    {
        E::value(Self::from_seed(seed)).cast()
    }

    fn build<'a>(self) -> NativeForm<'a, Result<Self::Value, Self::Error>, E>
    where
        Self: 'a,
    {
        E::value(self.build()).cast()
    }
}

impl<'ctx, Seed, Value, Error: Display + Debug, E: Environment> AsVisitor<'ctx, E>
    for MockBuilder<Seed, Value, Error, E>
{
    fn as_visitor<'a>(&'a mut self) -> DynVisitor<'a, 'ctx, E>
    where
        'ctx: 'a,
    {
        DynVisitor(self)
    }
}

impl<'ctx, Seed, Value, Error, E: Environment> AnyTrait<'ctx, E>
    for MockBuilder<Seed, Value, Error, E>
{
    fn upcast_to_id<'a>(
        &'a self,
        id: TypeNameId,
    ) -> Option<AnyTraitObject<'a, 'ctx, indirect::Ref, E>>
    where
        'ctx: 'a,
    {
        // Find the first trait handler that wants to upcast.
        self.traits(id).as_ref().and_then(|t| t.upcast_to_id(id))
    }

    fn upcast_to_id_mut<'a>(
        &'a mut self,
        id: TypeNameId,
    ) -> Option<AnyTraitObject<'a, 'ctx, indirect::Mut, E>>
    where
        'ctx: 'a,
    {
        // Find the first trait handler that wants to upcast.
        self.traits_mut(id)
            .as_mut()
            .and_then(|t| t.upcast_to_id_mut(id))
    }

    type Available = () where Self: Sized;
}