Skip to main content

LocalActor

Trait LocalActor 

Source
pub trait LocalActor: 'static {
    type Message: Send + 'static;
    type State: 'static;
    type Initialize: 'static;

    // Required methods
    async fn initialize(
        &self,
        handle: &ActorHandle<Self::Message>,
        tags: &Tags,
        initialize: Self::Initialize,
    ) -> Result<Self::State, ActorError>;
    async fn handle(
        &self,
        handle: &ActorHandle<Self::Message>,
        message: Self::Message,
        state: &mut Self::State,
    ) -> Result<(), ActorError>;

    // Provided methods
    fn tags(&self, tags: Tags) -> Result<Tags, ActorError> { ... }
    async fn shutdown(&self, _state: Self::State) -> Result<(), ActorError> { ... }
    fn spawner(
        tags: Tags,
        actor: Self,
    ) -> Result<LocalActorSpawner<Self>, ActorError>
       where Self: Sized + 'static { ... }
    fn spawn_with(
        spawner: impl LocalTaskSpawner,
        tags: Tags,
        actor: Self,
        initialize: Self::Initialize,
    ) -> Result<LocalActorInstance<Self>, ActorError>
       where Self: Sized + 'static { ... }
}
Expand description

A LocalActor will not moved between threads. This is sometimes necessary when interfacing with external code. This trait allows to implement such behaviour with same public interface as a normal [Actor] (ActorHandle). For new code that dont have this requirement is usually better to use [Actor] as it allows to use multithreading.

Required Associated Types§

Source

type Message: Send + 'static

Source

type State: 'static

Source

type Initialize: 'static

Required Methods§

Source

async fn initialize( &self, handle: &ActorHandle<Self::Message>, tags: &Tags, initialize: Self::Initialize, ) -> Result<Self::State, ActorError>

Source

async fn handle( &self, handle: &ActorHandle<Self::Message>, message: Self::Message, state: &mut Self::State, ) -> Result<(), ActorError>

Provided Methods§

Source

fn tags(&self, tags: Tags) -> Result<Tags, ActorError>

Source

async fn shutdown(&self, _state: Self::State) -> Result<(), ActorError>

Shutdown the actor. This is not cancelable. After this call no more message will be received. Will not be executed if actor panics.

Source

fn spawner( tags: Tags, actor: Self, ) -> Result<LocalActorSpawner<Self>, ActorError>
where Self: Sized + 'static,

Source

fn spawn_with( spawner: impl LocalTaskSpawner, tags: Tags, actor: Self, initialize: Self::Initialize, ) -> Result<LocalActorInstance<Self>, ActorError>
where Self: Sized + 'static,

Spawn actor using a task spawner.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§