Expand description
A library providing high-level abstractions for event loop concurrency, heavily borrowing ideas from KJ. Allows for coordination of asynchronous tasks using promises as a basic building block.
§Example
use gj::{EventLoop, Promise, ClosedEventPort};
EventLoop::top_level(|wait_scope| -> Result<(),()> {
let (promise1, fulfiller1) = Promise::<(),()>::and_fulfiller();
let (promise2, fulfiller2) = Promise::<(),()>::and_fulfiller();
let promise3 = promise2.then(|_| {
println!("world");
Promise::ok(())
});
let promise4 = promise1.then(move |_| {
println!("hello ");
fulfiller2.fulfill(());
Promise::ok(())
});
fulfiller1.fulfill(());
Promise::all(vec![promise3, promise4].into_iter())
.map(|_| Ok(()))
.wait(wait_scope, &mut ClosedEventPort(()))
}).expect("top level");
Macros§
- pry
- Like
try!()
, but for functions that return aPromise<T, E>
rather than aResult<T, E>
.
Structs§
- Closed
Event Port - An event port that never emits any events. On wait() it returns the error it was constructed with.
- Event
Loop - A queue of events being executed in a loop on a single thread.
- Forked
Promise - The result of
Promise::fork()
. Allows branches to be created. Dropping theForkedPromise
along with any branches created throughadd_branch()
will cancel the computation. - Promise
- A computation that might eventually resolve to a value of type
T
or to an error of typeE
. Dropping the promise cancels the computation. - Promise
Fulfiller - A handle that can be used to fulfill or reject a promise. If you think of a promise as the receiving end of a oneshot channel, then this is the sending end.
- TaskSet
- Holds a collection of
Promise<T, E>
s and ensures that each executes to completion. Destroying aTaskSet
automatically cancels all of its unfinished promises. - Wait
Scope - A scope in which asynchronous programming can occur. Corresponds to the top level scope of some event loop. Can be used to wait for the result of a promise.
Traits§
- Event
Port - Interface between an
EventLoop
and events originating from outside of the loop’s thread. Needed inPromise::wait()
. - Fulfiller
Dropped - Specifies an error to generate when a
PromiseFulfiller
is dropped. - Task
Reaper - Callbacks to be invoked when a task in a
TaskSet
finishes. You are required to implement at least the failure case.