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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
mod run_batch;  // a helper for compile-time batch execution
use run_batch::RunBatch;

use crate::at::Cps;

#[cfg(feature="batch_rt")]
use alloc::vec::Vec;

#[cfg(feature="batch_rt")]
use alloc::boxed::Box;


/// A builder for complex mutations. __Requires `batch_ct` or `batch_rt`.__
///
/// Comes in two flavors.
///
/// ## Compile-time version
///
/// Created by method `.batch_ct()` of any [`Cps`](trait.Cps.html)-bounded value.
///
/// Efficient but can't be combined with loops (and is difficult to use in 
/// presence of conditional branches).
///
/// ### Example
///
/// ```
/// use smart_access::Cps;
///
/// let mut foo = 0;
///
/// // here we use a mutable reference as a Cps-bounded value
/// let batch = (&mut foo).batch_ct();
/// 
/// // compile-time batches are immutable because adding a new mutator changes type of the batch
/// let batch = batch
///     .add(|v, _| { *v = *v + 2; 42 })
///     .add(|v, x| { *v = *v * x; "Hello!" });
///
/// let result = batch.run();
/// 
/// assert!(result == Some("Hello!"));
/// assert!(foo == (0 + 2) * 42);
/// ```
///
/// Compile-time batches are abstracted by the trait [`BatchCt`](trait.BatchCt.html).
///
/// If needed, the concrete type of the compile-time batch can be specified 
/// by means of the [`path`](macro.path.html) macro or fully explicitly: 
/// `CpsBatch<CPS, (..(((), F1), F2) .. Fn)>`.
///
///
/// ## Runtime version
///
/// Created by method `.batch_rt()`. Can be combined with loops but every `.add` consumes some memory.
///
/// _Almost_ compatible with compile-time version but with some quirks.
///
/// ### Example
///
/// ```
/// use smart_access::Cps;
///
/// let mut foo = 0;
///
/// let mut batch = (&mut foo).batch_rt();
///
/// for i in 1..=10 {
///     // "move" is required if the closure uses any local variables
///     batch = batch.add(move |v, _| { *v = *v + i; i });
/// }
///
/// // Previous result can be used but it is wrapped in Option. 
/// // This Option is None only in the first mutator in a batch, 
/// // i.e. when there is no previous value.
/// batch = batch.add(|v, prev| { *v = *v * prev.unwrap(); 42 });
///
/// // "Builder" style can also be used:
/// batch = batch
///     .add(|v, prev| { *v = -*v; prev.unwrap() } )
///     .add(|v, prev| { *v = -*v; prev.unwrap() } );
///
/// // Runtime batches give a direct access to the vector of actions:
/// batch.edit().access(|vec| {
///     let f = vec.pop().unwrap();
///     vec.push(f);
/// });
///
/// let result = batch.run();
///
/// // Unlike compile-time batches all intermediate results must be of the same type.
/// assert!(result == Some(42)); 
/// assert!(foo == (1..=10).sum::<i32>() * 10);
/// ```
///
/// Runtime batches are abstracted by the trait [`BatchRt`](trait.BatchRt.html).
#[must_use]
pub struct CpsBatch<CPS, L> {
    cps: CPS,
    list: L,
}

#[cfg(feature="batch_rt")]
pub type FnBoxRt<V, R> = Box<dyn FnOnce(&mut V, Option<R>) -> R>;


/// An _empty_ compile-time batch.
#[cfg(feature="batch_ct")]
impl<CPS> CpsBatch<CPS, ()> where
    CPS: Cps
{
    /// Runs an _empty_ compile-time batch. 
    ///
    /// Immediately returns `None`.
    pub fn run(self) -> Option<()> { None }

    /// Adds a new function to an _empty_ compile-time batch.
    pub fn add<F, R>(self, f: F) -> CpsBatch<CPS, ((), F)>
        where F: FnOnce(&mut CPS::View, ()) -> R
    {
        CpsBatch { cps: self.cps, list: (self.list, f) }
    }
}

/// A _nonempty_ compile-time batch.
#[cfg(feature="batch_ct")]
impl<CPS,Prev,F,R> CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev,F): RunBatch<CPS::View, Output=R>,
{
    /// Runs a _nonempty_ compile-time batch.
    pub fn run(self) -> Option<R> {
        let list = self.list;

        self.cps.access(|v| list.run(v))
    }
    
    /// Adds a new function to a _nonempty_ compile-time batch.
    pub fn add<G, S>(self, g: G) -> CpsBatch<CPS, ((Prev, F), G)>
        where G: FnOnce(&mut CPS::View, R) -> S
    {
        CpsBatch { cps: self.cps, list: (self.list, g) }
    }

    /// Takes the last function from a _nonempty_ compile-time batch.
    ///
    /// You can use it as follows:
    ///
    /// ```
    /// # use smart_access::Cps;
    /// let mut foo = 0;
    /// let mut maybe_f = None;
    /// foo.batch_ct()
    ///     .add(|x, _| { *x += 1; })
    ///     .add(|x, _| { *x += 1; })
    ///     .pop(Some(&mut maybe_f))
    ///     .run();
    ///
    /// assert!(foo == 1);
    /// 
    /// maybe_f.unwrap()(&mut foo, ());
    /// assert!(foo == 2);
    /// ```
    pub fn pop(self, dst: Option<&mut Option<F>>) -> CpsBatch<CPS, Prev> {
        let (prev, f) = self.list;
        
        if let Some(place) = dst { *place = Some(f); }
        
        CpsBatch { cps: self.cps, list: prev }
    }

    /// Clears a _nonempty_ compile-time batch.
    pub fn clear(self) -> CpsBatch<CPS, ()> {
        CpsBatch { cps: self.cps, list: () }
    }
}


#[cfg(feature="batch_ct")]#[test]
fn test_ct_batch_editing() {
    use crate::Cps;
    let mut foo = 1;

    foo.batch_ct()
        .add(|x, _| { *x += 1; })
        .add(|x, _| { *x += 1; })
        .pop(None)
        .run();

    assert!(foo == 2);
    
    foo.batch_ct()
        .add(|x, _| { *x += 1; })
        .add(|x, _| { *x += 1; })
        .clear()
        .run();

    assert!(foo == 2);
}



/// A runtime batch.
///
/// Has two interfaces:
/// * a direct access to the underlying vector: the `.edit()` method
/// * a compile-time batch compatibility layer
#[cfg(feature="batch_rt")]
impl<CPS: Cps, R> CpsBatch<CPS, Vec<FnBoxRt<CPS::View, R>>> {
    /// Runs an empty runtime batch. 
    ///
    /// Immediately returns `None` if the batch is empty.
    pub fn run(self) -> Option<R> {
        let list = self.list;

        if list.len() == 0 { return None; }

        self.cps.access(|v| list.run(v)).map(|x| x.unwrap())
    }
    
    /// Adds a new function to a runtime batch.
    pub fn add<F>(mut self, f: F) -> Self where 
        F: FnOnce(&mut CPS::View, Option<R>) -> R + 'static
    {
        self.list.push(Box::new(f));

        self
    }

    /// Takes the last function from a runtime batch.
    pub fn pop(mut self, dst: Option<&mut Option<FnBoxRt<CPS::View, R>>>) -> Self
    {
        let maybe_f = self.list.pop();

        if let Some(place) = dst { *place = maybe_f; }

        self
    }

    /// Clears a runtime batch.
    pub fn clear(mut self) -> Self {
        self.list.clear();

        self
    }

    /// A direct access to the underlying vector.
    pub fn edit(&mut self) -> &mut Vec<FnBoxRt<CPS::View, R>> {
        &mut self.list
    }
}


#[cfg(feature="batch_rt")]#[test]
fn test_rt_batch_editing() {
    use crate::Cps;
    let mut foo = 1;

    foo.batch_rt()
        .add(|x, _| { *x += 1; })
        .add(|x, _| { *x += 1; })
        .pop(None)
        .run();

    assert!(foo == 2);
    
    foo.batch_rt()
        .add(|x, _| { *x += 1; })
        .add(|x, _| { *x += 1; })
        .clear()
        .run();

    assert!(foo == 2);
    
    let mut maybe_f = None;
    foo.batch_rt()
        .add(|x, _| { *x += 1; })
        .add(|x, _| { *x += 1; })
        .pop(Some(&mut maybe_f))
        .run();
    
    assert!(foo == 3);
    
    maybe_f.unwrap()(&mut foo, None);
    assert!(foo == 4);
}



// Helpers for the Cps trait.
#[cfg(feature="batch_ct")]
pub fn new_batch_ct<CPS: Cps>(cps: CPS) -> CpsBatch<CPS, ()> {
    CpsBatch { cps: cps, list: () }
}

#[cfg(feature="batch_rt")]
pub fn new_batch_rt<CPS, V, R>(cps: CPS) -> CpsBatch<CPS, Vec<FnBoxRt<V,R>>> where 
    CPS: Cps<View=V>,
    V: ?Sized
{
    CpsBatch { cps: cps, list: Vec::new() }
}


/// An abstraction over [compile-time and runtime batches](struct.CpsBatch.html). 
/// __Requires `batch_ct` or `batch_rt`.__
///
/// The only thing which can be done with a value of `Batch`-bounded 
/// type is to [`.run()`](trait.Batch.html#tymethod.run) it.
///
/// Useful as a bound on a function return type.
///
/// If the batch returned by a function is to be edited later 
/// then consider using more precise bounds:
/// [`BatchCt`](trait.BatchCt.html) and [`BatchRt`](trait.BatchRt.html).
#[must_use]
pub trait Batch<R>: Sized {
    fn run(self) -> Option<R>;
}


#[cfg(feature="batch_ct")]
impl<CPS: Cps> Batch<()> for CpsBatch<CPS, ()> {
    fn run(self) -> Option<()> {
        self.run()
    }
}


#[cfg(feature="batch_ct")]
impl<CPS: Cps, Prev, F, R> Batch<R> for CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev,F): RunBatch<CPS::View, Output=R>
{
    fn run(self) -> Option<R> {
        self.run()
    }
}


#[cfg(feature="batch_rt")]
impl<CPS: Cps, R> Batch<R> for CpsBatch<CPS, Vec<FnBoxRt<CPS::View, R>>> {
    fn run(self) -> Option<R> {
        self.run()
    }
}


/// A compile-time batch. __Requires `batch_ct` feature.__
///
/// See basic usage guide [here](struct.CpsBatch.html).
///
/// Allows one to write batch transformers without specifying complex
/// return types.
///
/// ```
/// use smart_access::{ BatchCt, Cps };
///
/// fn add_inc<T>(batch: impl BatchCt<i32, T>) -> impl BatchCt<i32, ()>
/// {
///     batch.add(|x, _| { *x = *x + 1; })
/// }
///
/// let mut foo = 1;
///
/// add_inc(add_inc(foo.batch_ct())).run();
/// assert!(foo == 3);
/// ```
#[cfg(feature="batch_ct")]#[must_use]
pub trait BatchCt<V: ?Sized, R>: Batch<R> {
    type CPS: Cps<View=V>;
    type List: RunBatch<V, Output=R>;

    /// Adds a new function to a compile-time batch.
    fn add<G, S>(self, g: G) -> CpsBatch<Self::CPS, (Self::List, G)> where 
        G: FnOnce(&mut V, R) -> S;

    /// Clears a compile-time batch.
    fn clear(self) -> CpsBatch<Self::CPS, ()>;

    /// [Runs](trait.Batch.html#tymethod.run) a compile-time batch.
    fn run(self) -> Option<R> {
        <Self as Batch<R>>::run(self)
    }
}


#[cfg(feature="batch_ct")]
impl<CPS: Cps> BatchCt<CPS::View, ()> for CpsBatch<CPS, ()> {
    type CPS = CPS;
    type List = ();
    
    fn add<G, S>(self, g: G) -> CpsBatch<CPS, (Self::List, G)> where 
        G: FnOnce(&mut CPS::View, ()) -> S
    {
        self.add(g)
    }

    fn clear(self) -> CpsBatch<CPS, ()> {
        self
    }
}


#[cfg(feature="batch_ct")]
impl<CPS, Prev, F, R> BatchCt<CPS::View, R> for CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev,F): RunBatch<CPS::View, Output=R>
{
    type CPS = CPS;
    type List = (Prev, F);
    
    fn add<G, S>(self, g: G) -> CpsBatch<CPS, (Self::List, G)> where 
        G: FnOnce(&mut CPS::View, R) -> S
    {
        self.add(g)
    }

    fn clear(self) -> CpsBatch<CPS, ()> {
        self.clear()
    }
}


/// A runtime batch. __Requires `batch_rt` feature.__
///
/// See basic usage guide [here](struct.CpsBatch.html).
///
/// Allows one to write batch transformers without specifying complex
/// return types.
///
/// ```
/// use smart_access::{ BatchRt, Cps };
///
/// fn add_inc(batch: impl BatchRt<i32, ()>) -> impl BatchRt<i32, ()>
/// {
///     batch.add(|x, _| { *x = *x + 1; })
/// }
///
/// let mut foo = 1;
///
/// add_inc(add_inc(foo.batch_rt())).run();
/// assert!(foo == 3);
/// ```
#[cfg(feature="batch_rt")]#[must_use]
pub trait BatchRt<View: ?Sized, R>: Batch<R> {
    /// Adds a new function to a runtime batch.
    fn add<G>(self, g: G) -> Self
        where G: FnOnce(&mut View, Option<R>) -> R + 'static;

    /// Clears a runtime batch.
    fn clear(self) -> Self;
    
    /// Takes the last function from a runtime batch.
    fn pop(self, dst: Option<&mut Option<FnBoxRt<View, R>>>) -> Self;

    /// A direct access to the underlying vector.
    fn edit(&mut self) -> &mut Vec<FnBoxRt<View, R>>;
    
    /// [Runs](trait.Batch.html#tymethod.run) a runtime batch.
    fn run(self) -> Option<R> {
        <Self as Batch<R>>::run(self)
    }
}


#[cfg(feature="batch_rt")]
impl<CPS: Cps, R> BatchRt<CPS::View, R> for 
    CpsBatch<CPS, Vec<FnBoxRt<CPS::View, R>>> 
{
    fn add<G>(self, g: G) -> Self
        where G: FnOnce(&mut CPS::View, Option<R>) -> R + 'static 
    {
        self.add(g)
    }

    fn clear(self) -> Self {
        self.clear()
    }
    
    fn pop(self, dst: Option<&mut Option<FnBoxRt<CPS::View, R>>>) -> Self {
        self.pop(dst)
    }

    fn edit(&mut self) -> &mut Vec<FnBoxRt<CPS::View, R>> {
        self.edit()
    }

}