Skip to main content

Subscription

Struct Subscription 

Source
pub struct Subscription { /* private fields */ }
Expand description

RAII handle for event subscriptions.

When a Subscription is dropped, the associated handler is automatically removed from the event bus. This ensures proper cleanup even when code panics or returns early.

§Thread Safety

Subscription is Send + Sync, allowing it to be moved between threads and shared (though sharing is unusual since drop triggers unsubscription).

§Example

// Subscription automatically unsubscribes when dropped
{
    let sub = bus.subscribe::<MyEvent, _>(100, handler);
    // Handler is active here
} // Handler is removed here

Implementations§

Source§

impl Subscription

Source

pub const fn id(&self) -> SubscriptionId

Get the subscription ID.

Source

pub const fn type_id(&self) -> TypeId

Get the TypeId of the subscribed event type.

Source

pub const fn type_name(&self) -> &'static str

Get the type name of the subscribed event type.

Source

pub fn is_active(&self) -> bool

Check if the subscription is still active.

Returns false after cancel() has been called.

Source

pub fn cancel(&self)

Explicitly cancel the subscription.

This is equivalent to dropping the subscription, but allows for explicit control over when unsubscription occurs.

Calling cancel() multiple times is safe and idempotent.

Source

pub fn detach(&self)

Detach the subscription from RAII lifecycle management.

After calling this, dropping the Subscription will NOT unsubscribe the handler. The handler remains active until the EventBus is dropped.

This is necessary when the owner of the Subscription has a shorter lifetime than the desired handler lifetime (e.g., modules that are dropped after initialization while their event handlers should persist for the session).

§Example
let sub = bus.subscribe::<MyEvent, _>(100, handler);
sub.detach(); // Handler stays active even after `sub` is dropped

Trait Implementations§

Source§

impl Debug for Subscription

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Subscription

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.