[−][src]Struct smart_access::CpsBatch
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]
CPS: Cps,
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]
F: FnOnce(&mut CPS::View, ()) -> R,
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]
CPS: Cps,
(Prev, F): RunBatch<CPS::View, Output = R>,
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]
G: FnOnce(&mut CPS::View, R) -> S,
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]
F: FnOnce(&mut CPS::View, Option<R>) -> R + 'static,
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]
self,
dst: Option<&mut Option<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>
) -> Self
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]
&mut self
) -> &mut Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>
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]
CPS: Cps,
(Prev, F): RunBatch<CPS::View, Output = R>,
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 = ()
fn add<G, S>(self, g: G) -> CpsBatch<CPS, (Self::List, G)> where
G: FnOnce(&mut CPS::View, ()) -> S,
[src]
G: FnOnce(&mut CPS::View, ()) -> S,
fn clear(self) -> CpsBatch<CPS, ()>
[src]
fn run(self) -> Option<R>
[src]
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]
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,
[src]
G: FnOnce(&mut CPS::View, R) -> S,
fn clear(self) -> CpsBatch<CPS, ()>
[src]
fn run(self) -> Option<R>
[src]
impl<CPS: Cps, R> BatchRt<<CPS as Cps>::View, R> for CpsBatch<CPS, Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>
[src]
fn add<G>(self, g: G) -> Self where
G: FnOnce(&mut CPS::View, Option<R>) -> R + 'static,
[src]
G: FnOnce(&mut CPS::View, Option<R>) -> R + 'static,
fn clear(self) -> Self
[src]
fn pop(
self,
dst: Option<&mut Option<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>
) -> Self
[src]
self,
dst: Option<&mut Option<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>>
) -> Self
fn edit(&mut self) -> &mut Vec<Box<dyn FnOnce(&mut CPS::View, Option<R>) -> R>>
[src]
fn run(self) -> Option<R>
[src]
Auto Trait Implementations
impl<CPS, L> Send for CpsBatch<CPS, L> where
CPS: Send,
L: Send,
CPS: Send,
L: Send,
impl<CPS, L> Sync for CpsBatch<CPS, L> where
CPS: Sync,
L: Sync,
CPS: Sync,
L: Sync,
impl<CPS, L> Unpin for CpsBatch<CPS, L> where
CPS: Unpin,
L: Unpin,
CPS: Unpin,
L: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,