pub struct ReplayChannel<T: Clone + Send + 'static> { /* private fields */ }
Expand description

A ReplayChannel provides a multi-receiver, message-passing communication channel where receivers can “catch up” by receiving all previously sent messages before continuing to receive new messages.

Each new Receiver created from a ReplayChannel will first receive all messages that were sent prior to its creation, ensuring that it starts with the full context. Once it has caught up, it will then receive messages as they are sent in real-time.

This is particularly useful in scenarios where the state history is important and late-joining receivers need to process all past messages to be properly synchronized with the current state.

§Examples

Creating a ReplayChannel and sending messages:

let replay_channel = ReplayChannel::new();
let sender = replay_channel.sender();
sender.send("message 1");
sender.send("message 2");

let mut receiver = replay_channel.receiver();
assert_eq!(receiver.receive().await, "message 1");
assert_eq!(receiver.receive().await, "message 2");

let mut new_receiver = replay_channel.receiver();
assert_eq!(new_receiver.receive().await, "message 1");
assert_eq!(new_receiver.receive().await, "message 2");

sender.send("message 3");
assert_eq!(new_receiver.receive().await, "message 3");

Implementations§

source§

impl<T: Clone + Send + Sync + 'static> ReplayChannel<T>

source

pub fn new() -> Self

source

pub fn sender(&self) -> Sender<T>

source

pub fn receiver(&self) -> Receiver<T>

Auto Trait Implementations§

§

impl<T> Freeze for ReplayChannel<T>

§

impl<T> !RefUnwindSafe for ReplayChannel<T>

§

impl<T> Send for ReplayChannel<T>

§

impl<T> Sync for ReplayChannel<T>

§

impl<T> Unpin for ReplayChannel<T>

§

impl<T> !UnwindSafe for ReplayChannel<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.