Struct Topic

Source
#[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,

Source

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!"));
Source

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();
Source

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);
Source

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");
Source

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.

Source

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);
Source

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);

Trait Implementations§

Source§

impl<T: Debug> Debug for Topic<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Topic<T>

§

impl<T> RefUnwindSafe for Topic<T>

§

impl<T> Send for Topic<T>
where T: Send + Sync,

§

impl<T> Sync for Topic<T>
where T: Send + Sync,

§

impl<T> Unpin for Topic<T>

§

impl<T> UnwindSafe for Topic<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>,

Source§

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>,

Source§

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.