4 个版本

使用旧的 Rust 2015

0.1.0 2016 年 6 月 20 日
0.0.3 2016 年 6 月 19 日
0.0.2 2016 年 6 月 19 日
0.0.1 2016 年 6 月 19 日

#thunk 中排名第 6

MIT/Apache

8KB
124

thunks

Rust 的异步组合器。

Crates version Build Status Coverage Status Crates downloads

演示

原始 thunks

let thunk: Thunk<i32, &str> = Thunk::new(|cb| {
    thread::spawn(move || {
        thread::sleep(Duration::new(3, 0));
        cb(Ok(1));
    });
});
let res = thunk.await().unwrap();
assert_eq!(res, 1);

顺序控制

let thunk_vec: Vec<Thunk<i32, &str>> = vec![
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(1));
        });
    }),
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(2));
        });
    }),
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(3));
        });
    })
];
let res = Thunk::seq(thunk_vec).await().unwrap();
assert_eq!(res, vec![1, 2, 3]);

并行控制

let thunk_vec: Vec<Thunk<i32, &str>> = vec![
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(1));
        });
    }),
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(2));
        });
    }),
    Thunk::new(|cb| {
        thread::spawn(move || {
            thread::sleep(Duration::new(1, 0));
            cb(Ok(3));
        });
    })
];
let res = Thunk::all(thunk_vec).await().unwrap();
assert_eq!(res, vec![1, 2, 3]);

JavaScript 版本: https://github.com/thunks/thunks

API

文档 https://iorust.github.io/thunks/thunks

extern crate thunks;
use thunks::Thunk;

结构体 thunks::Thunk

pub struct Thunk<T, E>(_)

方法

impl<T, E> Thunk<T, E> whereT:Send +'static, E:Send +'static

fn new<F>(task: F) -> Thunk<T, E>
where F: Fn(Box<Fn(Result<T, E>) + Send + 'static>) + Send + 'static
fn seq(thunk_vec: Vec<Thunk<T, E>>) -> Thunk<Vec<T>, E>
fn all(thunk_vec: Vec<Thunk<T, E>>) -> Thunk<Vec<T>, E>
fn await(&self) -> Result<T, E>

无运行时依赖