Trait simpl_actor::Actor

source ·
pub trait Actor: Sized {
    type Ref: ActorRef;

    // Provided methods
    fn name(&self) -> Cow<'_, str> { ... }
    fn actor_ref(&self) -> Self::Ref { ... }
    fn try_actor_ref() -> Option<Self::Ref> { ... }
    async fn on_start(&mut self) -> Result<(), BoxError> { ... }
    async fn on_panic(
        &mut self,
        err: PanicErr
    ) -> Result<Option<ActorStopReason>, BoxError> { ... }
    async fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError> { ... }
    async fn on_link_died(
        &mut self,
        id: u64,
        reason: ActorStopReason
    ) -> Result<Option<ActorStopReason>, BoxError> { ... }
}
Expand description

Defines the core functionality for actors within an actor-based concurrency model.

Implementors of this trait can leverage asynchronous task execution, lifecycle management hooks, and custom error handling.

This can be implemented with default behaviour using the Actor derive macro.

Methods in this trait that return BoxError will stop the actor with the reason ActorReason::Panicked with the error.

Required Associated Types§

source

type Ref: ActorRef

Actor ref type.

Provided Methods§

source

fn name(&self) -> Cow<'_, str>

Actor name, useful for logging.

source

fn actor_ref(&self) -> Self::Ref

Retrieves a reference to the current actor.

§Panics

This function will panic if called outside the scope of an actor.

§Returns

A reference to the actor of type Self::Ref.

Examples found in repository?
examples/counter.rs (line 63)
62
63
64
    pub async fn inc_myself(&self) -> Result<(), SendError<i64>> {
        self.actor_ref().inc_async(1)
    }
More examples
Hide additional examples
examples/link.rs (line 22)
21
22
23
24
25
26
27
28
29
30
31
    async fn on_start(&mut self) -> Result<(), BoxError> {
        let id = self.actor_ref().id();
        println!("starting actor {id}");
        Ok(())
    }

    async fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError> {
        let id = self.actor_ref().id();
        println!("stopping actor {id} because {reason}");
        Ok(())
    }
source

fn try_actor_ref() -> Option<Self::Ref>

Retrieves a reference to the current actor, if available.

§Returns

An Option containing a reference to the actor of type Self::Ref if available, or None if the actor reference is not available.

source

async fn on_start(&mut self) -> Result<(), BoxError>

Hook that is called before the actor starts processing messages.

This asynchronous method allows for initialization tasks to be performed before the actor starts receiving messages.

§Returns

A result indicating successful initialization or an error if initialization fails.

source

async fn on_panic( &mut self, err: PanicErr ) -> Result<Option<ActorStopReason>, BoxError>

Hook that is called when an actor panicked or returns an error during an async message.

This method provides an opportunity to clean up or reset state. It can also determine whether the actor should be killed or if it should continue processing messages by returning None.

§Parameters
  • err: The error that occurred.
§Returns

Whether the actor should continue processing, or be stopped by returning a stop reason.

source

async fn on_stop(self, reason: ActorStopReason) -> Result<(), BoxError>

Hook that is called before the actor is stopped.

This method allows for cleanup and finalization tasks to be performed before the actor is fully stopped. It can be used to release resources, notify other actors, or complete any final tasks.

§Parameters
  • reason: The reason why the actor is being stopped.

Hook that is called when a linked actor dies.

By default, the current actor will be stopped if the reason is anything other than normal.

§Returns

Whether the actor should continue processing, or be stopped by returning a stop reason.

Object Safety§

This trait is not object safe.

Implementors§