Skip to main content

Crate skid_pipe

Crate skid_pipe 

Source
Expand description

Reusable, stateful computation pipelines for embedded, Wasm, and native Rust.

Build computation modules as values and compose them with Pipe::from_chain and Pipe::then_chain. Composition uses no allocation; user-provided stages remain responsible for their own resource use.

skid-pipe’s default feature set is dependency-free and uses only core. It adds no allocator, dynamic dispatch, runtime, or executor requirement, so the same pipeline types work on native targets, WebAssembly, and no_std firmware. The opt-in tokio feature adds task-spawning adapters for Tokio applications without changing the default core. The crate contains no unsafe code at all: async sequencing is ordinary async blocks, so the compiler generates every state machine, its drop glue, and its pin projection.

§Synchronous pipeline

use skid_pipe::Pipe;

let mut pipeline = Pipe::new(|value: u8| value + 1)
    .then(|value| value * 2);

assert_eq!(pipeline.run(4), 10);

§Fallible pipeline

use skid_pipe::TryPipe;

fn decode(value: u8) -> Result<u16, &'static str> {
    if value == 0 { Err("empty") } else { Ok(u16::from(value)) }
}

fn classify(value: u16) -> Result<bool, &'static str> {
    Ok(value > 10)
}

let mut pipeline = TryPipe::new(decode).try_then(classify);
assert_eq!(pipeline.run(12), Ok(true));

§Fallible asynchronous pipeline

TryAsyncPipe composes asynchronous Result-returning functions and stops at the first error, without selecting an executor or allocating.

use skid_pipe::TryAsyncPipe;

async fn fetch(value: u8) -> Result<u16, &'static str> {
    Ok(u16::from(value))
}

async fn validate(value: u16) -> Result<bool, &'static str> {
    Ok(value > 10)
}

let mut pipeline = TryAsyncPipe::new(fetch).try_then(validate);
assert_eq!(pipeline.run(12).await, Ok(true));

§Asynchronous pipeline

AsyncPipe composes functions that return core::future::Future. It does not poll the future itself or select an executor.

use skid_pipe::AsyncPipe;

async fn increment(value: u8) -> u8 {
    value + 1
}

async fn double(value: u8) -> u8 {
    value * 2
}

let mut pipeline = AsyncPipe::new(increment).then(double);
assert_eq!(pipeline.run(4).await, 10);

§Branching

Branching is an ordinary if or match inside a stage, so this crate adds no combinator for it. match dispatches over any number of arms, and the compiler already requires every arm to produce the same type.

use skid_pipe::Pipe;

let mut pipeline = Pipe::new(|value: i32| value)
    .then(|value: i32| match value.signum() {
        1 => value * 2,
        -1 => -value,
        _ => 0,
    })
    .then(|value: i32| value + 1);

assert_eq!(pipeline.run(4), 9);
assert_eq!(pipeline.run(-4), 5);

The same holds for AsyncPipe, where only the selected arm is awaited. A FnMut stage can update captured state before returning its future. To update state inside the future, use shared state such as a Cell, or implement AsyncStep for a named stage whose future borrows its mutable state.

§Type names

A pipeline’s concrete type nests with every step (Pipe<F3, Pipe<F2, Pipe<F1, End>>>). Builder functions can hide that name without changing the static pipeline:

Return impl Chain<Input, Output = O> (or impl TryChain, impl AsyncChain, or impl TryAsyncChain) from a builder function. This remains zero-cost and preserves compile-time validation of every connection.

use skid_pipe::{Chain, Pipe};

fn build() -> impl Chain<u16, Output = bool> {
    Pipe::new(|value: u16| value as f32 / 4095.0).then(|value: f32| value > 0.5)
}

let mut pipeline = build();
assert!(pipeline.run(3000));

The crate deliberately has no type-erased or runtime-configured pipeline. AsyncChain also returns a concrete future, so it remains allocation-free.

§Type checking

ⓘ
use skid_pipe::Pipe;

fn decode(_: u8) -> u16 { 0 }
fn needs_boolean(_: bool) -> u32 { 0 }

let mut pipeline = Pipe::new(decode).then(needs_boolean);
let _ = pipeline.run(1_u8);
ⓘ
use skid_pipe::TryPipe;

let mut pipeline = TryPipe::new(|value: u8| Ok::<_, u8>(value))
    .try_then(|value| Ok::<_, bool>(value));
let _ = pipeline.run(1_u8);
ⓘ
use skid_pipe::AsyncPipe;

async fn decode(_: u8) -> u16 { 0 }
async fn needs_boolean(_: bool) -> u32 { 0 }

let mut pipeline = AsyncPipe::new(decode).then(needs_boolean);
let _ = pipeline.run(1_u8);
ⓘ
use skid_pipe::TryAsyncPipe;

async fn decode(_: u8) -> Result<u16, ()> { Ok(0) }
async fn needs_boolean(_: bool) -> Result<u32, ()> { Ok(0) }

let mut pipeline = TryAsyncPipe::new(decode).try_then(needs_boolean);
let _ = pipeline.run(1_u8);

Structs§

AsyncPipe
A reusable, statically typed asynchronous function pipeline.
ChainStage
Adapts a synchronous Chain to a Step without allocation or erasure.
End
Recursive terminator shared by synchronous and asynchronous pipelines.
Pipe
A reusable, statically typed synchronous function pipeline.
TryAsyncPipe
A reusable, statically typed fallible asynchronous pipeline.
TryPipe
A reusable, statically typed pipeline of fallible functions.

Traits§

AsyncChain
A complete asynchronous pipeline, runnable for one input value.
AsyncChainSend
The Send variant of AsyncChain for one pipeline borrow.
AsyncStep
A callable asynchronous pipeline stage.
Chain
A complete synchronous pipeline, runnable for one input value.
Step
A callable synchronous pipeline stage.
TokioAsyncChainExttokio
Spawns an infallible async chain on a Tokio runtime.
TokioTryAsyncChainExttokio
Spawns a fallible async chain on a Tokio runtime.
TryAsyncChain
A complete fallible asynchronous pipeline, runnable for one input value.
TryAsyncChainSend
The Send variant of TryAsyncChain for one pipeline borrow.
TryAsyncStep
A callable fallible asynchronous pipeline stage.
TryChain
A complete fallible pipeline, runnable for one input value.
TryStep
A callable Result-returning pipeline stage.