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 hereImplementations§
Source§impl Subscription
impl Subscription
Sourcepub const fn id(&self) -> SubscriptionId
pub const fn id(&self) -> SubscriptionId
Get the subscription ID.
Sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Check if the subscription is still active.
Returns false after cancel() has been called.
Sourcepub fn cancel(&self)
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.
Sourcepub fn detach(&self)
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