async_event_streams_derive

Crates.ioasync_event_streams_derive
lib.rsasync_event_streams_derive
version0.1.0
sourcesrc
created_at2022-09-06 15:35:33.280948
updated_at2022-09-06 15:35:33.280948
descriptionAsynchronous Events Pub/Sub Library
homepage
repositoryhttps://github.com/milyin/async-event-streams/tree/main/async-event-streams-derive
max_upload_size
id659585
size14,571
Michael Ilyin (milyin)

documentation

README

Async Event Streams

Library for publishing events for multiple consumers using asynchromous streams

Library provides EventStreams<T: 'static + Send + Sync> object which translates events of type T to arbitrary number of EventStream objects, which implements standard futures::Stream interface

Usage example

use futures::{executor::LocalPool, task::LocalSpawnExt, StreamExt};
use async_events::EventStreams;

let mut pool = LocalPool::new();

let streams = EventStreams::new();
let mut stream = streams.create_event_stream();

let sender_task = async move {
    assert!(streams.count() == 1);
    streams.send_event(42, None).await;
    streams.send_event(451, None).await;
    streams.send_event(1984, None).await;
};

let receiver_task = async move {
    let mut values = Vec::new();
    while let Some(event) = stream.next().await {
        values.push(*event)
    }
    // next() returns none when 'streams' is dropped
    assert!(values == vec![42, 451, 1984]);
};

pool.spawner().spawn_local(sender_task);
pool.spawner().spawn_local(receiver_task);
pool.run();
Commit count: 129

cargo fmt