#pubsub #future #futures #messages-sent

flo_stream

Pubsub and related streams for Rust futures

9 releases (breaking)

0.7.0 Nov 20, 2021
0.6.0 Feb 8, 2021
0.5.0 Jan 31, 2021
0.4.0 Dec 29, 2019
0.1.1 Aug 7, 2018

#372 in Concurrency

Download history 729/week @ 2023-11-23 708/week @ 2023-11-30 708/week @ 2023-12-07 728/week @ 2023-12-14 267/week @ 2023-12-21 176/week @ 2023-12-28 442/week @ 2024-01-04 423/week @ 2024-01-11 371/week @ 2024-01-18 453/week @ 2024-01-25 482/week @ 2024-02-01 665/week @ 2024-02-08 1103/week @ 2024-02-15 499/week @ 2024-02-22 507/week @ 2024-02-29 69/week @ 2024-03-07

2,441 downloads per month
Used in 6 crates (4 directly)

Apache-2.0

85KB
1K SLoC

flo_stream = "0.7"

flo_stream

flo_stream is a crate providing some extra utilities for streams in Rust's futures library. The primary new feature it provides is a "pubsub" mechanism - a way to subscribe to updates sent to a futures Sink. This differs from the Sender/Receiver mechanism provided in the main futures library in two key ways: it's possible to have multiple receivers, and messages sent when there is no subscriber connected will be ignored.

PubSub

The sink type provided is Publisher. You can create one with let publisher = Publisher::new(10). This implements the Sink trait so can be used in a very similar way to send messages. The number passed in is the maximum number of waiting messages allowed for any given subscriber.

A subscription can be created using let subscription = publisher.subscribe(). Any messages sent to the sink after this is called is relayed to all subscriptions. A subscription is a Stream so can interact with other parts of the futures library in the usual way.

Here's a full worked example with a single subscriber:

let mut publisher       = Publisher::new(10);
let mut subscriber      = publisher.subscribe();

executor::block_on(async {
    publisher.publish(1).await;
    publisher.publish(2).await;
    publisher.publish(3).await;

    assert!(subscriber.next().await == Some(1));
    assert!(subscriber.next().await == Some(2));
    assert!(subscriber.next().await == Some(3));
});

It's also possible to call subscriber.clone() to create a new subscription from an existing one without needing to keep a reference to the publisher. This can be used to reduce the amount of effort needed in passing objects around, and to hide implementation details from the caller.

Dependencies

~1MB
~18K SLoC