pub struct ActorRef<Actor, Message, State, Response, Error> { /* private fields */ }Expand description
The ActorRef struct represents a reference to an actor in the Actorlib framework.
It contains the following fields:
-
tx: AMutexwrapping anOptionthat contains a sender part of a message passing channel. This is used to send messages to the actor’s task. -
join_handle: AMutexwrapping anOptionthat contains a handle to the actor’s task. This is used to control the execution of the actor’s task. -
state: AnOptionthat contains the current state of the actor, wrapped in anArc<Mutex>for thread safety. -
self_ref: AMutexwrapping anOptionthat contains a reference to the actor itself. This is useful for actors that need to send messages to themselves. -
message_id: AMutexwrapping an integer that represents the ID of the last message sent to the actor. -
promise: AMutexwrapping aHashMapthat maps message IDs to one-shot channel senders. This is used to send responses back to the sender of a message. -
name: AStringthat represents the name of the actor. -
actor: AnArcthat contains the actor. -
running: AMutexwrapping a boolean that indicates whether the actor is currently running or not.
§Type parameters
Actor: The type of the actor.Message: The type of the messages that the actor can process.State: The type of the state of the actor.Response: The type of the response that the actor produces after processing a message.Error: The type of the error that the actor can produce.
Trait Implementations§
source§impl<Actor: Handler<Actor = Actor, State = State, Message = Message, Error = Error, Response = Response> + SSSD, Message: SSSD, State: SSSD, Response: SSSD, Error: SSSD + Error + From<Error>> ActorRefTrait for ActorRef<Actor, Message, State, Response, Error>
impl<Actor: Handler<Actor = Actor, State = State, Message = Message, Error = Error, Response = Response> + SSSD, Message: SSSD, State: SSSD, Response: SSSD, Error: SSSD + Error + From<Error>> ActorRefTrait for ActorRef<Actor, Message, State, Response, Error>
source§async fn new(
name: impl AsRef<str>,
actor: Self::Actor,
state: Self::State,
buffer: usize,
) -> Result<Arc<Self>, Self::Error>
async fn new( name: impl AsRef<str>, actor: Self::Actor, state: Self::State, buffer: usize, ) -> Result<Arc<Self>, Self::Error>
Creates a new ActorRef instance.
This function takes the following parameters:
name: AStringthat represents the name of the actor.actor: An instance of the actor.state: The initial state of the actor.buffer: The size of the message buffer.
The function returns a Result that contains an Arc to the ActorRef instance in case of success,
or an Error in case of failure.
The function performs the following steps:
- Wraps the initial state in an
Arc<Mutex>. - Creates a new message passing channel with the specified buffer size.
- Creates a new
ActorRefinstance and wraps it in anArc. - Spawns a new asynchronous task that listens for incoming messages and processes them.
- Calls the
pre_startmethod of the actor. - Sets the
runningflag of theActorReftotrue. - Returns the
Arcto theActorRefinstance.
§Type parameters
Actor: The type of the actor. This type must implement theHandlertrait and beSync,Send, and'static.Message: The type of the messages that the actor can process. This type must beDebug,Send, andSync.State: The type of the state of the actor. This type must beDebug,Send, andSync.Response: The type of the response that the actor produces after processing a message. This type must beDebug,Send, andSync.Error: The type of the error that the actor can produce. This type must implement thestd::error::Errortrait and beDebug,Send,Sync, andFrom<std::io::Error>.
source§async fn state(&self) -> Result<Arc<Mutex<Self::State>>, Error>
async fn state(&self) -> Result<Arc<Mutex<Self::State>>, Error>
Retrieves the current state of the actor.
This method is part of the ActorRef struct and is used to retrieve the current state of the actor.
The state is stored in the state field of the ActorRef struct, which is an Option containing
the state wrapped in an Arc<Mutex>.
The method clones the state field and logs a trace message containing the name of the actor and the state.
If the state field is None, the method returns an error. Otherwise, it returns the state.
§Returns
Result<Arc<Mutex<State>>, std::io::Error>: The current state of the actor wrapped in anArc<Mutex>, or an error if thestatefield isNone.
type Actor = Actor
type State = State
type Error = Error
source§impl<Actor: Handler<Actor = Actor, State = State, Message = Message, Error = Error, Response = Response> + SSSD, Message: SSSD, State: SSSD, Response: SSSD, Error: SSSD + Error + From<Error>> ActorTrait for ActorRef<Actor, Message, State, Response, Error>
impl<Actor: Handler<Actor = Actor, State = State, Message = Message, Error = Error, Response = Response> + SSSD, Message: SSSD, State: SSSD, Response: SSSD, Error: SSSD + Error + From<Error>> ActorTrait for ActorRef<Actor, Message, State, Response, Error>
source§async fn ask(&self, msg: Message) -> Result<Response, Error>
async fn ask(&self, msg: Message) -> Result<Response, Error>
Sends a message to the actor and waits for a response.
This method is part of the ActorRef struct and is used to send a message to the actor
and wait for a response. This is done by sending the message and a unique message ID
through a channel to the actor’s task, which processes the message and sends the response
back through a one-shot channel.
The method locks the tx field of the ActorRef struct, which is an Option containing
a sender part of a message passing channel. If the tx field is None, the method returns
an error. Otherwise, it creates a new one-shot channel for the response, increments the
message ID, inserts the sender part of the one-shot channel into the promise field of the
ActorRef struct (which is a HashMap mapping message IDs to one-shot channel senders),
and sends the message and the message ID through the tx channel.
After sending the message, the method awaits the receiver part of the one-shot channel. If the receiver receives a response, the method returns the response. If the receiver is closed without sending a response, the method returns an error.
§Parameters
msg: The message to send to the actor.
§Returns
Result<Response, Error>: The response from the actor, or an error if the actor failed to process the message or if thetxfield isNone.
source§async fn send(&self, msg: Message) -> Result<(), Error>
async fn send(&self, msg: Message) -> Result<(), Error>
Sends a message to the actor without waiting for a response.
This method is part of the ActorRef struct and is used to send a message to the actor
without waiting for a response. This is done by sending the message and a zero message ID
through a channel to the actor’s task, which processes the message.
The method locks the tx field of the ActorRef struct, which is an Option containing
a sender part of a message passing channel. If the tx field is None, the method returns
an error. Otherwise, it sends the message and a zero message ID through the tx channel.
After sending the message, the method returns Ok(()) if the message was sent successfully,
or an error if the message could not be sent.
§Parameters
msg: The message to send to the actor.
§Returns
Result<(), std::io::Error>:Ok(())if the message was sent successfully, or an error if the message could not be sent or if thetxfield isNone.
source§async fn stop(&self) -> Result<(), Error>
async fn stop(&self) -> Result<(), Error>
Stops the actor.
This method is part of the ActorRef struct and is used to stop the actor’s execution.
The method performs the following steps:
- Checks if the actor is already stopped. If it is, the method returns
Ok(()). - Calls the
pre_stopmethod of the actor. - Sets the
runningflag of theActorReftofalse. - Clears the
txandself_reffields of theActorRef. - Aborts the actor’s task if it is running.
- Clears the
join_handlefield of theActorRef. - Logs a debug message indicating that the actor has stopped.
§Returns
Result<(), Error>:Ok(())if the actor was stopped successfully, or anErrorif thepre_stopmethod of the actor returned an error.