pub trait Actor:
Send
+ Sized
+ 'static {
type Error: Send;
type Message: Send;
// Required method
fn on_msg(
&mut self,
this: &WeakActorRef<Self>,
msg: Self::Message,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
// Provided methods
fn on_start(
&mut self,
this: &WeakActorRef<Self>,
) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
fn on_stop(
&mut self,
stop: Option<Self::Message>,
) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
fn run_with(
&mut self,
mailbox: Mailbox<Self>,
) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
fn into_future(
self,
mailbox_size: Option<usize>,
) -> (ActorRef<Self>, ActorRun<Self>) { ... }
}Required Associated Types§
Required Methods§
Sourcefn on_msg(
&mut self,
this: &WeakActorRef<Self>,
msg: Self::Message,
) -> impl Future<Output = Result<(), Self::Error>> + Send
fn on_msg( &mut self, this: &WeakActorRef<Self>, msg: Self::Message, ) -> impl Future<Output = Result<(), Self::Error>> + Send
Called when a message is received by the actor. This is where the actor’s main logic should be implemented.
this is a WeakActorRef, which can be used to send
messages to itself, or to stop itself. As this is a weak reference, the
actor may have already been dropped by external code, so it may not be
possible to use the weak reference.
Provided Methods§
Sourcefn on_start(
&mut self,
this: &WeakActorRef<Self>,
) -> impl Future<Output = Result<(), Self::Error>> + Send
fn on_start( &mut self, this: &WeakActorRef<Self>, ) -> impl Future<Output = Result<(), Self::Error>> + Send
Called when the actor is started, and before any messages are processed. Useful for initialization/setup logic that should run in the same async context as the actor, and not in the Actor struct’s constructor.
this is a WeakActorRef, which can be used to send
messages to itself, or to stop itself. As this is a weak reference, the
actor may have already been dropped by external code, so it may not be
possible to use the weak reference.
Sourcefn on_stop(
&mut self,
stop: Option<Self::Message>,
) -> impl Future<Output = Result<(), Self::Error>> + Send
fn on_stop( &mut self, stop: Option<Self::Message>, ) -> impl Future<Output = Result<(), Self::Error>> + Send
Called when the actor is stopped. This is the place to perform any
cleanup logic, such as closing connections, etc. This method is called
after the actor has received a stop message, or after the last ActorRef
to the actor is dropped.
If the actor is stopped by dropping the last ActorRef to it, stop is None.
Otherwise, stop contains the stop message that was sent to the actor.
Sourcefn run_with(
&mut self,
mailbox: Mailbox<Self>,
) -> impl Future<Output = Result<(), Self::Error>> + Send
fn run_with( &mut self, mailbox: Mailbox<Self>, ) -> impl Future<Output = Result<(), Self::Error>> + Send
Runs the actor with the given mailbox. Unless you have a specific reason to,
the default implementation of this method should be used. You should not need to
call this method directly either, see Actor::into_future instead.
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.