pub trait Actor: Sized + Send + 'static {
const NAME: &'static str = "hannibal::Actor";
// Provided methods
fn started(
&mut self,
ctx: &mut Context<Self>
) -> impl Future<Output = Result<()>> + Send { ... }
fn stopped(
&mut self,
ctx: &mut Context<Self>
) -> impl Future<Output = ()> + Send { ... }
fn start_default() -> impl Future<Output = Result<Addr<Self>>> + Send
where Self: Default { ... }
fn start(self) -> impl Future<Output = Result<Addr<Self>>> + Send { ... }
fn name(&self) -> Cow<'static, str> { ... }
}
Expand description
Actors are objects which encapsulate state and behavior.
Actors run within a specific execution context Context<A>
.
The context object is available only during execution.
Each actor has a separate execution context.
Roles communicate by exchanging messages.
The requester can wait for a response.
By Addr
referring to the actors, the actors must provide an Handler<T>
implementation for this message.
All messages are statically typed.
Provided Associated Constants§
Provided Methods§
sourcefn started(
&mut self,
ctx: &mut Context<Self>
) -> impl Future<Output = Result<()>> + Send
fn started( &mut self, ctx: &mut Context<Self> ) -> impl Future<Output = Result<()>> + Send
Called when the actor is first started.
sourcefn stopped(
&mut self,
ctx: &mut Context<Self>
) -> impl Future<Output = ()> + Send
fn stopped( &mut self, ctx: &mut Context<Self> ) -> impl Future<Output = ()> + Send
Called after an actor is stopped.
sourcefn start(self) -> impl Future<Output = Result<Addr<Self>>> + Send
fn start(self) -> impl Future<Output = Result<Addr<Self>>> + Send
Start a new actor, returning its address.
§Examples
use hannibal::*;
struct MyActor;
impl Actor for MyActor {}
#[message(result = i32)]
struct MyMsg(i32);
impl Handler<MyMsg> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>, msg: MyMsg) -> i32 {
msg.0 * msg.0
}
}
#[hannibal::main]
async fn main() -> Result<()> {
// Start actor and get its address
let mut addr = MyActor.start().await?;
// Send message `MyMsg` to actor via addr
let res = addr.call(MyMsg(10)).await?;
assert_eq!(res, 100);
Ok(())
}
fn name(&self) -> Cow<'static, str>
Object Safety§
This trait is not object safe.