async-pipeline

Crates.ioasync-pipeline
lib.rsasync-pipeline
version0.0.2
sourcesrc
created_at2022-10-28 03:52:05.285162
updated_at2022-10-28 03:56:34.601546
descriptionEasy way to pipeline sync and async functions
homepage
repository
max_upload_size
id700054
size21,670
(huyumars)

documentation

README

async-pipeline

Easy way to pipeline sync and async functions

Example

use async_pipeline::begin;
use async_pipeline::link::{Linkable, Pipeline};
use async_pipeline::Error;
use std::time::Duration;
use tokio::time::sleep;

fn square(n: i32) -> String {
    (n * n).to_string()
}

fn from_str(n: String) -> Result<i32, Error> {
    Ok(n.parse::<i32>()?)
}

#[tokio::main]
async fn main() {
    let pipeline = begin()
        .then(square)
        .then_async(|mut s| async {
            s.push('9');
            println!("wait 1 second");
            sleep(Duration::from_secs(1)).await;
            s
        })
        .then_result(from_str)
        .then_async_result(|i| async move {
            match i % 7 {
                0 => Err("inject error".into()),
                _ => Ok(i),
            }
        })
        .then(|i| {
            println!("last step");
            i
        });
    let r1 = pipeline.process(2).await;
    println!("{}", r1.err().unwrap());
    //assert!(r1.err().unwrap());
    let r2 = pipeline.process(3).await;
    println!("{}", r2.unwrap());
}

Commit count: 0

cargo fmt