pub trait Actor:
Send
+ Sync
+ 'static {
// Required method
fn handle<'life0, 'life1, 'async_trait>(
&'life0 mut self,
message: Message,
context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided methods
fn pre_start<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn stopping<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn subscribe_to_everything(&self) -> bool { ... }
fn try_clone_storage(&self) -> Option<Box<dyn Actor>> { ... }
}Expand description
The core actor trait.
Implementors define how to handle [Message] values and optionally
configure lifecycle hooks (pre_start, stopping).
§Lifecycle
pre_start— called once before the actor begins processing messageshandle— called for each message receivedstopping— called once after the actor’s message loop exits
§Example
use beam::actor::{Actor, ActorContext};
use beam::message::Message;
use async_trait::async_trait;
struct EchoActor;
#[async_trait]
impl Actor for EchoActor {
async fn handle(&mut self, msg: Message, _ctx: &ActorContext) {
// Process message
}
}Required Methods§
Provided Methods§
Sourcefn pre_start<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn pre_start<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called once before the actor starts processing messages.
Override to initialize state, spawn child actors, or establish connections. Defaults to a no-op.
Sourcefn stopping<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn stopping<'life0, 'life1, 'async_trait>(
&'life0 mut self,
_context: &'life1 ActorContext,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called once after the actor’s message loop exits.
Override for cleanup logic. Defaults to a no-op.
Sourcefn subscribe_to_everything(&self) -> bool
fn subscribe_to_everything(&self) -> bool
Whether this actor wants to receive all messages (not just addressed to it). Used by the Multicast adapter.
Defaults to false.
Sourcefn try_clone_storage(&self) -> Option<Box<dyn Actor>>
fn try_clone_storage(&self) -> Option<Box<dyn Actor>>
Attempts to produce a clone of this actor for storage read/write splitting.
Storage adapters override this to return a boxed clone, enabling the
[crate::router::Router] to start separate read and write actors that
share the same underlying database. Non-storage actors return None
(the default).
When the Router receives Some, it starts two actors: one registered
in read_adapters (receives only Get), one in write_adapters
(receives Put, BatchPut, Flush). Both share the same underlying
data store via Arc, so reads see committed writes immediately.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".