When the code below is executed with the asynchronous context everything works fine. However, when switching to the synchronized context, the ctx.wait
method stops working.
use actix::prelude::*;
use tokio::time::{sleep, Duration};
#[derive(Message)]
#[rtype(result = "()")]
struct Sum(usize, usize);
struct Calculator;
impl Actor for Calculator {
type Context = SyncContext<Self>;
// type Context = Context<Self>;
}
impl Handler<Sum> for Calculator {
type Result = ();
fn handle(&mut self, msg: Sum, ctx: &mut Self::Context) -> Self::Result {
let fut = stop(msg.0, msg.1);
let actor_future = fut.into_actor(self);
ctx.wait(actor_future);
}
}
async fn stop(a: usize, b: usize) {
sleep(Duration::from_millis(3000)).await;
println!("{}", a + b);
}
#[actix::main]
async fn main() {
let addr = SyncArbiter::start(3, || Calculator);
// let addr = Calculator.start();
let _res = addr.do_send(Sum(10, 5));
sleep(Duration::from_millis(4000)).await;
}
I receive the following error message
error[E0599]: the method `wait` exists for mutable reference `&mut SyncContext<Calculator>`, but its trait bounds were not satisfied
--> src/main.rs:21:13
|
21 | ctx.wait(actor_future);
| ^^^^ method cannot be called on `&mut SyncContext<Calculator>` due to unsatisfied trait bounds
|
::: /home/lucas/.cargo/registry/src/index.crates.io-6f17d22bba15001f/actix-0.13.1/src/sync.rs:237:1
|
237 | pub struct SyncContext<A>
| -------------------------
| |
| doesn't satisfy `<_ as ActorFuture<_>>::Output = ()`
| doesn't satisfy `_: ContextFutureSpawner<_>`
| doesn't satisfy `actix::SyncContext<Calculator>: ActorFuture<_>`
|
= note: the following trait bounds were not satisfied:
`<&mut actix::SyncContext<Calculator> as ActorFuture<_>>::Output = ()`
which is required by `&mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`&mut actix::SyncContext<Calculator>: ActorFuture<_>`
which is required by `&mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`<&&mut actix::SyncContext<Calculator> as ActorFuture<_>>::Output = ()`
which is required by `&&mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`&&mut actix::SyncContext<Calculator>: ActorFuture<_>`
which is required by `&&mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`<&mut &mut actix::SyncContext<Calculator> as ActorFuture<_>>::Output = ()`
which is required by `&mut &mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`&mut &mut actix::SyncContext<Calculator>: ActorFuture<_>`
which is required by `&mut &mut actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`<actix::SyncContext<Calculator> as ActorFuture<_>>::Output = ()`
which is required by `actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`actix::SyncContext<Calculator>: ActorFuture<_>`
which is required by `actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`<&actix::SyncContext<Calculator> as ActorFuture<_>>::Output = ()`
which is required by `&actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>`
`&actix::SyncContext<Calculator>: ActorFuture<_>`
which is required by `&actix::SyncContext<Calculator>: actix::ContextFutureSpawner<_>