Publisher

Struct Publisher 

Source
pub struct Publisher<T> { /* private fields */ }
Expand description

The sending end of a gyre channel.

Publisher is responsible for sending events to the shared ring buffer. It is Send and Sync, and can be cloned to support multiple concurrent producers. The channel is closed when the last Publisher (including all its clones) is dropped.

Implementations§

Source§

impl<T> Publisher<T>

Source

pub async fn publish(&self, event: T) -> Result<(), T>

Asynchronously publishes an event to the channel.

This method first claims an exclusive slot in the ring buffer, then waits if necessary for the slowest consumer to advance (asynchronous backpressure). Once a slot is confirmed to be free, it writes the event and makes it visible to all consumers.

§Arguments
  • event: The event to be published.
§Returns
  • Ok(()): If the event was successfully published.
  • Err(T): If there are currently no active Consumers. In this case, the event is not published and is returned to the caller.
§Cancellation Safety

This method is cancellation safe. A publisher task first acquires a temporary, exclusive lock to “claim” a slot. If the future is cancelled at any await point (e.g., while waiting for backpressure), the lock is automatically released, and the global state remains consistent. No sequence number “gap” is created.

The next publisher will simply acquire the lock and attempt to publish to the same slot. It is safe to use this method in tokio::select!, timeouts, etc.

Source

pub async fn subscribe(&self) -> Consumer<T>

Dynamically subscribes a new Consumer to the event bus.

The newly created Consumer will receive all events that are newly published after this method call completes. It will not receive any historical events that were already in the ring buffer before the subscription.

This is different from consumer.clone(), which creates a copy that starts consuming from the same position.

§Returns

A new Consumer instance.

§Cancellation Safety

This method is cancellation safe. If the future is cancelled, it will be before any changes to the shared state are made. The new consumer is only registered with the bus after all await points have completed. It is safe to use this method in contexts like tokio::select!.

Trait Implementations§

Source§

impl<T> Clone for Publisher<T>

Source§

fn clone(&self) -> Self

Clones a Publisher.

All cloned Publisher instances share the same publish sequence number, allowing multiple producers to publish to the same channel in a thread-safe manner.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Publisher<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 Publisher<T>

§

impl<T> !RefUnwindSafe for Publisher<T>

§

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

§

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

§

impl<T> Unpin for Publisher<T>

§

impl<T> !UnwindSafe for Publisher<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.