[][src]Struct smart_access::CpsBatch

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

A builder for complex mutations.

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);

Runtime version

Created by method .batch_rt(). Has mutable interface. Can be combined with loops but every .add consumes some memory.

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.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.add(|v, prev| { *v = *v * prev.unwrap(); 42 });

// "Builder" style can also be used:
batch
    .add(|v, prev| { *v = -*v; prev.unwrap() } )
    .add(|v, prev| { *v = -*v; prev.unwrap() } );

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);

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, Result = 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.

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

A runtime batch.

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

Runs an empty runtime batch.

Immediately returns None if the batch is empty.

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

Adds a new function to a runtime batch.

Auto Trait Implementations

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

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

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

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.