Struct replay_channel::ReplayChannel
source · 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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more