parallel-event-emitter

Crates.ioparallel-event-emitter
lib.rsparallel-event-emitter
version0.2.4
sourcesrc
created_at2017-01-18 11:41:11.168938
updated_at2017-01-20 23:46:59.054802
descriptionParallel Event Emitter for concurrent listener invocation based on futures-rs and futures-cpupool
homepage
repositoryhttps://github.com/novacrazy/parallel-event-emitter
max_upload_size
id8121
size35,118
Nova (novacrazy)

documentation

https://docs.rs/parallel-event-emitter/

README

Parallel Event Emitter

Implementation of an event emitter that invokes event listener callbacks concurrently in a configurable thread pool, using Futures to notify callers of success or errors.

Because all values must be transferred across thread boundaries, all types T must be Send.

Additionally, all types T must be Any, so T: 'static.

Usage

[dependencies]
futures = "0.1"
parallel-event-emitter = "0.2.4"

Example using a String as the key:

extern crate futures;
extern crate parallel_event_emitter;

use futures::Future;
use parallel_event_emitter::*;

fn main() {
    let mut emitter: ParallelEventEmitter<String> = ParallelEventEmitter::new();

    emitter.add_listener("some event", || {
        println!("Hello, World!");

        Ok(())
    }).unwrap();

    assert_eq!(1, emitter.emit("some event").wait().unwrap());
}

Or using a custom event type:

extern crate futures;
extern crate parallel_event_emitter;

use futures::Future;
use parallel_event_emitter::*;

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
enum MyEvents {
    EventA,
    EventB,
}

fn main() {
    let mut emitter: ParallelEventEmitter<MyEvents> = ParallelEventEmitter::new();

    emitter.add_listener(MyEvents::EventA, || {
        println!("Hello, World!");

        Ok(())
    }).unwrap();

    assert_eq!(1, emitter.emit(MyEvents::EventA).wait().unwrap());
}

Trace<E> type

This crate depends on the trace-error crate to have simple and lightweight backtraces on all error Results.

If you choose not to use that, which is fine by me, simply call .into_error() on all Trace<E> values to get the real error.

impl Trait feature

Instead of having all the emit* methods returning a boxed Future (BoxFuture), the Cargo feature conservative_impl_trait can be given to enable impl Future return types on all the emit* methods.

[dependencies.parallel-event-emitter]
version = "0.2.4"
features = ["default", "conservative_impl_trait"] # And maybe integer_atomics

Larger ListenerIds

Although the ListenerId type itself is u64, the atomic counter underneath is restricted to AtomicUsize by default.

To enable true guaranteed 64-bit counters, use the integer_atomics feature for the crate.

[dependencies.parallel-event-emitter]
version = "0.2.4"
features = ["default", "integer_atomics"] # And maybe conservative_impl_trait
Commit count: 30

cargo fmt