[][src]Struct smart_access::CpsBatch

#[must_use]pub struct CpsBatch<CPS, L> { /* fields omitted */ }

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-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.

If needed, the concrete type of the compile-time batch can be specified by means of the path 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.

Implementations

impl<CPS> CpsBatch<CPS, ()> where
    CPS: Cps
[src]

An empty compile-time batch.

pub fn run(self) -> Option<()>[src]

Runs an empty compile-time batch.

Immediately returns None.

pub fn add<F, R>(self, f: F) -> CpsBatch<CPS, ((), F)> where
    F: FnOnce(&mut CPS::View, ()) -> R, 
[src]

Adds a new function to an empty compile-time batch.

impl<CPS, Prev, F, R> CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev, F): RunBatch<CPS::View, Output = R>, 
[src]

A nonempty compile-time batch.

pub fn run(self) -> Option<R>[src]

Runs 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, 
[src]

Adds a new function to a nonempty compile-time batch.

pub fn pop(self, dst: Option<&mut Option<F>>) -> CpsBatch<CPS, Prev>[src]

Takes the last function from a nonempty compile-time batch.

You can use it as follows:

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 clear(self) -> CpsBatch<CPS, ()>[src]

Clears a nonempty compile-time batch.

impl<CPS: Cps, R> CpsBatch<CPS, Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>[src]

A runtime batch.

Has two interfaces:

  • a direct access to the underlying vector: the .edit() method
  • a compile-time batch compatibility layer

pub fn run(self) -> Option<R>[src]

Runs an empty runtime batch.

Immediately returns None if the batch is empty.

pub fn add<F>(self, f: F) -> Self where
    F: FnOnce(&mut CPS::View, Option<R>) -> R + 'static, 
[src]

Adds a new function to a runtime batch.

pub fn pop(
    self,
    dst: Option<&mut Option<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>
) -> Self
[src]

Takes the last function from a runtime batch.

pub fn clear(self) -> Self[src]

Clears a runtime batch.

pub fn edit(
    &mut self
) -> &mut Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>
[src]

A direct access to the underlying vector.

Trait Implementations

impl<CPS: Cps> Batch<()> for CpsBatch<CPS, ()>[src]

impl<CPS: Cps, Prev, F, R> Batch<R> for CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev, F): RunBatch<CPS::View, Output = R>, 
[src]

impl<CPS: Cps, R> Batch<R> for CpsBatch<CPS, Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>[src]

impl<CPS: Cps> BatchCt<<CPS as Cps>::View, ()> for CpsBatch<CPS, ()>[src]

type CPS = CPS

type List = ()

impl<CPS, Prev, F, R> BatchCt<<CPS as Cps>::View, R> for CpsBatch<CPS, (Prev, F)> where
    CPS: Cps,
    (Prev, F): RunBatch<CPS::View, Output = R>, 
[src]

type CPS = CPS

type List = (Prev, F)

impl<CPS: Cps, R> BatchRt<<CPS as Cps>::View, R> for CpsBatch<CPS, Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>[src]

Auto Trait Implementations

impl<CPS, L> Send for CpsBatch<CPS, L> where
    CPS: Send,
    L: Send

impl<CPS, L> Sync for CpsBatch<CPS, L> where
    CPS: Sync,
    L: Sync

impl<CPS, L> Unpin for CpsBatch<CPS, L> where
    CPS: Unpin,
    L: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.