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
use effectful::{
    effective::Effective,
    environment::{DynBind, EnvConfig, Environment, NativeForm},
    SendSync,
};

use crate::{
    any::TypeName,
    hkt::Marker,
    protocol::{
        walker::hint::{HasProtocol, HintMeta, Meta},
        DynVisitor,
    },
    Status,
};

use super::VisitResult;

pub trait Recoverable<'ctx, E: Environment>: DynBind<E> {
    fn visit<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, E>,
    ) -> NativeForm<'a, VisitResult, E>;
}

#[derive(SendSync)]
pub struct RecoverableProto<E: Environment>(Marker<E>);

impl<'a, 'ctx, E> TypeName::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for RecoverableProto<E>
where
    E: Environment,
{
    type T = dyn Recoverable<'ctx, E> + 'a;
}

impl<'a, 'ctx, E> TypeName::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()>
    for dyn Recoverable<'ctx, E> + 'a
where
    E: Environment,
{
    type Higher = RecoverableProto<E>;
}

pub trait RecoverableScope<'ctx, E: Environment>: DynBind<E> {
    fn new_walk<'this: 'effect, 'visitor: 'effect, 'effect>(
        &'this mut self,
        visitor: DynVisitor<'visitor, 'ctx, E>,
    ) -> NativeForm<'effect, Status, E>;
}

pub type DynRecoverableScope<'a, 'ctx, E> = &'a mut (dyn RecoverableScope<'ctx, E> + 'a);

#[derive(SendSync)]
pub struct RecoverableKnown;

impl<'a, 'ctx, E: EnvConfig> Meta::MemberTypeForLt<'a, 'ctx, E, &'a &'ctx ()> for RecoverableKnown {
    type T = RecoverableKnown;
}

impl<'a, 'ctx, E: EnvConfig> Meta::LowerTypeWithBound<'a, 'ctx, E, &'a &'ctx ()>
    for RecoverableKnown
{
    type Higher = RecoverableKnown;
}

impl<E: Environment> HintMeta for RecoverableProto<E> {
    type Known = RecoverableKnown;

    type Hint = ();

    type Effect = E;
}

pub fn visit_recoverable<'a, 'ctx, E: Environment>(
    visitor: DynVisitor<'a, 'ctx, E>,
    scope: DynRecoverableScope<'a, 'ctx, E>,
) -> NativeForm<'a, VisitResult, E> {
    if let Some(object) = visitor.0.upcast_mut::<RecoverableProto<E>>() {
        // Allow the visitor to give a hint if it wants.
        object.visit(scope)
    } else {
        // If the visitor doesn't support request hint then we continue.
        E::value(VisitResult::Skipped(())).cast()
    }
}

impl<'ctx, T, E: Environment> HasProtocol<RecoverableProto<E>> for T where T: Recoverable<'ctx, E> {}