Skip to main content

Crate listener_holder

Crate listener_holder 

Source
Expand description

A thread-safe holder for an optional listener (single observer).

Use this when you have one optional callback/listener that may be set or cleared at runtime, and you want to notify it from any thread without repeating RwLock<Option<...>> boilerplate.

The implementation uses arc-swap for a lock-free read path. See the CONCURRENCY.md document in the repo for a comparison with RwLock<Option<Arc<L>>>.

§Type requirements

L must be Send + Sync so that the holder can be shared across threads. The holder itself is Send + Sync when L: Send + Sync.

§Memory and dropping

The holder stores at most one Arc<L>. Calling set(None) or replacing the listener drops the previous Arc (reference count decremented). Dropping the holder drops its current Arc. Cloning the holder or get clones the Arc; the listener is freed when all Arc references are dropped. There is no custom Drop and no intentional leak.

Structs§

ListenerHolder