Module actor_local

Module actor_local 

Source
Expand description

Per-actor local storage, analogous to thread_local! but scoped to actor instances.

Use ActorLocal to declare static variables whose values are isolated per actor.

§Example

use hyperactor::ActorLocal;
use hyperactor::actor_local::Entry;

static REQUEST_COUNT: ActorLocal<u64> = ActorLocal::new();

impl Handler<MyMessage> for MyActor {
    async fn handle(&mut self, cx: &Context<Self>, msg: MyMessage) -> Result<(), Error> {
        // Fluent style (most common)
        *REQUEST_COUNT.entry(cx).or_default().get_mut() += 1;

        // Or with and_modify
        REQUEST_COUNT.entry(cx)
            .and_modify(|v| *v += 1)
            .or_insert(0);

        // Or pattern matching style
        match REQUEST_COUNT.entry(cx) {
            Entry::Occupied(mut o) => {
                *o.get_mut() += 1;
            }
            Entry::Vacant(v) => {
                v.insert(0);
            }
        }

        Ok(())
    }
}

§Thread Safety

Each ActorLocal has its own internal lock, so accessing different ActorLocal statics concurrently is safe and does not cause deadlocks.

Structs§

ActorLocal
Per-actor local storage slot.
ActorLocalStorage
Storage container for actor-local values.
OccupiedEntry
Entry for an occupied actor-local slot with typed access.
VacantEntry
Entry for a vacant actor-local slot with typed access.

Enums§

Entry
Entry into actor-local storage with type-safe access.