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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// use crate::Kp;
// use async_trait::async_trait;
// use std::pin::Pin;
// /// Used so that `then_async` can infer `V2` from `AsyncKp::Value` without ambiguity
// /// (e.g. `&i32` has both `Borrow<i32>` and `Borrow<&i32>`; this picks the referent).
// /// Implemented only for reference types so there is no overlap with the blanket impl.
// pub trait KeyPathValueTarget {
// type Target: Sized;
// }
// impl<T> KeyPathValueTarget for &T {
// type Target = T;
// }
// impl<T> KeyPathValueTarget for &mut T {
// type Target = T;
// }
// pub trait KpTrait<R, V>: KpReadable<R, V> + KPWritable<R, V> {
// fn type_id_of_root() -> std::any::TypeId
// where
// R: 'static,
// {
// std::any::TypeId::of::<R>()
// }
// fn type_id_of_value() -> std::any::TypeId
// where
// V: 'static,
// {
// std::any::TypeId::of::<V>()
// }
// fn then<SV, G2, S2>(
// self,
// next: Kp<V, SV, G2, S2>,
// ) -> Kp<
// R,
// SV,
// impl for<'r> Fn(&'r R) -> Option<&'r SV>,
// impl for<'r> Fn(&'r mut R) -> Option<&'r mut SV>,
// >
// where
// G2: for<'r> Fn(&'r V) -> Option<&'r SV>,
// S2: for<'r> Fn(&'r mut V) -> Option<&'r mut SV>,
// for<'r> V: 'r;
// }
// pub trait KpReadable<R, V> {
// fn get<'a>(&self, root: &'a R) -> Option<&'a V>;
// }
// pub trait KPWritable<R, V> {
// fn set<'a>(&self, root: &'a mut R) -> Option<&'a mut V>;
// }
// pub trait AccessorTrait<R, V>: KpTrait<R, V> {
// /// Like [get](KpReadable::get), but takes an optional root.
// #[inline]
// fn get_optional<'a>(&self, root: Option<&'a R>) -> Option<&'a V> {
// root.and_then(|r| self.get(r))
// }
// /// Like [set](KPWritable::set), but takes an optional mutable root.
// #[inline]
// fn get_mut_optional<'a>(&self, root: Option<&'a mut R>) -> Option<&'a mut V> {
// root.and_then(|r| self.set(r))
// }
// /// Returns the value if the keypath succeeds, otherwise returns fallback from `f`.
// #[inline]
// fn get_or_else<'a, F>(&self, root: &'a R, f: F) -> &'a V
// where
// F: FnOnce() -> &'a V,
// {
// self.get(root).unwrap_or_else(f)
// }
// /// Returns the mutable value if the keypath succeeds, otherwise returns fallback from `f`.
// #[inline]
// fn get_mut_or_else<'a, F>(&self, root: &'a mut R, f: F) -> &'a mut V
// where
// F: FnOnce() -> &'a mut V,
// {
// self.set(root).unwrap_or_else(f)
// }
// }
// pub trait CoercionTrait<R, V>: KpTrait<R, V> {
// fn for_arc(
// &self,
// ) -> Kp<
// std::sync::Arc<R>,
// V,
// impl for<'r> Fn(&'r std::sync::Arc<R>) -> Option<&'r V> + '_,
// impl for<'r> Fn(&'r mut std::sync::Arc<R>) -> Option<&'r mut V> + '_,
// > {
// Kp::new(
// move |arc_root: &std::sync::Arc<R>| self.get(arc_root.as_ref()),
// move |arc_root: &mut std::sync::Arc<R>| {
// std::sync::Arc::get_mut(arc_root).and_then(|r_mut| self.set(r_mut))
// },
// )
// }
// fn for_box(
// &self,
// ) -> Kp<
// Box<R>,
// V,
// impl for<'r> Fn(&'r Box<R>) -> Option<&'r V> + '_,
// impl for<'r> Fn(&'r mut Box<R>) -> Option<&'r mut V> + '_,
// > {
// Kp::new(
// move |boxed_root: &Box<R>| self.get(boxed_root.as_ref()),
// move |boxed_root: &mut Box<R>| self.set(boxed_root.as_mut()),
// )
// }
// /// Convert a keypath-like object into a getter closure.
// fn into_get(self) -> impl for<'r> Fn(&'r R) -> Option<&'r V>
// where
// Self: Sized,
// {
// move |root: &R| self.get(root)
// }
// /// Convert a keypath-like object into a setter closure.
// fn into_set(self) -> impl for<'r> Fn(&'r mut R) -> Option<&'r mut V>
// where
// Self: Sized,
// {
// move |root: &mut R| self.set(root)
// }
// }
// pub trait HofTrait<R, V, G, S>: KpTrait<R, V>
// where
// G: for<'r> Fn(&'r R) -> Option<&'r V>,
// S: for<'r> Fn(&'r mut R) -> Option<&'r mut V>,
// {
// /// Maps the keypath value into an owned transformed value.
// fn map<MappedValue, F>(&self, mapper: F) -> impl for<'r> Fn(&'r R) -> Option<MappedValue> + '_
// where
// F: Fn(&V) -> MappedValue + 'static,
// {
// move |root: &R| self.get(root).map(&mapper)
// }
// /// Filters values using a predicate and returns a new keypath.
// fn filter<F>(
// &self,
// predicate: F,
// ) -> Kp<
// R,
// V,
// impl for<'r> Fn(&'r R) -> Option<&'r V> + '_,
// impl for<'r> Fn(&'r mut R) -> Option<&'r mut V> + '_,
// >
// where
// F: Fn(&V) -> bool + Clone + 'static,
// {
// let predicate_for_get = predicate.clone();
// Kp::new(
// move |root: &R| self.get(root).filter(|value| predicate_for_get(value)),
// move |root: &mut R| self.set(root).filter(|value| predicate(value)),
// )
// }
// /// Maps and flattens the keypath value when mapper returns `Option`.
// fn filter_map<MappedValue, F>(
// &self,
// mapper: F,
// ) -> impl for<'r> Fn(&'r R) -> Option<MappedValue> + '_
// where
// F: Fn(&V) -> Option<MappedValue> + 'static,
// {
// move |root: &R| self.get(root).and_then(&mapper)
// }
// /// Runs `inspector` for side effects and returns a keypath for the same value.
// fn inspect<F>(
// &self,
// inspector: F,
// ) -> Kp<
// R,
// V,
// impl for<'r> Fn(&'r R) -> Option<&'r V> + '_,
// impl for<'r> Fn(&'r mut R) -> Option<&'r mut V> + '_,
// >
// where
// F: Fn(&V) + Clone + 'static,
// {
// let inspector_for_get = inspector.clone();
// Kp::new(
// move |root: &R| {
// self.get(root).inspect(|value| {
// inspector_for_get(value);
// })
// },
// move |root: &mut R| {
// self.set(root).inspect(|value| {
// inspector(value);
// })
// },
// )
// }
// /// Flat map - maps to an iterator and flattens.
// fn flat_map<I, Item, F>(&self, mapper: F) -> impl for<'r> Fn(&'r R) -> Vec<Item> + '_
// where
// F: Fn(&V) -> I + 'static,
// I: IntoIterator<Item = Item>,
// {
// move |root: &R| {
// self.get(root)
// .map(|value| mapper(value).into_iter().collect())
// .unwrap_or_else(Vec::new)
// }
// }
// /// Fold/reduce the value using an accumulator function.
// fn fold_value<Acc, F>(&self, init: Acc, folder: F) -> impl for<'r> Fn(&'r R) -> Acc + '_
// where
// F: Fn(Acc, &V) -> Acc + 'static,
// Acc: Copy + 'static,
// {
// move |root: &R| {
// self.get(root)
// .map(|value| folder(init, value))
// .unwrap_or(init)
// }
// }
// /// Check if the value satisfies a predicate.
// fn any<F>(&self, predicate: F) -> impl for<'r> Fn(&'r R) -> bool + '_
// where
// F: Fn(&V) -> bool + 'static,
// {
// move |root: &R| self.get(root).map(&predicate).unwrap_or(false)
// }
// /// Check if the value satisfies a predicate; returns true for missing values.
// fn all<F>(&self, predicate: F) -> impl for<'r> Fn(&'r R) -> bool + '_
// where
// F: Fn(&V) -> bool + 'static,
// {
// move |root: &R| self.get(root).map(&predicate).unwrap_or(true)
// }
// /// Count elements in a collection-like value.
// fn count_items<F>(&self, counter: F) -> impl for<'r> Fn(&'r R) -> Option<usize> + '_
// where
// F: Fn(&V) -> usize + 'static,
// {
// move |root: &R| self.get(root).map(&counter)
// }
// /// Find an item in a collection-like value.
// fn find_in<Item, F>(&self, finder: F) -> impl for<'r> Fn(&'r R) -> Option<Item> + '_
// where
// F: Fn(&V) -> Option<Item> + 'static,
// {
// move |root: &R| self.get(root).and_then(&finder)
// }
// /// Take first N elements from a collection-like value.
// fn take<Output, F>(&self, n: usize, taker: F) -> impl for<'r> Fn(&'r R) -> Option<Output> + '_
// where
// F: Fn(&V, usize) -> Output + 'static,
// {
// move |root: &R| self.get(root).map(|value| taker(value, n))
// }
// /// Skip first N elements from a collection-like value.
// fn skip<Output, F>(&self, n: usize, skipper: F) -> impl for<'r> Fn(&'r R) -> Option<Output> + '_
// where
// F: Fn(&V, usize) -> Output + 'static,
// {
// move |root: &R| self.get(root).map(|value| skipper(value, n))
// }
// /// Partition a collection-like value into two groups.
// fn partition_value<Output, F>(
// &self,
// partitioner: F,
// ) -> impl for<'r> Fn(&'r R) -> Option<Output> + '_
// where
// F: Fn(&V) -> Output + 'static,
// {
// move |root: &R| self.get(root).map(&partitioner)
// }
// /// Get min value from a collection-like value.
// fn min_value<Item, F>(&self, min_fn: F) -> impl for<'r> Fn(&'r R) -> Option<Item> + '_
// where
// F: Fn(&V) -> Option<Item> + 'static,
// {
// move |root: &R| self.get(root).and_then(&min_fn)
// }
// /// Get max value from a collection-like value.
// fn max_value<Item, F>(&self, max_fn: F) -> impl for<'r> Fn(&'r R) -> Option<Item> + '_
// where
// F: Fn(&V) -> Option<Item> + 'static,
// {
// move |root: &R| self.get(root).and_then(&max_fn)
// }
// /// Sum values from a collection-like value.
// fn sum_value<Sum, F>(&self, sum_fn: F) -> impl for<'r> Fn(&'r R) -> Option<Sum> + '_
// where
// F: Fn(&V) -> Sum + 'static,
// {
// move |root: &R| self.get(root).map(&sum_fn)
// }
// }
// /// Lock adapter abstraction used by sync lock keypaths.
// pub trait LockAccess<Lock, Mid> {
// fn with_read<Rv, F>(&self, lock: &Lock, f: F) -> Option<Rv>
// where
// F: FnOnce(&Mid) -> Option<Rv>;
// fn with_write<Rv, F>(&self, lock: &Lock, f: F) -> Option<Rv>
// where
// F: FnOnce(&mut Mid) -> Option<Rv>;
// }
// /// Sync keypath abstraction used by composed async/pin keypaths.
// pub trait SyncKeyPathLike<R, V> {
// fn sync_get<'a>(&self, root: &'a R) -> Option<&'a V>;
// fn sync_get_mut<'a>(&self, root: &'a mut R) -> Option<&'a mut V>;
// }
// /// Await abstraction for `#[pin]` future keypaths.
// #[async_trait(?Send)]
// pub trait PinFutureAwaitLike<S, Output> {
// async fn get_await(&self, this: Pin<&mut S>) -> Option<Output>;
// }
// /// Async lock adapter abstraction used by async lock keypaths.
// #[async_trait(?Send)]
// pub trait AsyncLockLike<Lock, Mid>: Send + Sync {
// async fn with_read<Rv, F>(&self, lock: &Lock, f: F) -> Option<Rv>
// where
// F: FnOnce(&Mid) -> Option<Rv>;
// async fn with_write<Rv, F>(&self, lock: &Lock, f: F) -> Option<Rv>
// where
// F: FnOnce(&mut Mid) -> Option<Rv>;
// }
// /// Async keypath abstraction for async composition.
// #[async_trait(?Send)]
// pub trait AsyncKeyPathLike<R> {
// type Value;
// async fn get(&self, root: &R) -> Option<Self::Value>;
// }