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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
pub mod hint {
}

pub mod recoverable {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    /// Protocol for providing a recoverable walker.
    ///
    /// This protocol is implemented by visitors.
    pub enum Recoverable {}

    /// Implemented by walkers that can be reset.
    pub trait RecoverableWalker<'ctx> {
        /// Start a new walk with the walker reset to it's starting state.
        fn new_walk(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    /// Object implementing the [`Recoverable`] protocol.
    pub trait Object<'ctx> {
        /// Visit with a recoverable walker.
        ///
        /// The visitor can then use the `walker` to try multiple different ways
        /// of walking.
        fn visit(&mut self, walker: &mut dyn RecoverableWalker<'ctx>) -> Result<(), ()>;
    }

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

    impl Meta for Recoverable {
        type Known<'a, 'ctx: 'a> = ();

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

pub mod owned {
    use core::marker::PhantomData;

    use crate::{protocol::Protocol, walk::protocols::hint::Meta};

    pub struct Owned<T: 'static>(PhantomData<fn() -> T>);

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

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

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

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

pub mod borrowed {
}

pub mod borrowed_mut {
    use core::marker::PhantomData;

    use crate::{protocol::Protocol, walk::protocols::hint::Meta};

    pub struct BorrowedMut<T: 'static>(PhantomData<fn() -> T>);

    pub enum Value<'a, 'ctx, T: 'static> {
        Temp(&'a mut T),
        Context(&'ctx mut T),
        Static(&'static mut T),
    }

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

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

    pub enum Kind {
        Temp,
        Context,
        Static,
    }

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

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

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

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

pub mod tagged {
}

pub mod sequence {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    pub enum Sequence {}

    #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
    pub enum Status {
        Done,
        Continue,
    }

    pub trait SequenceWalker<'ctx> {
        fn next(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<Status, ()>;
    }

    pub trait Object<'ctx> {
        fn visit(&mut self, walker: &mut dyn SequenceWalker<'ctx>) -> Result<(), ()>;
    }

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

    #[derive(Default)]
    pub struct Known {
        pub len: (usize, Option<usize>),
    }

    pub struct Hint {
        pub len: (usize, Option<usize>),
    }

    impl Meta for Sequence {
        type Known<'a, 'ctx: 'a> = Known;

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

pub mod map {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    pub enum Map {}

    #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
    pub enum Status {
        Done,
        Continue,
    }

    pub trait MapWalker<'ctx> {
        fn next_key(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<Status, ()>;

        fn value(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    pub trait Object<'ctx> {
        fn visit(&mut self, walker: &mut dyn MapWalker<'ctx>) -> Result<(), ()>;
    }

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

    #[derive(Default)]
    pub struct Known {
        pub len: (usize, Option<usize>),
    }

    pub struct Hint {
        pub len: (usize, Option<usize>),
    }

    impl Meta for Map {
        type Known<'a, 'ctx: 'a> = Known;

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