Hello.
I would like to run actors based on the configuration.
In the first step I want to create an actor. Then start it if the name is in the list (cfg).
How to get sender address in handler ?
use actix::*;
use std::collections::HashMap;
struct MyActor
{
val: usize,
}
impl MyActor
{
pub fn new() -> Self { return MyActor{ val: 0 }; }
}
impl Actor for MyActor
{
type Context = Context<Self>;
}
#[derive(Debug, Clone)]
pub struct PushData<T> {
pub value: T,
}
impl<T: 'static> Message for PushData<T>
{
type Result = T;
}
impl Handler<PushData> for MyActor
{
type Result = ();
fn handle(&mut self, msg: PushData, ctx: &mut Self::Context) -> Self::Result
{
// ...any code
}
}
fn main()
{
//load actors
let mut actors = HashMap::new();
actors.insert("MyActor1", MyActor::new());
//config
let mut cfg = Vec::new();
cfg.push("MyActor1");
cfg.push("MyActor2");
cfg.push("MyActorX");
for c in cfg
{
System::run(move || {
let addr = actors[c].start();
});
}
}
Errors:
error[E0507]: cannot move out of index of `std::collections::HashMap<&str, MyActor>`
--> src/main.rs:45:24
|
45 | let addr = actors[c].start();
| ^^^^^^^^^ move occurs because value has type `MyActor`, which does not implement the `Copy` trait
|
32 | let mut actors = HashMap::new();
| ---------- move occurs because `actors` has type `std::collections::HashMap<&str, MyActor>`, which does not implement the `Copy` trait
...
44 | System::run(move || {
| ^^^^^^^ value moved into closure here, in previous iteration of loop
45 | let addr = actors[c].start();
| ------ use occurs due to use in closure