Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use actix::prelude::*;
- /// This actor needs to handle the message.
- struct LeafActor;
- impl Actor for LeafActor {
- type Context = Context<Self>;
- }
- /// This actor needs to receive a message, pass it to the leaf actor, and then handle the result
- /// from the leaf actor.
- struct NodeActor {
- leaf_addr: Addr<LeafActor>,
- }
- impl Actor for NodeActor {
- type Context = Context<Self>;
- }
- #[derive(Debug)]
- struct Msg;
- #[derive(Debug)]
- enum MyError {
- /// An actual error that will be returned from the leaf actor.
- Logical,
- /// Indicating that something went wrong in the mechanism (a.k.a - a mailbox error)
- Technical,
- }
- impl Message for Msg {
- type Result = Result<(), MyError>;
- }
- impl Handler<Msg> for LeafActor {
- type Result = Result<(), MyError>;
- fn handle(&mut self, msg: Msg, _ctx: &mut Context<Self>) -> Self::Result {
- println!("Leaf actor got {:?}", msg);
- Err(MyError::Logical)
- }
- }
- impl Handler<Msg> for NodeActor {
- type Result = ResponseActFuture<Self, (), MyError>;
- fn handle(&mut self, msg: Msg, _ctx: &mut Context<Self>) -> Self::Result {
- println!("Node actor got {:?}", msg);
- Box::new(
- self.leaf_addr.send(msg).into_actor(self)
- .map_err(|error: MailboxError, _, _| {
- println!("Got a mailbox error {:?}, but we have to pass a MyError", error);
- MyError::Technical
- })
- .map(|result: Result<(), MyError>, _, _| {
- println!("The result node actor got from leaf actor is {:?}, but we can't pass the error", result);
- () // this is all we can return here...
- })
- )
- }
- }
- fn main() {
- let sys = System::new("my-system");
- let leaf_addr = LeafActor.start();
- let node_addr = NodeActor{leaf_addr}.start();
- Arbiter::spawn(node_addr.send(Msg)
- .map(|response: Result<(), MyError>| {
- println!("Main got response from node actor: {:?}", response);
- })
- .map_err(|error: MailboxError| {
- println!("Main got mailbox error from node actor: {:?}", error);
- })
- );
- sys.run().unwrap();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement