pub trait Handler<Actor: Sync + Send + 'static, Message: Sync + Send + 'static, State: Sync + Send + 'static, Response: Sync + Send + 'static, Error: Sync + Send + 'static> {
// Required method
fn receive<'life0, 'async_trait>(
&'life0 self,
ctx: Arc<Context<Actor, Message, State, Response, Error>>
) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn pre_start<'life0, 'async_trait>(
&'life0 self,
_state: Arc<Mutex<State>>,
_self_ref: Arc<ActorRef<Actor, Message, State, Response, Error>>
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
fn pre_stop<'life0, 'async_trait>(
&'life0 self,
_state: Arc<Mutex<State>>,
_self_ref: Arc<ActorRef<Actor, Message, State, Response, Error>>
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Handler is a trait that defines the behavior of an actor in the Actorlib framework.
This trait is implemented by any type that can act as an actor in the Actorlib framework. It provides methods for handling messages and managing the actor’s lifecycle.
§Type parameters
Actor: The type of the actor. This type must beSync,Sendand'static.Message: The type of the messages that the actor can process. This type must beSync,Sendand'static.State: The type of the state of the actor. This type must beSync,Sendand'static.Response: The type of the response that the actor produces after processing a message. This type must beSync,Sendand'static.Error: The type of the error that the actor can produce. This type must beSync,Sendand'static.
§Methods
receive: This method is called when the actor receives a message. It takes a context, which contains the message and the state of the actor, and returns aFuturethat resolves to aResultcontaining either the response produced by the actor after processing the message, or an error.pre_start: This method is called before the actor starts. It takes the state of the actor and a reference to the actor itself, and returns aFuturethat resolves to aResult. If theResultisOk, the actor starts; if it isErr, the actor does not start. By default, this method returnsOk(()).pre_stop: This method is called before the actor stops. It takes the state of the actor and a reference to the actor itself, and returns aFuturethat resolves to aResult. If theResultisOk, the actor stops; if it isErr, the actor does not stop. By default, this method returnsOk(()).