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
use effectful::{
    blocking::BlockingSpin,
    bound::{Bool, IsSend, IsSync},
    effective::{Effective, Canonical},
    environment::{Environment},
    forward_send_sync,
};
use mockall::mock;
use treaty::{
    any::{type_name, AnyTrait},
    protocol::{
        visitor::{
            visit_recoverable, visit_sequence, DynRecoverableScope, DynSequenceScope, Recoverable,
            RecoverableScope, RequestHint, Sequence, SequenceScope, Value, VisitResult,
        },
        AsVisitor, DynVisitor, DynWalker,
    },
    Flow, Status,
};

mock! {
    pub RecoverableVisitor<E: Environment> {
        pub fn visit<'a, 'ctx>(&mut self, scope: DynRecoverableScope<'a, 'ctx, E>) -> VisitResult;
    }
}

forward_send_sync!({} {} {E: (Environment + Send)} MockRecoverableVisitor<E>);

// any_trait! {
//     impl['ctx][E] MockRecoverableVisitor<E> = [
//         RecoverableProto<E>
//     ] where
//         E: Environment,
// }

impl<'ctx, E: Environment + Send> Recoverable<'ctx, E> for MockRecoverableVisitor<E> {
    fn visit<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, E>,
    ) -> Canonical<'a, VisitResult, E> {
        E::value(self.visit(scope)).cast()
    }
}

mock! {
    pub RecoverableScopeVisitor<E: Environment> {
        pub fn new_walk<'a, 'b, 'lt, 'ctx>(&'a mut self, visitor: DynVisitor<'b, 'lt, 'ctx, E>) -> Status;
    }
}

forward_send_sync!({} {} {E: (Environment + Send)} MockRecoverableScopeVisitor<E>);

impl<'ctx, E: Environment + Send> RecoverableScope<'ctx, E> for MockRecoverableScopeVisitor<E> {
    fn new_walk<'a: 'c, 'b: 'c, 'd: 'c, 'c>(
        &'a mut self,
        visitor: DynVisitor<'b, 'd, 'ctx, E>,
    ) -> Canonical<'c, Status, E> {
        E::value(self.new_walk(visitor)).cast()
    }
}

pub trait RecoverableVisitorExt<'lt, 'ctx> {
    fn visit_recoverable_and_done<'a>(&'a mut self, scope: DynRecoverableScope<'a, 'ctx, BlockingSpin>);
}

impl<'lt, 'ctx, T> RecoverableVisitorExt<'lt, 'ctx> for T
where
    T: AsVisitor<'lt, 'ctx, BlockingSpin>,
{
    fn visit_recoverable_and_done<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, BlockingSpin>,
    ) {
        let result = visit_recoverable(self.as_visitor(), scope).wait();

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