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
//! Protocol for giving a visitor a borrowed value.

use core::marker::PhantomData;

use crate::{builtins::walker::hint::Meta, protocol::Protocol};

pub struct Value<T: ?Sized>(PhantomData<fn() -> *const T>);

pub enum Kind<'a, 'ctx: 'a, T: ?Sized> {
    Temp(&'a T),
    Context(&'ctx T),
    TempMut(&'a mut T),
    ContextMut(&'ctx mut T),
}

impl<'a, 'ctx, T: ?Sized> Kind<'a, 'ctx, T> {
    pub fn into_temp(self) -> &'a T {
        use Kind as K;
        match self {
            K::Temp(v) | K::Context(v) => v,
            K::TempMut(v) | K::ContextMut(v) => v,
        }
    }

    pub fn into_context(self) -> Result<&'ctx T, Self> {
        use Kind as K;
        match self {
            K::Context(v) => Ok(v),
            K::ContextMut(v) => Ok(v),
            this @ (K::Temp(_) | K::TempMut(_)) => Err(this),
        }
    }

    pub fn into_temp_mut(self) -> Result<&'a mut T, Self> {
        use Kind as K;
        match self {
            K::TempMut(v) | K::ContextMut(v) => Ok(v),
            this @ (K::Temp(_) | K::Context(_)) => Err(this),
        }
    }

    pub fn into_context_mut(self) -> Result<&'ctx mut T, Self> {
        use Kind as K;
        match self {
            K::ContextMut(v) => Ok(v),
            this @ (K::Temp(_) | K::Context(_) | K::TempMut(_)) => {
                Err(this)
            }
        }
    }

    pub fn variant(&self) -> KindVariants {
        use Kind as K;
        use KindVariants as V;
        match self {
            K::Temp(_) => V::Temp,
            K::Context(_) => V::Context,
            K::TempMut(_) => V::TempMut,
            K::ContextMut(_) => V::ContextMut,
        }
    }
}

pub trait Object<'ctx, T: 'static> {
    fn visit(&mut self, value: Kind<'_, 'ctx, T>) -> Result<(), ()>;
}

impl<T: 'static> Protocol for Value<T> {
    type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx, T>;
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum KindVariants {
    Temp,
    Context,
    TempMut,
    ContextMut,
}

impl KindVariants {
    pub fn can_become(&self, wanted: Self) -> bool {
        use KindVariants as V;
        match (self, wanted) {
            (V::Temp, V::Temp) => true,
            (V::Temp, V::Context) => false,
            (V::Temp, V::TempMut) => false,
            (V::Temp, V::ContextMut) => false,
            (V::Context, V::Temp) => true,
            (V::Context, V::Context) => true,
            (V::Context, V::TempMut) => false,
            (V::Context, V::ContextMut) => false,
            (V::TempMut, V::Temp) => true,
            (V::TempMut, V::Context) => false,
            (V::TempMut, V::TempMut) => true,
            (V::TempMut, V::ContextMut) => false,
            (V::ContextMut, V::Temp) => true,
            (V::ContextMut, V::Context) => true,
            (V::ContextMut, V::TempMut) => true,
            (V::ContextMut, V::ContextMut) => true,
        }
    }
}

pub struct Known {
    pub kind: Option<KindVariants>,
}

pub struct Hint {
    pub kind: Option<KindVariants>,
}

impl<T: 'static> Meta for Value<T> {
    type Known<'a, 'ctx: 'a> = Known;

    type Hint<'a, 'ctx: 'a> = Hint;
}