Module crossbus::address

source ·
Expand description

types for actors Messages Communication and Handling

In actor model, actor receives message from other actors, and send messages to other actor as well.

From the perspective of an individual actor’s message queue, there are many producers and only the host can consume the messages.

Roughly speaking,

  • Sender is the message producer, through what other actors can send message to self the message queue closed when all sender get dropped and reopen when new sender is created

  • Receiver is the message consumer, used inside of actor itself. The message can be retrived via Stream

  • WeakSender and WeakReceiver can be safe reference of its upgrade

struct Num(uisze);
impl Message for Num {}

struct CrossBus{
    sum: isize
}
impl Actor for CrossBus {
    type Message = Num;

    fn create(ctx: &mut Context<Self>) -> Self {
        Self { sum: 0, }
    }

    fn action(&mut self, msg: Self::Message, ctx: &mut Context<Self>) {
        self.sum += msg.0;
    }

}

let (addr1, _) = CrossBus::start();
let (addr2, _) = CrossBus::start();
let sender1 = addr1.sender();
sender1.send(Num(1)).unwrap();
sender1.send(Num(2)).unwrap();
sender1.send(Num(3)).unwrap();
assert_eq!(sender1.message_number(), 3)
let sender2 = addr2.sender();
sender2.send(Num(-1)).unwrap();
sender2.send(Num(-2)).unwrap();
sender2.send(Num(-3)).unwrap();

Structs

Enums

  • error types that occurs when sending an message to the queue.

Functions

  • An asynchronous thread-safe mpsc(multiple producer single consumer) channel that used to communicate with between actors and threads