Expand description
§Computation process
A Rust library for defining stateful computations (and generators) that support suspend/resume, interleaving, cancellation, and serialization.
This crate does not use unsafe.
§Overview
This library provides abstractions for defining “long-running” computations. The concepts are similar to asynchronous code but offer features that were never a priority in async programming. The target audience are projects that implement CPU-intensive, long-running computations but require granular control over the computation state.
§Key Features
- Cancellation: Each computation can be forcefully stopped using cooperative
cancellation (compatible with the
cancel-thiscrate). - Suspend/resume: A computation can define safe suspend points. During these points, it is safe to serialize, interleave, or otherwise “transfer” the computation.
- Interleaving: Suspend points allow safely interleaving multiple computations on a single thread with priority-based scheduling.
- Serialization: The state of each computation is isolated and can be saved/restored.
§Core Concepts
Completable<T>: A result type that can be incomplete (Suspended,Cancelled, orExhausted).Computable<T>: A trait for objects that can be driven to completion by callingComputable::try_compute.Algorithm<CTX, STATE, T>: ExtendsComputablewith access to context and state.Generatable<T>: LikeComputable, but produces a stream of values.GenAlgorithm<CTX, STATE, T>: ExtendsGeneratablewith context and state.ComputationandGenerator: Default implementations using step functions.
§Quick Example
use computation_process::{Computation, ComputationStep, Completable, Incomplete, Computable, Stateful};
struct CountingStep;
impl ComputationStep<u32, u32, u32> for CountingStep {
fn step(target: &u32, count: &mut u32) -> Completable<u32> {
*count += 1;
if *count >= *target {
Ok(*count)
} else {
Err(Incomplete::Suspended)
}
}
}
let mut computation = Computation::<u32, u32, u32, CountingStep>::from_parts(5, 0);
let result = computation.compute().unwrap();
assert_eq!(result, 5);Structs§
- Collector
- A
Computablethat collects all items from aGeneratableinto a collection. - Computable
Identity - A trivial
Computablethat immediately returns a pre-computed value. - Computable
Result - A result-like object that stores the result of a
Computablefor later use. - Computation
- A stateful computation that can be suspended and resumed.
- Generator
- A stateful generator that can be suspended and resumed.
Enums§
- Incomplete
- The error type returned by an algorithm when the result is not (yet) available.
Traits§
- Algorithm
- Extends
Computabletrait with immutableCONTEXTand mutableSTATE. - Computable
- A generic trait implemented by types that represent a “computation”.
- Computation
Step - Defines a single step of a
Computation. - GenAlgorithm
- Extends
Generatabletrait with immutableCONTEXTand mutableSTATE. - Generatable
- An alternative to
crate::Computablewhich is intended for generators. - Generator
Step - Defines a single step of a
Generator. - Stateful
- A shared interface of objects that provide access to
an immutable
CONTEXTand mutableSTATE.
Type Aliases§
- Completable
- A
Completableresult is a value eventually computed by an algorithm where the computation can beIncompletewhen the value is polled. - DynAlgorithm
- A type alias for
Box<dyn Algorithm<CONTEXT, STATE, OUTPUT>>. - DynComputable
- A type alias for
Box<dyn Computable<T>>. - DynGen
Algorithm - A type alias for
Box<dyn GenAlgorithm<CONTEXT, STATE, OUTPUT>>. - DynGeneratable
- A type alias for
Box<dyn Generatable<T>>.