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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::any::TypeId;

use effectful::SendSync;
use treaty::{
    any::{TypeName, TypeNameId},
    protocol::walker::hint::{self, HintMeta, HintProto},
};

use crate::common::Blocking;

mod common;

// /// This tests for the hint protocol being able to give the known info and being able to hint.
// #[test]
// fn can_get_known_and_hint() {
//     // The protocol we will hint for.
//     struct MyProtocol;
//
//     // The known value for the protocol.
//     #[derive(Debug, PartialEq)]
//     struct Known(f32);
//
//     // Link the higher ranked type with the known type.
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for Known = Known;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Known = Known;
//         }
//     }
//
//     #[derive(Debug, PartialEq)]
//     struct Hint(i32);
//
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for Hint = Hint;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Hint = Hint;
//         }
//     }
//
//     // Enroll the protocol in the hint system.
//     impl HintMeta for MyProtocol {
//         type Known = Known;
//         type Hint = Hint;
//         type Effect = Blocking;
//     }
//
//     let mut mock = MockHintWalker::<MyProtocol>::new();
//
//     mock.expect_known()
//         .once()
//         .return_const((|_, Hint(hint)| Ok(Known(*hint as f32))) as KnownFactory<MyProtocol>);
//
//     mock.expect_hint()
//         .once()
//         .withf(|_visitor, Hint(hint)| *hint == 123)
//         .return_const(Flow::Done);
//
//     // Get the mock as a hint protocol trait object.
//     let walker: &mut dyn hint::Hint<MyProtocol> = &mut mock;
//
//     // We can call known to get what the walker knows about the protocol.
//     assert_eq!(walker.known(&Hint(42)).value(), Ok(Known(42.0)));
//
//     {
//         // A hint needs the visitor for the walker to walk.
//         let mut mock = MockBuilder::<(), (), ()>::new();
//
//         // We can call hint to "commit" to the protocol and ask the walker to use it.
//         assert_eq!(
//             walker.hint(DynVisitor(&mut mock), Hint(123)).value(),
//             Flow::Done
//         );
//     }
// }

// #[test]
// fn known_can_have_temp_mutable_borrow() {
//     struct MyProtocol;
//
//     struct KnownHrt;
//
//     #[derive(Debug, PartialEq)]
//     struct Known<'a>(&'a mut String);
//
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for KnownHrt = Known<'a>;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Known<'a> = KnownHrt;
//         }
//     }
//
//     impl HintMeta for MyProtocol {
//         type Known = KnownHrt;
//         type Hint = ();
//         type Effect = Blocking;
//     }
//
//     struct Walker<'ctx>(&'ctx mut String);
//
//     impl<'ctx> Hint<'ctx, MyProtocol> for Walker<'ctx> {
//         fn hint<'this, 'visitor, 'hint, 'e>(
//             &'this mut self,
//             visitor: DynVisitor<'visitor, 'ctx>,
//             hint: MetaHint<'hint, 'ctx, MyProtocol>,
//         ) -> ErasedEffective<'e, Flow, Blocking>
//         where
//             'ctx: 'this + 'visitor + 'hint + 'e,
//         {
//             unreachable!()
//         }
//
//         fn known<'a>(
//             &'a mut self,
//             (): &'a <MyProtocol as HintMeta>::Hint,
//         ) -> ErasedEffective<'a, Result<MetaKnown<'a, 'ctx, MyProtocol>, ()>, Blocking> {
//             self.0.push_str("test");
//
//             Blocking::<Spin>::ready(Ok(Known(self.0)))
//         }
//     }
//
//     let mut context = String::new();
//
//     {
//         let mut walker = Walker(&mut context);
//
//         // Get the mock as a hint protocol trait object.
//         let walker: &mut dyn Hint<MyProtocol> = &mut walker;
//
//         // We can call known to get what the walker knows about the protocol.
//         let mut x = String::from("test");
//         assert_eq!(walker.known(&()).value(), Ok(Known(&mut x)));
//     }
//
//     drop(context);
// }

// #[test]
// fn known_can_have_context_borrow() {
//     struct MyProtocol;
//
//     struct KnownHrt;
//
//     #[derive(Debug, PartialEq)]
//     struct Known<'ctx>(&'ctx String);
//
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for KnownHrt = Known<'ctx>;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Known<'ctx> = KnownHrt;
//         }
//     }
//
//     impl HintMeta for MyProtocol {
//         type Known = KnownHrt;
//         type Hint = ();
//         type Effect = Blocking;
//     }
//
//     struct Walker<'ctx>(&'ctx String);
//
//     impl<'ctx> Hint<'ctx, MyProtocol> for Walker<'ctx> {
//         fn hint<'this, 'visitor, 'hint, 'e>(
//             &'this mut self,
//             visitor: DynVisitor<'visitor, 'ctx>,
//             hint: MetaHint<'hint, 'ctx, MyProtocol>,
//         ) -> ErasedEffective<'e, Flow, Blocking>
//         where
//             'ctx: 'this + 'visitor + 'hint + 'e,
//         {
//             unreachable!()
//         }
//
//         fn known<'a>(
//             &'a mut self,
//             (): &'a <MyProtocol as HintMeta>::Hint,
//         ) -> ErasedEffective<'a, Result<MetaKnown<'a, 'ctx, MyProtocol>, ()>, Blocking> {
//             Blocking::<Spin>::ready(Ok(Known(self.0)))
//         }
//     }
//
//     let context = String::from("test");
//
//     let ctx = {
//         let mut walker = Walker(&context);
//
//         // Get the mock as a hint protocol trait object.
//         let walker: &mut dyn Hint<MyProtocol> = &mut walker;
//
//         // We can call known to get what the walker knows about the protocol.
//         let Ok(Known(y)) = walker.known(&()).value() else {
//             unreachable!()
//         };
//         y
//     };
//
//     assert_eq!(ctx, "test");
//
//     drop(context);
// }
//
// #[test]
// fn hint_can_have_temp_mutable_borrow() {
//     struct MyProtocol;
//
//     struct HintHrt;
//
//     #[derive(Debug, PartialEq)]
//     struct Hint<'a>(&'a mut String);
//
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for HintHrt = Hint<'a>;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Hint<'a> = HintHrt;
//         }
//     }
//
//     impl HintMeta for MyProtocol {
//         type Known = ();
//         type Hint = HintHrt;
//         type Effect = Blocking;
//     }
//
//     let mut mock = MockHintWalker::new();
//
//     mock.expect_hint().once().returning(|_, hint: Hint| {
//         hint.0.push_str("test");
//
//         Flow::Done
//     });
//
//     {
//         // Get the mock as a hint protocol trait object.
//         let walker: &mut dyn hint::Hint<MyProtocol> = &mut mock;
//
//         let mut visitor = MockBuilder::<(), (), ()>::new();
//
//         let mut temp = String::new();
//
//         // We can call known to get what the walker knows about the protocol.
//         assert_eq!(
//             walker
//                 .hint(DynVisitor(&mut visitor), Hint(&mut temp))
//                 .value(),
//             Flow::Done
//         );
//
//         assert_eq!(temp, "test");
//     }
// }
//
// #[test]
// fn hint_can_have_context_borrow() {
//     struct MyProtocol;
//
//     struct HintHrt;
//
//     #[derive(Debug, PartialEq)]
//     struct Hint<'ctx>(&'ctx String);
//
//     higher_ranked_type! {
//         impl Meta {
//             impl['a, 'ctx] type T['a, 'ctx] for HintHrt = Hint<'ctx>;
//             impl['a, 'ctx] type HigherRanked['a, 'ctx] for Hint<'ctx> = HintHrt;
//         }
//     }
//
//     impl HintMeta for MyProtocol {
//         type Known = ();
//         type Hint = HintHrt;
//         type Effect = Blocking;
//     }
//
//     let mut mock = MockHintWalker::new();
//
//     mock.expect_hint().once().returning(|_, hint: Hint| {
//         assert_eq!(hint.0, "test");
//
//         Flow::Done
//     });
//
//     let context = String::from("test");
//
//     {
//         // Get the mock as a hint protocol trait object.
//         let walker: &mut dyn hint::Hint<MyProtocol> = &mut mock;
//
//         let mut visitor = MockBuilder::<(), (), ()>::new();
//
//         // We can call known to get what the walker knows about the protocol.
//         assert_eq!(
//             walker
//                 .hint(DynVisitor(&mut visitor), Hint(&context))
//                 .value(),
//             Flow::Done
//         );
//     }
//
//     drop(context);
// }

#[test]
fn hint_proto() {
    #[derive(SendSync)]
    struct MyProtocol;

    impl<'a, 'ctx> TypeName::MemberTypeForLt<'a, 'ctx, Blocking, &'a &'ctx ()> for MyProtocol {
        type T = MyProtocol;
    }

    impl<'a, 'ctx> TypeName::LowerTypeWithBound<'a, 'ctx, Blocking, &'a &'ctx ()> for MyProtocol {
        type Higher = MyProtocol;
    }

    impl HintMeta for MyProtocol {
        type Known = ();
        type Hint = ();
        type Effect = Blocking;
    }

    // The type id of the higher ranked type.
    let id = TypeId::of::<HintProto<MyProtocol>>();

    // The type id for the lifetime containing value protocol trait object.
    let name_id = TypeNameId::of_lower::<dyn hint::Hint<MyProtocol>, Blocking>();

    // They should be the same.
    assert_eq!(id, name_id.into_type_id());
}