Computation

Struct Computation 

Source
pub struct Computation<CONTEXT, STATE, OUTPUT, STEP: ComputationStep<CONTEXT, STATE, OUTPUT>> { /* private fields */ }
Expand description

A stateful computation that can be suspended and resumed.

Computation is the default implementation of Algorithm. It delegates the actual computation logic to a ComputationStep implementation while handling the boilerplate of state management and cancellation checking.

§Type Parameters

  • CONTEXT: Immutable configuration passed to each step
  • STATE: Mutable state that persists across steps
  • OUTPUT: The final result type
  • STEP: The ComputationStep implementation that defines the computation logic

§Example

use computation_process::{Computation, ComputationStep, Completable, Incomplete, Computable, Stateful};

struct SumStep;

impl ComputationStep<Vec<i32>, usize, i32> for SumStep {
    fn step(numbers: &Vec<i32>, index: &mut usize) -> Completable<i32> {
        if *index < numbers.len() {
            *index += 1;
            Err(Incomplete::Suspended) // Suspend after processing each number
        } else {
            Ok(numbers.iter().sum())
        }
    }
}

let mut computation = Computation::<Vec<i32>, usize, i32, SumStep>::from_parts(
    vec![1, 2, 3, 4, 5],
    0,
);
assert_eq!(computation.compute().unwrap(), 15);

Trait Implementations§

Source§

impl<CONTEXT, STATE, OUTPUT, STEP: ComputationStep<CONTEXT, STATE, OUTPUT>> Algorithm<CONTEXT, STATE, OUTPUT> for Computation<CONTEXT, STATE, OUTPUT, STEP>

Source§

fn run<I1: Into<CONTEXT>, I2: Into<STATE>>( context: I1, initial_state: I2, ) -> Cancellable<OUTPUT>
where Self: Sized + 'static,

Configure and immediately execute the computation, skipping over all suspended states.
Source§

fn dyn_algorithm(self) -> DynAlgorithm<CONTEXT, STATE, OUTPUT>
where Self: Sized + 'static,

Convert to a dynamic Algorithm variant.
Source§

impl<CONTEXT, STATE, OUTPUT, STEP: ComputationStep<CONTEXT, STATE, OUTPUT>> Computable<OUTPUT> for Computation<CONTEXT, STATE, OUTPUT, STEP>

Source§

fn try_compute(&mut self) -> Completable<OUTPUT>

Try to advance this computation, returning a value once the computation is done.
Source§

fn compute_completable(&mut self) -> Completable<T>

Advance this computation until it either completes, is canceled, or becomes exhausted, skipping over all suspended states. Read more
Source§

fn compute(&mut self) -> Cancellable<T>

Advance this computation until completion, skipping over all suspended states. Read more
Source§

fn dyn_computable(self) -> DynComputable<T>
where Self: Sized + 'static,

Utility method to convert this Computable to a dynamic type.
Source§

impl<CONTEXT: Debug, STATE: Debug, OUTPUT: Debug, STEP: Debug + ComputationStep<CONTEXT, STATE, OUTPUT>> Debug for Computation<CONTEXT, STATE, OUTPUT, STEP>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<CONTEXT, STATE, OUTPUT, STEP: ComputationStep<CONTEXT, STATE, OUTPUT>> Stateful<CONTEXT, STATE> for Computation<CONTEXT, STATE, OUTPUT, STEP>

Source§

fn from_parts(context: CONTEXT, state: STATE) -> Self
where Self: Sized + 'static,

Create new Stateful instance from CONTEXT and STATE.
Source§

fn into_parts(self) -> (CONTEXT, STATE)

Destruct the Stateful instance into CONTEXT and STATE objects.
Source§

fn context(&self) -> &CONTEXT

Access to the underlying immutable CONTEXT.
Source§

fn state(&self) -> &STATE

Access to the underlying STATE.
Source§

fn state_mut(&mut self) -> &mut STATE

Access to the underlying STATE as a mutable reference. Read more
Source§

fn configure<I1: Into<CONTEXT>, I2: Into<STATE>>( context: I1, initial_state: I2, ) -> Self
where Self: Sized + 'static,

Create new Stateful instance using values that can be converted to CONTEXT and STATE.

Auto Trait Implementations§

§

impl<CONTEXT, STATE, OUTPUT, STEP> Freeze for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: Freeze, STATE: Freeze,

§

impl<CONTEXT, STATE, OUTPUT, STEP> RefUnwindSafe for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: RefUnwindSafe, STATE: RefUnwindSafe, OUTPUT: RefUnwindSafe, STEP: RefUnwindSafe,

§

impl<CONTEXT, STATE, OUTPUT, STEP> Send for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: Send, STATE: Send, OUTPUT: Send, STEP: Send,

§

impl<CONTEXT, STATE, OUTPUT, STEP> Sync for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: Sync, STATE: Sync, OUTPUT: Sync, STEP: Sync,

§

impl<CONTEXT, STATE, OUTPUT, STEP> Unpin for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: Unpin, STATE: Unpin, OUTPUT: Unpin, STEP: Unpin,

§

impl<CONTEXT, STATE, OUTPUT, STEP> UnwindSafe for Computation<CONTEXT, STATE, OUTPUT, STEP>
where CONTEXT: UnwindSafe, STATE: UnwindSafe, OUTPUT: UnwindSafe, STEP: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.