Crates.io | thunks |
lib.rs | thunks |
version | 0.1.0 |
source | src |
created_at | 2016-06-19 10:00:08.679806 |
updated_at | 2016-06-20 01:41:35.665536 |
description | Asynchronous composer for Rust. |
homepage | https://github.com/iorust/thunks |
repository | https://github.com/iorust/thunks.git |
max_upload_size | |
id | 5423 |
size | 20,326 |
Asynchronous composer for Rust.
primitive thunk:
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);
Sequence control:
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]);
Parallel control:
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]);
extern crate thunks;
use thunks::Thunk;
pub struct Thunk<T, E>(_)
impl<T, E> Thunk<T, E> where T: 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>