#[repr(align(64))]pub struct Topic<T> { /* private fields */ }Expand description
A message topic that delivers only the latest published message to subscribers.
Implementations§
Source§impl<T> Topic<T>where
T: Clone,
impl<T> Topic<T>where
T: Clone,
Sourcepub fn publish(&self, message: T)
pub fn publish(&self, message: T)
Publishes a message to this topic.
§Examples
let bus = Bus::<Bytes>::new();
let topic = bus.topic("events");
topic.publish(Bytes::from("Hello, World!"));Sourcepub fn subscribe(self: &Arc<Self>) -> Sub<T>
pub fn subscribe(self: &Arc<Self>) -> Sub<T>
Creates a new subscriber for this topic.
Subscribers receive only messages published after they subscribe. The subscriber will receive the latest published message and all subsequent messages until it’s dropped.
§Examples
let bus = Bus::<String>::new();
let topic = bus.topic("events");
let mut subscriber = topic.subscribe();Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Returns the number of active subscribers.
This count includes all subscribers that haven’t been dropped yet.
§Examples
let bus = Bus::<String>::new();
let topic = bus.topic("events");
assert_eq!(topic.subscriber_count(), 0);
let _sub1 = topic.subscribe();
let _sub2 = topic.subscribe();
assert_eq!(topic.subscriber_count(), 2);Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the topic name.
§Examples
let bus = Bus::<String>::new();
let topic = bus.topic("events");
assert_eq!(topic.name(), "events");Sourcepub fn has_subscribers(&self) -> bool
pub fn has_subscribers(&self) -> bool
Returns true if the topic has any subscribers.
This is equivalent to topic.subscriber_count() > 0 but more efficient.
§Examples
let bus = Bus::<String>::new();
let topic = bus.topic("events");
assert!(!topic.has_subscribers());
let _subscriber = topic.subscribe();
assert!(topic.has_subscribers());Source§impl Topic<Bytes>
Zero-copy bytes operations for Topics.
impl Topic<Bytes>
Zero-copy bytes operations for Topics.
Sourcepub fn publish_slice(&self, data: &[u8])
pub fn publish_slice(&self, data: &[u8])
Publishes a byte slice to this topic.
This method copies the slice into a Bytes object for publishing.
For true zero-copy, use publish_vec with a Vec<u8>.
§Arguments
data- Byte slice to publish
§Examples
let bus = Bus::<Bytes>::new();
let topic = bus.topic("events");
let data = b"Hello, World!";
topic.publish_slice(data);Sourcepub fn publish_vec(&self, data: Vec<u8>)
pub fn publish_vec(&self, data: Vec<u8>)
Publishes a Vec<u8> to this topic.
This method provides zero-copy publishing by converting the Vec<u8>
directly into a Bytes object without copying the data.
§Arguments
data- Vector of bytes to publish
§Examples
let bus = Bus::<Bytes>::new();
let topic = bus.topic("events");
let data = vec![72, 101, 108, 108, 111]; // "Hello"
topic.publish_vec(data);