actix_broker/issue.rs
1use actix::prelude::*;
2
3use std::any::TypeId;
4
5use crate::broker::{ArbiterBroker, RegisteredBroker, SystemBroker};
6use crate::msgs::*;
7
8/// The `BrokerIssue` provides functions to issue messages to subscribers.
9///
10/// This will not deliver the message to the actor that sent it.
11pub trait BrokerIssue
12where
13 Self: Actor,
14 <Self as Actor>::Context: AsyncContext<Self>,
15{
16 /// Asynchronously issue a message.
17 /// This bypasses the mailbox capacity, and will always queue the message.
18 /// If the mailbox is closed, the message is silently dropped and the subscriber
19 /// is detached from the broker.
20 fn issue_async<T: RegisteredBroker, M: BrokerMsg>(&self, msg: M) {
21 let broker = T::get_broker();
22 broker.do_send(IssueAsync(msg, TypeId::of::<Self>()));
23 }
24
25 /// Synchronously issue a message.
26 /// This also causes the broker to synchronously forward those messages on to any subscribers
27 /// before handling any other messages.
28 fn issue_sync<T: RegisteredBroker, M: BrokerMsg>(&self, msg: M, ctx: &mut Self::Context) {
29 let broker = T::get_broker();
30 broker
31 .send(IssueSync(msg, TypeId::of::<Self>()))
32 .into_actor(self)
33 .map(|_, _, _| ())
34 .wait(ctx);
35 }
36
37 /// Helper to asynchronously issue to an system broker
38 /// This is the equivalent of `self.issue_async::<SystemBroker, M>(ctx);`
39 fn issue_system_async<M: BrokerMsg>(&self, msg: M) {
40 self.issue_async::<SystemBroker, M>(msg);
41 }
42
43 /// Helper to synchronously issue to an system broker
44 /// This is the equivalent of `self.issue_sync::<SystemBroker, M>(ctx);`
45 fn issue_system_sync<M: BrokerMsg>(&self, msg: M, ctx: &mut Self::Context) {
46 self.issue_sync::<SystemBroker, M>(msg, ctx);
47 }
48
49 /// Helper to asynchronously issue to an arbiter-specific broker
50 /// This is the equivalent of `self.issue_async::<ArbiterBroker, M>(ctx);`
51 fn issue_arbiter_async<M: BrokerMsg>(&self, msg: M) {
52 self.issue_async::<ArbiterBroker, M>(msg);
53 }
54
55 /// Helper to synchronously issue to an arbiter-specific broker
56 /// This is the equivalent of `self.issue_sync::<ArbiterBroker, M>(ctx);`
57 fn issue_arbiter_sync<M: BrokerMsg>(&self, msg: M, ctx: &mut Self::Context) {
58 self.issue_sync::<ArbiterBroker, M>(msg, ctx);
59 }
60}
61
62impl<A> BrokerIssue for A
63where
64 A: Actor,
65 <A as Actor>::Context: AsyncContext<A>,
66{
67}