pub struct Topic<T: Send + Sync + 'static> { /* private fields */ }Expand description
A cloneable stream-native broadcast topic.
Clone the handle for publishers. Each call to Topic::subscribe returns a
source blueprint; each materialization registers a fresh subscriber slot.
Subscribers observe every element published after their registration, in the
same global order as every other subscriber, subject to this topic’s
overflow policy.
Capacity is per subscriber and must be greater than zero.
Implementations§
Source§impl<T: Send + Sync + 'static> Topic<T>
impl<T: Send + Sync + 'static> Topic<T>
Sourcepub fn new(capacity: usize, overflow: TopicOverflow) -> StreamResult<Self>
pub fn new(capacity: usize, overflow: TopicOverflow) -> StreamResult<Self>
Create a new topic with per-subscriber capacity and overflow policy.
Panics if capacity == 0.
Sourcepub async fn publish(&self, value: T) -> Result<(), TopicPublishError<T>>
pub async fn publish(&self, value: T) -> Result<(), TopicPublishError<T>>
Publish one value, waiting only under TopicOverflow::Backpressure.
The publish linearizes when this call owns its global sequence turn and loads the subscriber-table snapshot. Subscribers in that snapshot receive the value according to the configured overflow policy; subscribers registered later do not. If the topic has no subscribers, the value is accepted and dropped.
Sourcepub fn try_publish(&self, value: T) -> Result<(), TopicTryPublishError<T>>
pub fn try_publish(&self, value: T) -> Result<(), TopicTryPublishError<T>>
Try to publish one value without awaiting subscriber capacity or an earlier in-flight publisher.
Under TopicOverflow::Backpressure, this returns
TopicTryPublishError::Full if any active subscriber in the publish
snapshot is full. Under all policies, it returns
TopicTryPublishError::Busy if another publisher currently owns an
earlier global publish turn.
Sourcepub fn subscribe(&self) -> Source<T>where
T: Clone,
pub fn subscribe(&self) -> Source<T>where
T: Clone,
Return a source blueprint that registers a fresh subscriber when materialized.
The source emits only elements published after its registration. If the topic is already closed, the materialized source completes immediately.
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Return the number of currently active subscribers.
Sourcepub fn close(&self) -> StreamResult<()>
pub fn close(&self) -> StreamResult<()>
Gracefully close the topic.
Current subscribers drain their queued elements before completing.
Publishers that begin after close fail with TopicPublishError::Closed
or TopicTryPublishError::Closed.