Skip to main content

HandlerGuard

Struct HandlerGuard 

Source
pub struct HandlerGuard<E: Send + Sync + 'static> { /* private fields */ }
Available on crate feature sync only.
Expand description

RAII handle for a registered handler.

Drop the guard to unregister. Call HandlerGuard::forget to detach the guard from the handler, leaving the handler registered indefinitely.

The guard is not Clone; ownership of a registration is unique.

§Examples

Automatic cleanup when the guard leaves scope:

use std::sync::Arc;
use registry_io::SyncRegistry;

let registry = Arc::new(SyncRegistry::<u32>::new());
assert_eq!(registry.handler_count(), 0);

{
    let _guard = registry.register_guard(|n| {
        println!("got {n}");
    });
    assert_eq!(registry.handler_count(), 1);
}

assert_eq!(registry.handler_count(), 0);

Forgetting the guard to keep the handler registered:

use std::sync::Arc;
use registry_io::SyncRegistry;

let registry = Arc::new(SyncRegistry::<()>::new());
let guard = registry.register_guard(|_| {});
let id = guard.id();
guard.forget();
// Handler is still active.
assert_eq!(registry.handler_count(), 1);
// Manually unregister via the returned id.
assert!(registry.unregister(id));

Implementations§

Source§

impl<E: Send + Sync + 'static> HandlerGuard<E>

Source

pub fn id(&self) -> HandlerId

Returns the HandlerId of the underlying registration.

Useful for diagnostic logging or to retain the id before consuming the guard with HandlerGuard::forget.

§Examples
use std::sync::Arc;
use registry_io::SyncRegistry;

let registry = Arc::new(SyncRegistry::<()>::new());
let guard = registry.register_guard(|_| {});
let id = guard.id();
drop(guard);
// The id is still meaningful for logging, but the handler is gone:
assert!(!registry.contains(id));
Source

pub fn forget(self)

Consume the guard without unregistering the handler.

The handler remains registered until it is explicitly removed via SyncRegistry::unregister or the registry is dropped.

§Examples
use std::sync::Arc;
use registry_io::SyncRegistry;

let registry = Arc::new(SyncRegistry::<()>::new());
let guard = registry.register_guard(|_| {});
guard.forget();
assert_eq!(registry.handler_count(), 1);

Trait Implementations§

Source§

impl<E: Send + Sync + 'static> Debug for HandlerGuard<E>

Source§

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

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

impl<E: Send + Sync + 'static> Drop for HandlerGuard<E>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<E> Freeze for HandlerGuard<E>

§

impl<E> !RefUnwindSafe for HandlerGuard<E>

§

impl<E> Send for HandlerGuard<E>

§

impl<E> Sync for HandlerGuard<E>

§

impl<E> Unpin for HandlerGuard<E>

§

impl<E> UnsafeUnpin for HandlerGuard<E>

§

impl<E> !UnwindSafe for HandlerGuard<E>

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.