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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::build::protocols;
use crate::protocol::Implementer;
use crate::protocol::{ImplementerExt, ProtocolExt};
use crate::{implementer, walk, Walker};

use super::MissingProtocol;

pub struct OwnedCloneWalker<T> {
    value: Option<T>,
    error: Option<MissingProtocol>,
    hint_given: bool,
}

impl<T> OwnedCloneWalker<T> {
    pub const fn new(value: T) -> Self {
        Self {
            value: Some(value),
            error: None,
            hint_given: false,
        }
    }
}

impl<T> From<T> for OwnedCloneWalker<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl<'ctx, T: 'static + Clone> Walker<'ctx> for OwnedCloneWalker<T> {
    type Error = MissingProtocol;

    type Value = ();

    fn walk(mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<Self::Value, Self::Error> {
        // Request the visitor give a hint.
        // This allows the use of the recoverable protocol.
        if let Some(interface) = visitor.interface_for::<protocols::hint::RequestHint>() {
            match interface.as_object().request_hint(&mut self) {
                Ok(()) => {}
                Err(()) => {
                    return match self.error {
                        Some(err) => Err(err),
                        None => Ok(()),
                    };
                }
            }
        }

        // If no hint was given then just use the owned protocol.
        if !self.hint_given {
            let _ = walk::protocols::hint::Object::<protocols::owned::Owned<T>>::hint(
                &mut self,
                visitor,
                (),
            );
        }

        // Return an error if there was one.
        match self.error {
            Some(err) => Err(err),
            None => Ok(()),
        }
    }
}

implementer! {
    impl['ctx, T: 'static + Clone] OwnedCloneWalker<T> = [
        walk::protocols::hint::Hint<protocols::owned::Owned<T>>,
        walk::protocols::hint::Hint<protocols::recoverable::Recoverable>,
        walk::protocols::hint::Hint<protocols::borrowed::Borrowed<T>>,
        walk::protocols::hint::Hint<protocols::borrowed_mut::BorrowedMut<T>>,
    ];
}

impl<'ctx, T: 'static + Clone> walk::protocols::hint::Object<'ctx, protocols::owned::Owned<T>>
    for OwnedCloneWalker<T>
{
    fn hint(&mut self, visitor: &mut dyn Implementer<'ctx>, _hint: ()) -> Result<(), ()> {
        self.hint_given = true;

        if let Some(interface) = visitor.interface_for::<protocols::owned::Owned<T>>() {
            // If this happens it means the value was already visited and consumed.
            let Some(value) = self.value.take() else {
                return Ok(()); // We return Ok because the walker isn't in an error state.
            };

            // Visit the value.
            interface.as_object().visit(value)
        } else {
            self.error = Some(MissingProtocol(protocols::owned::Owned::<T>::id()));
            Err(())
        }
    }

    fn known(&mut self, _hint: &()) -> Result<(), ()> {
        Ok(())
    }
}

impl<'ctx, T: 'static + Clone> walk::protocols::hint::Object<'ctx, protocols::borrowed::Borrowed<T>>
    for OwnedCloneWalker<T>
{
    fn hint(
        &mut self,
        visitor: &mut dyn Implementer<'ctx>,
        _hint: protocols::borrowed::Hint,
    ) -> Result<(), ()> {
        self.hint_given = true;

        if let Some(interface) = visitor.interface_for::<protocols::borrowed::Borrowed<T>>() {
            // If this happens it means the value was already visited and consumed.
            let Some(value) = &self.value else {
                return Ok(()); // We return Ok because the walker isn't in an error state.
            };

            // Visit the value.
            interface.as_object().visit(protocols::borrowed::Value::Temp(value))
        } else {
            self.error = Some(MissingProtocol(protocols::owned::Owned::<T>::id()));
            Err(())
        }
    }

    fn known(
        &mut self,
        _hint: &protocols::borrowed::Hint,
    ) -> Result<protocols::borrowed::Known, ()> {
        Ok(protocols::borrowed::Known {
            kind: Some(protocols::borrowed::Kind::Temp),
        })
    }
}

impl<'ctx, T: 'static + Clone> walk::protocols::hint::Object<'ctx, protocols::borrowed_mut::BorrowedMut<T>>
    for OwnedCloneWalker<T>
{
    fn hint(
        &mut self,
        visitor: &mut dyn Implementer<'ctx>,
        _hint: protocols::borrowed_mut::Hint,
    ) -> Result<(), ()> {
        self.hint_given = true;

        if let Some(interface) = visitor.interface_for::<protocols::borrowed_mut::BorrowedMut<T>>() {
            // If this happens it means the value was already visited and consumed.
            let Some(value) = &mut self.value else {
                return Ok(()); // We return Ok because the walker isn't in an error state.
            };

            // Visit the value.
            interface.as_object().visit(protocols::borrowed_mut::Value::Temp(value))
        } else {
            self.error = Some(MissingProtocol(protocols::owned::Owned::<T>::id()));
            Err(())
        }
    }

    fn known(
        &mut self,
        _hint: &protocols::borrowed_mut::Hint,
    ) -> Result<protocols::borrowed_mut::Known, ()> {
        Ok(protocols::borrowed_mut::Known {
            kind: Some(protocols::borrowed_mut::Kind::Temp),
        })
    }
}

impl<'ctx, T: 'static + Clone>
    walk::protocols::hint::Object<'ctx, protocols::recoverable::Recoverable>
    for OwnedCloneWalker<T>
{
    fn hint(&mut self, visitor: &mut dyn Implementer<'ctx>, _hint: ()) -> Result<(), ()> {
        self.hint_given = true;

        if let Some(interface) = visitor.interface_for::<protocols::recoverable::Recoverable>() {
            // This walker is already recoverable.
            interface.as_object().visit(self)
        } else {
            self.error = Some(MissingProtocol(protocols::recoverable::Recoverable::id()));
            Err(())
        }
    }

    fn known(&mut self, _hint: &()) -> Result<(), ()> {
        Ok(())
    }
}

impl<'ctx, T: 'static + Clone> protocols::recoverable::RecoverableWalker<'ctx>
    for OwnedCloneWalker<T>
{
    fn new_walk(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()> {
        // Short circuit if the walker has an error.
        if self.error.is_some() {
            return Err(());
        }

        // Short circuit if the walker doesn't have a value.
        // If this happens it means the value was already visited and consumed.
        let Some(value) = &self.value else {
            return Ok(()); // We return Ok because the walker isn't in an error state.
        };

        // Clone the value to put into a new walker instance.
        let walker = Self::new(value.clone());

        // Walk the walker.
        match walker.walk(visitor) {
            Ok(()) => Ok(()), // We don't know if the visitor had an issue.
            Err(err) => {
                self.error = Some(err);
                Err(())
            }
        }
    }
}