Skip to main content

behavior/supervision/
protocol.rs

1//! Typed event and command protocols used by supervision behaviors.
2
3use crate::protocol::forward::forward_event_lane;
4use crate::protocol::{ChildStopped, CreationResolved, WorkerCreationResolved, WorkerStopped};
5use crate::{Address, Behavior, User, UserEvent};
6
7#[derive(Clone, PartialEq, Eq)]
8pub enum ProxyEvent<E: UserEvent> {
9    Command(E),
10    ChildStopped(ChildStopped<E::Addr>),
11    CreationResolved(CreationResolved<<E::Addr as Address>::Nonce>),
12}
13
14impl<E: UserEvent> crate::RouteInput<CreationResolved<<E::Addr as Address>::Nonce>>
15    for ProxyEvent<E>
16{
17    fn route(
18        event: CreationResolved<<E::Addr as Address>::Nonce>,
19    ) -> Result<Self, CreationResolved<<E::Addr as Address>::Nonce>> {
20        Ok(Self::CreationResolved(event))
21    }
22}
23
24impl<E: UserEvent> crate::EventInput<CreationResolved<<E::Addr as Address>::Nonce>>
25    for ProxyEvent<E>
26{
27    fn inject(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Self {
28        Self::CreationResolved(event)
29    }
30}
31
32impl<E: UserEvent> crate::RouteInput<ChildStopped<E::Addr>> for ProxyEvent<E> {
33    fn route(event: ChildStopped<E::Addr>) -> Result<Self, ChildStopped<E::Addr>> {
34        Ok(Self::ChildStopped(event))
35    }
36}
37
38impl<E: UserEvent> crate::EventInput<ChildStopped<E::Addr>> for ProxyEvent<E> {
39    fn inject(event: ChildStopped<E::Addr>) -> Self {
40        Self::ChildStopped(event)
41    }
42}
43
44impl<E: UserEvent> UserEvent for ProxyEvent<E> {
45    type Addr = E::Addr;
46    type Message = E::Message;
47
48    fn user(from: Self::Addr, message: Self::Message) -> Self {
49        Self::Command(E::user(from, message))
50    }
51
52    fn into_user(self) -> Result<User<Self::Addr, Self::Message>, Self> {
53        match self {
54            Self::Command(event) => event.into_user().map_err(Self::Command),
55            service @ (Self::ChildStopped(_) | Self::CreationResolved(_)) => Err(service),
56        }
57    }
58}
59
60forward_event_lane!(ProxyEvent, crate::TimerElapsed, Command);
61forward_event_lane!(ProxyEvent, crate::PeerStopped<E::Addr>, Command);
62forward_event_lane!(ProxyEvent, crate::WorkerStopped<E::Addr>, Command);
63forward_event_lane!(
64    ProxyEvent,
65    crate::WorkerCreationResolved<<E::Addr as crate::Address>::Nonce>,
66    Command
67);
68forward_event_lane!(ProxyEvent, crate::ShutdownRequested, Command);
69
70#[derive(Clone, PartialEq, Eq)]
71pub enum SupervisionEvent<E: UserEvent> {
72    Behavior(E),
73    ChildStopped(ChildStopped<E::Addr>),
74    WorkerStopped(WorkerStopped<E::Addr>),
75    CreationResolved(CreationResolved<<E::Addr as Address>::Nonce>),
76    WorkerCreationResolved(WorkerCreationResolved<<E::Addr as Address>::Nonce>),
77}
78
79impl<E: UserEvent> crate::RouteInput<CreationResolved<<E::Addr as Address>::Nonce>>
80    for SupervisionEvent<E>
81{
82    fn route(
83        event: CreationResolved<<E::Addr as Address>::Nonce>,
84    ) -> Result<Self, CreationResolved<<E::Addr as Address>::Nonce>> {
85        Ok(Self::CreationResolved(event))
86    }
87}
88
89impl<E: UserEvent> crate::EventInput<CreationResolved<<E::Addr as Address>::Nonce>>
90    for SupervisionEvent<E>
91{
92    fn inject(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Self {
93        Self::CreationResolved(event)
94    }
95}
96
97impl<E: UserEvent> crate::RouteInput<WorkerCreationResolved<<E::Addr as Address>::Nonce>>
98    for SupervisionEvent<E>
99{
100    fn route(
101        event: WorkerCreationResolved<<E::Addr as Address>::Nonce>,
102    ) -> Result<Self, WorkerCreationResolved<<E::Addr as Address>::Nonce>> {
103        Ok(Self::WorkerCreationResolved(event))
104    }
105}
106
107impl<E: UserEvent> crate::EventInput<WorkerCreationResolved<<E::Addr as Address>::Nonce>>
108    for SupervisionEvent<E>
109{
110    fn inject(event: WorkerCreationResolved<<E::Addr as Address>::Nonce>) -> Self {
111        Self::WorkerCreationResolved(event)
112    }
113}
114
115impl<E: UserEvent> crate::RouteInput<ChildStopped<E::Addr>> for SupervisionEvent<E> {
116    fn route(event: ChildStopped<E::Addr>) -> Result<Self, ChildStopped<E::Addr>> {
117        Ok(Self::ChildStopped(event))
118    }
119}
120
121impl<E: UserEvent> crate::EventInput<ChildStopped<E::Addr>> for SupervisionEvent<E> {
122    fn inject(event: ChildStopped<E::Addr>) -> Self {
123        Self::ChildStopped(event)
124    }
125}
126
127impl<E: UserEvent> crate::RouteInput<WorkerStopped<E::Addr>> for SupervisionEvent<E> {
128    fn route(event: WorkerStopped<E::Addr>) -> Result<Self, WorkerStopped<E::Addr>> {
129        Ok(Self::WorkerStopped(event))
130    }
131}
132
133impl<E: UserEvent> crate::EventInput<WorkerStopped<E::Addr>> for SupervisionEvent<E> {
134    fn inject(event: WorkerStopped<E::Addr>) -> Self {
135        Self::WorkerStopped(event)
136    }
137}
138
139impl<E: UserEvent> UserEvent for SupervisionEvent<E> {
140    type Addr = E::Addr;
141    type Message = E::Message;
142
143    fn user(from: Self::Addr, message: Self::Message) -> Self {
144        Self::Behavior(E::user(from, message))
145    }
146
147    fn into_user(self) -> Result<User<Self::Addr, Self::Message>, Self> {
148        match self {
149            Self::Behavior(event) => event.into_user().map_err(Self::Behavior),
150            service @ (Self::ChildStopped(_)
151            | Self::WorkerStopped(_)
152            | Self::CreationResolved(_)
153            | Self::WorkerCreationResolved(_)) => Err(service),
154        }
155    }
156}
157
158forward_event_lane!(SupervisionEvent, crate::TimerElapsed);
159forward_event_lane!(SupervisionEvent, crate::PeerStopped<E::Addr>);
160forward_event_lane!(SupervisionEvent, crate::ShutdownRequested);
161
162/// Commands accepted by a stable proxy.
163#[derive(Debug)]
164pub enum ProxyCommand<C: Behavior> {
165    Forward(C::Msg),
166    Replace(C),
167}