Module ring

Module ring 

Source
Expand description

A bounded mpsc channel that drops the oldest item when full instead of applying backpressure.

This is useful for scenarios where you want to keep the most recent items and can tolerate losing older ones, such as real-time data streams or status updates where only the latest values matter.

§Example

use futures::executor::block_on;
use futures::{SinkExt, StreamExt};
use commonware_utils::{NZUsize, channels::ring};

block_on(async {
    let (mut sender, mut receiver) = ring::channel::<u32>(NZUsize!(2));

    // Fill the channel
    sender.send(1).await.unwrap();
    sender.send(2).await.unwrap();

    // This will drop the oldest item (1) and insert 3
    sender.send(3).await.unwrap();

    // Receive the remaining items
    assert_eq!(receiver.next().await, Some(2));
    assert_eq!(receiver.next().await, Some(3));
});

Structs§

ChannelClosed
Error returned when sending to a channel whose receiver has been dropped.
Receiver
The receiving half of a ring channel.
Sender
The sending half of a ring channel.

Functions§

channel
Creates a new ring channel with the specified capacity.