-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e505041
commit 2a98fd1
Showing
6 changed files
with
962 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::time::Duration; | ||
|
||
use actix::{Actor, Addr, Context, Handler, MailboxError, Message}; | ||
use actix::clock::sleep; | ||
use actix::fut::result; | ||
|
||
use actix_async_handler::async_handler; | ||
|
||
#[derive(Message, Clone, Copy)] | ||
#[rtype(result = "u64")] | ||
struct Ping(u64); | ||
|
||
struct Ponger {} | ||
|
||
impl Actor for Ponger { | ||
type Context = Context<Self>; | ||
} | ||
|
||
#[async_handler] | ||
impl Handler<Ping> for Ponger { | ||
type Result = u64; | ||
|
||
async fn handle(&mut self, msg: Ping, _ctx: &mut Self::Context) -> Self::Result { | ||
println!("[Ponger] sleeping for {} secs", msg.0); | ||
sleep(Duration::from_secs(msg.0)).await; | ||
println!("[Ponger] woke up."); | ||
msg.0 | ||
} | ||
} | ||
|
||
struct Pinger { | ||
ponger: Addr<Ponger> | ||
} | ||
|
||
impl Actor for Pinger { | ||
type Context = Context<Self>; | ||
} | ||
|
||
#[async_handler] | ||
impl Handler<Ping> for Pinger { | ||
|
||
type Result = u64; | ||
|
||
async fn handle(&mut self, msg: Ping, ctx: &mut Self::Context) -> Self::Result { | ||
|
||
if msg.0 > 1 { | ||
self.ponger.send(msg).await | ||
} | ||
|
||
if msg.0 > 2 { | ||
self.ponger.send(msg).await | ||
} else if msg.0 > 3 { | ||
// This seems useless, but it is here to test the else having an await | ||
self.ponger.send(msg).await | ||
} | ||
|
||
let result: Result<u64, MailboxError> = if msg.0 > 0 { | ||
let part = self.ponger.send(msg).await; | ||
self.ponger.send(Ping(part.unwrap())).await | ||
} else { | ||
Ok(42u64) | ||
}; | ||
|
||
let mut final_result: Result<u64, MailboxError> = Err(MailboxError::Closed); | ||
final_result = if result.unwrap() > 1 { | ||
self.ponger.send(Ping(result.unwrap())).await | ||
} else { | ||
result | ||
}; | ||
|
||
if let Ok(v) = final_result { | ||
self.ponger.send(Ping(v)).await | ||
} | ||
|
||
final_result.unwrap() + 1 | ||
|
||
} | ||
|
||
} | ||
|
||
#[actix_rt::main] | ||
async fn main() { | ||
|
||
let ponger = Ponger {}.start(); | ||
let pinger = Pinger { ponger }.start(); | ||
|
||
println!("Result {}", pinger.send(Ping(2)).await.unwrap()); | ||
println!("Result {}", pinger.send(Ping(3)).await.unwrap()); | ||
} |
Oops, something went wrong.