Expand description
Thread-local actor registry with lifetime-safe access guards.
§Design
The actor registry stores actors in thread-local storage and provides access via
ActorRef<T> guards. This design addresses several constraints:
- Use-after-free prevention:
ActorRefholds anRcclone, keeping the actor alive even if removed from the registry while the guard exists. - Re-entrant callbacks: Message handlers frequently call back into the registry
to access other actors. Unlike
RefCell-style borrow tracking, multipleActorRefguards can exist simultaneously without panicking. - No
'staticlifetime lie: Previous designs returned&'static mut T, which didn’t reflect actual validity. The guard-based approach ties the borrow to the guard’s lifetime.
§Limitations
- Aliasing not prevented: Two guards can exist for the same actor simultaneously, allowing aliased mutable access. This is technically undefined behavior but is required by the re-entrant callback pattern. Higher-level discipline is required.
- Thread-local only: Guards must not be sent across threads.
§Invariants
These contracts must hold regardless of how the registry is implemented internally. The first three are verified by tests in this module. The fourth is a usage discipline enforced by convention.
- Thread isolation: Each thread has its own registry instance. An actor registered on one thread is never visible from another.
- Guard survival: An
ActorRefkeeps its actor alive via reference counting. Removing or replacing an actor in the registry does not invalidate existing guards. - Type safety:
get_actor_uncheckedandtry_get_actor_uncheckedverify the concrete type at runtime before casting. A type mismatch panics or returnsNone, respectively. - Short-lived guards: Guards must be obtained, used, and dropped within a
single synchronous scope. Never store an
ActorRefin a struct or hold one across an.awaitpoint.
Structs§
- Actor
Ref - A guard providing mutable access to an actor.
- Actor
Registry - Registry for storing actors.
Functions§
- actor_
count - Returns the number of registered actors.
- actor_
exists - Checks if an actor with the
idexists in the registry. - get_
actor - get_
actor_ registry - get_
actor_ unchecked - Returns a guard providing mutable access to the registered actor of type
T. - register_
actor - Registers an actor.
- try_
get_ actor_ unchecked - Attempts to get a guard providing mutable access to the registered actor.