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>
impl<E: Send + Sync + 'static> HandlerGuard<E>
Sourcepub fn id(&self) -> HandlerId
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));Sourcepub fn forget(self)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more