pub trait Actor:
Sized
+ Send
+ 'static {
// Provided methods
fn started<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn stopped<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn start_default<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
where Self: Default + 'async_trait { ... }
fn start<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
where Self: 'async_trait { ... }
}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 Handle<T> implementation for this message.
All messages are statically typed.
Provided Methods§
Sourcefn started<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn started<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when the actor is first started.
Sourcefn stopped<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn stopped<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called after an actor is stopped.
Sourcefn start_default<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>where
Self: Default + 'async_trait,
fn start_default<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>where
Self: Default + 'async_trait,
Construct and start a new actor, returning its address.
This is constructs a new actor using the Default trait, and invokes its start method.
Sourcefn start<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>where
Self: 'async_trait,
fn start<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>where
Self: 'async_trait,
Start a new actor, returning its address.
§Examples
use xactor::*;
struct MyActor;
impl Actor for MyActor {}
#[message(result = "i32")]
struct MyMsg(i32);
#[async_trait::async_trait]
impl Handler<MyMsg> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>, msg: MyMsg) -> i32 {
msg.0 * msg.0
}
}
#[xactor::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(())
}Examples found in repository?
More examples
10async fn main() -> std::io::Result<()> {
11 let parent_addr = SubscriberParent::new().await.start().await.unwrap();
12 parent_addr.wait_for_stop().await;
13 Ok(())
14}
15
16// Subscriber Parent - A
17
18struct SubscriberParent {
19 children_subscribers: Vec<Addr<Subscriber>>,
20 message_producer: Addr<MessageProducer>,
21}
22
23impl SubscriberParent {
24 async fn new() -> SubscriberParent {
25 SubscriberParent {
26 children_subscribers: Vec::new(),
27 message_producer: MessageProducer::new().start().await.unwrap(),
28 }
29 }
30}
31
32#[async_trait::async_trait]
33impl Actor for SubscriberParent {
34 async fn started(&mut self, ctx: &mut Context<Self>) -> Result<()> {
35 println!("Subscriber Parent Started");
36 let _ = ctx.address().send(InitializeChildSubscribers);
37 Ok(())
38 }
39}
40
41#[message]
42struct InitializeChildSubscribers;
43
44#[async_trait::async_trait]
45impl Handler<InitializeChildSubscribers> for SubscriberParent {
46 async fn handle(&mut self, _ctx: &mut Context<Self>, _msg: InitializeChildSubscribers) {
47 let message_producer_addr = self.message_producer.clone();
48 let dummy_ids: Vec<i32> = vec![1, 2, 3, 4, 5];
49 let children_unstarted_actors_vec = dummy_ids.into_iter().map(move |id| {
50 let id = id.clone();
51 let addr = message_producer_addr.clone();
52
53 Subscriber::new(id, addr)
54 });
55
56 let children_addr_vec = children_unstarted_actors_vec
57 .into_iter()
58 .map(|actor| async { actor.start().await.unwrap() });
59
60 let children_addr_vec = futures::future::join_all(children_addr_vec).await;
61
62 self.children_subscribers = children_addr_vec;
63 }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.