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
use effectful::{
    bound::{Bool, Dynamic, IsSend, IsSync},
    effective::{Effective, Canonical},
    environment::{ Environment},
    DynBind,
    blocking::BlockingSpin,
    forward_send_sync,
};
use mockall::mock;
use treaty::{
    any::{type_name, AnyTrait, OwnedStatic},
    protocol::{
        visitor::{visit_value, Value, VisitResult},
        AsVisitor, DynVisitor,
    },
    Flow,
};

mock! {
    pub ValueVisitor<T: type_name::Static>
    where
        for<'a, 'ctx> type_name::Lowered<'a, 'ctx, T>: Sized
    {
        pub fn visit<'a, 'ctx>(&'a mut self, value: &type_name::Lowered<'a, 'ctx, T>) -> VisitResult<()>;
    }
}

forward_send_sync!({} {} {T: (type_name::Static + Send)} MockValueVisitor<T> where {
    for<'a, 'ctx> type_name::Lowered<'a, 'ctx, T>: Sized
});

// any_trait! {
//     impl['ctx, T][E] MockValueVisitor<T, E> = [
//         ValueProto<T, E>
//     ] where
//         T: TypeName::MemberType<E>,
//         for<'a, 'b> TypeName::T<'a, 'b, T, E>: Sized,
//         for<'a> Dynamic<TypeName::T<'a, 'ctx, T, E>>: DynBind<E>,
//         E: Environment,
// }

impl<'ctx, T: type_name::Static + Send, E: Environment> Value<'ctx, T, E> for MockValueVisitor<T>
where
    for<'a, 'b> type_name::Lowered<'a, 'b, T>: Sized,
{
    fn visit<'this: 'value, 'value: 'e, 'e>(
        &'this mut self,
        value: type_name::Lowered<'value, 'ctx, T>,
    ) -> Canonical<'e, VisitResult<Dynamic<type_name::Lowered<'value, 'ctx, T>>>, E>
    where
        // type_name::Lowered<'value, 'ctx, T>: Sized,
        Dynamic<type_name::Lowered<'value, 'ctx, T>>: DynBind<E>,
        'ctx: 'this + 'value,
    {
        todo!()
    }

    // fn visit<'a>(
    //     &'a mut self,
    //     value: TypeName::T<'a, 'ctx, T, E>,
    // ) -> Canonical<'a, VisitResult<Dynamic<TypeName::T<'a, 'ctx, T, E>>>, E>
    // where
    //     'ctx: 'a,
    // {
    //     E::value(match self.visit(&value) {
    //         VisitResult::Skipped(_) => VisitResult::Skipped(Dynamic(value)),
    //         VisitResult::Control(flow) => VisitResult::Control(flow),
    //     })
    //     .cast()
    // }
}

pub trait ValueVisitorExt<'lt, 'ctx> {
    fn visit_value_and_done<'a, T>(&'a mut self, value: T)
    where
        T: type_name::WithLt<'a, 'ctx>,
        type_name::Raised<'a, 'ctx, T>: type_name::Static,
        'ctx: 'a;

    fn visit_value_and_skipped<'a, T>(&'a mut self, value: T)
    where
        T: type_name::WithLt<'a, 'ctx>,
        type_name::Raised<'a, 'ctx, T>: type_name::Static,
        'ctx: 'a;
}

impl<'lt, 'ctx, U> ValueVisitorExt<'lt, 'ctx> for U
where
    U: AsVisitor<'lt, 'ctx, BlockingSpin>,
{
    #[track_caller]
    fn visit_value_and_done<'a, T>(&'a mut self, value: T)
    where
        T: type_name::WithLt<'a, 'ctx>,
        type_name::Raised<'a, 'ctx, T>: type_name::Static,
        'ctx: 'a,
    {
        let result = visit_value::<_, BlockingSpin>(self.as_visitor(), value).wait();

        assert_eq!(result, VisitResult::Control(Flow::Done));
    }

    #[track_caller]
    fn visit_value_and_skipped<'a, T>(&'a mut self, value: T)
    where
        T: type_name::WithLt<'a, 'ctx>,
        type_name::Raised<'a, 'ctx, T>: type_name::Static,
        'ctx: 'a,
    {
        todo!()
    }

    // #[track_caller]
    // fn visit_value_and_done<'a, T>(&'a mut self, value: T)
    // where
    //     T: TypeName::LowerType<'a, 'ctx, BlockingSpin>,
    //     TypeName::HigherRanked<'a, 'ctx, T, BlockingSpin>: TypeName::MemberType<BlockingSpin>,
    //     'ctx: 'a,
    // {
    //     let result = visit_value::<_, BlockingSpin>(self.as_visitor(), value).into_value();
    //
    //     assert_eq!(result, VisitResult::Control(Flow::Done));
    // }
    //
    // #[track_caller]
    // fn visit_value_and_skipped<'a, T>(&'a mut self, value: T)
    // where
    //     T: TypeName::LowerType<'a, 'ctx, BlockingSpin>,
    //     TypeName::HigherRanked<'a, 'ctx, T, BlockingSpin>: TypeName::MemberType<BlockingSpin>,
    //     'ctx: 'a,
    // {
    //     let result = visit_value::<_, BlockingSpin>(self.as_visitor(), value).into_value();
    //
    //     assert_eq!(result.unit_skipped(), VisitResult::Skipped(()));
    // }
}