Trait simpl_actor::Spawn

source ·
pub trait Spawn {
    type Ref: ActorRef;

    // Required methods
    fn spawn(self) -> Self::Ref;
    fn spawn_link<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Self::Ref> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn spawn_child<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Self::Ref> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn try_actor_ref() -> Option<Self::Ref>;

    // Provided method
    fn actor_ref(&self) -> Self::Ref { ... }
}
Expand description

Provides functionality to spawn actors and obtain references to them.

This trait defines how actors are spawned and allows for retrieving references to the actor. Implementing this trait enables the creation and management of actor instances.

This trait is automatically implemented by the actor macro.

Required Associated Types§

source

type Ref: ActorRef

The type of reference to the actor, allowing for message sending and interaction.

Required Methods§

source

fn spawn(self) -> Self::Ref

Spawns an actor, creating a new instance of it.

This method takes ownership of the actor instance and starts its execution, returning a reference to the newly spawned actor. This reference can be used to send messages to the actor.

The spawned actor is not linked to any others.

§Returns

A reference to the spawned actor, of the type specified by Self::Ref.

Spawns an actor with a bidirectional link between the current actor and the one being spawned.

If either actor dies, Actor::on_link_died will be called on the other actor.

source

fn spawn_child<'async_trait>( self ) -> Pin<Box<dyn Future<Output = Self::Ref> + Send + 'async_trait>>
where Self: 'async_trait,

Spawns an actor with a unidirectional link between the current actor and the child.

If the current actor dies, Actor::on_link_died will be called on the spawned one, however if the spawned actor dies, Actor::on_link_died will not be called.

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.

Provided Methods§

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 64)
63
64
65
    pub async fn inc_myself(&self) -> Result<(), ActorError<i64>> {
        self.actor_ref().inc_async(1).await
    }

Object Safety§

This trait is not object safe.

Implementors§