Skip to main content

guinea_core/actor/
traits.rs

1use crate::actor::event_bus::subscribe::Event;
2use crate::actor::{Addr, Cx};
3use crate::trace::Bus;
4
5pub trait Handler<M: 'static>: 'static {
6    /// Where the handler was written; `#[handler]` fills it in.
7    const DECLARED: Option<crate::actor::shape::Declared> = None;
8
9    /// Whether handling `M` answers it: set for a request's handler, the one
10    /// that returns the reply. A bus lets one subscriber answer each request
11    /// type; the rest only hear it.
12    const ANSWERS: bool = false;
13
14    fn handle(&mut self, msg: M, cx: Cx<Self, M>)
15    where
16        Self: Sized;
17}
18
19pub trait ManagedActor: Sized + 'static {
20    type Bus: EventSubscription<Self>;
21    type Signals;
22    /// Declared outgoing messages per handler, or `Open` when undeclared.
23    type Flow;
24    /// What `actor!` declared, for devtools.
25    const SHAPE: crate::actor::shape::Shape = crate::actor::shape::Shape::UNKNOWN;
26}
27
28/// What an actor's manifest subscribes to on the global bus. `actor!` writes
29/// it; the subscriptions are the actor's, and end when it is disposed.
30pub trait EventSubscription<A> {
31    fn subscribe_into(addr: &Addr<A>);
32}
33
34impl<A, M> EventSubscription<A> for M
35where
36    M: Event,
37    A: Handler<M> + 'static,
38{
39    fn subscribe_into(addr: &Addr<A>) {
40        addr.subscribe_on::<M>(Bus::Global);
41    }
42}
43
44impl<A> EventSubscription<A> for () {
45    fn subscribe_into(_: &Addr<A>) {}
46}
47
48pub trait AllowedSignal<M: 'static> {}
49impl<M: 'static> AllowedSignal<M> for M {}