1 个不稳定版本

0.1.0 2022年9月6日

#46 in #event-stream


async_event_streams中使用

MIT/Apache

34KB
547 代码行

异步事件流

使用异步流为多个消费者发布事件的库

库提供EventStreams<T: 'static + Send + Sync>对象,该对象将类型T的事件转换为任意数量的EventStream对象,这些对象实现了标准的futures::Stream接口

使用示例

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();

依赖关系

~6–18MB
~218K SLoC