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::{
5    ChildEvent, ChildStopped, CreationEvent, CreationResolved, WorkerCreationEvent,
6    WorkerCreationResolved, WorkerEvent, WorkerStopped,
7};
8use crate::{Address, Behavior, User, UserEvent};
9
10#[derive(Clone, PartialEq, Eq)]
11pub enum ProxyEvent<E: UserEvent> {
12    Inner(E),
13    ChildStopped(ChildStopped<E::Addr>),
14    CreationResolved(CreationResolved<<E::Addr as Address>::Nonce>),
15}
16
17impl<E: UserEvent> CreationEvent for ProxyEvent<E> {
18    fn creation_resolved(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Option<Self> {
19        Some(Self::CreationResolved(event))
20    }
21}
22
23impl<E: UserEvent> crate::EventInput<CreationResolved<<E::Addr as Address>::Nonce>>
24    for ProxyEvent<E>
25{
26    fn inject(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Self {
27        Self::CreationResolved(event)
28    }
29}
30
31impl<E: UserEvent> ChildEvent for ProxyEvent<E> {
32    fn child_stopped(event: ChildStopped<E::Addr>) -> Option<Self> {
33        Some(Self::ChildStopped(event))
34    }
35}
36
37impl<E: UserEvent> crate::EventInput<ChildStopped<E::Addr>> for ProxyEvent<E> {
38    fn inject(event: ChildStopped<E::Addr>) -> Self {
39        Self::ChildStopped(event)
40    }
41}
42
43impl<E: UserEvent> UserEvent for ProxyEvent<E> {
44    type Addr = E::Addr;
45    type Message = E::Message;
46
47    fn user(from: Self::Addr, message: Self::Message) -> Self {
48        Self::Inner(E::user(from, message))
49    }
50
51    fn into_user(self) -> Result<User<Self::Addr, Self::Message>, Self> {
52        match self {
53            Self::Inner(event) => event.into_user().map_err(Self::Inner),
54            service @ (Self::ChildStopped(_) | Self::CreationResolved(_)) => Err(service),
55        }
56    }
57}
58
59forward_event_lane!(ProxyEvent, TimeEvent, time_reached, crate::TimerElapsed);
60forward_event_lane!(
61    ProxyEvent,
62    PeerEvent,
63    peer_stopped,
64    crate::PeerStopped<E::Addr>
65);
66forward_event_lane!(
67    ProxyEvent,
68    WorkerEvent,
69    worker_stopped,
70    crate::WorkerStopped<E::Addr>
71);
72forward_event_lane!(
73    ProxyEvent,
74    WorkerCreationEvent,
75    worker_creation_resolved,
76    crate::WorkerCreationResolved<<E::Addr as crate::Address>::Nonce>
77);
78forward_event_lane!(
79    ProxyEvent,
80    ShutdownEvent,
81    shutdown_requested,
82    crate::ShutdownRequested
83);
84
85#[derive(Clone, PartialEq, Eq)]
86pub enum SupervisionEvent<E: UserEvent> {
87    Inner(E),
88    ChildStopped(ChildStopped<E::Addr>),
89    WorkerStopped(WorkerStopped<E::Addr>),
90    CreationResolved(CreationResolved<<E::Addr as Address>::Nonce>),
91    WorkerCreationResolved(WorkerCreationResolved<<E::Addr as Address>::Nonce>),
92}
93
94impl<E: UserEvent> CreationEvent for SupervisionEvent<E> {
95    fn creation_resolved(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Option<Self> {
96        Some(Self::CreationResolved(event))
97    }
98}
99
100impl<E: UserEvent> crate::EventInput<CreationResolved<<E::Addr as Address>::Nonce>>
101    for SupervisionEvent<E>
102{
103    fn inject(event: CreationResolved<<E::Addr as Address>::Nonce>) -> Self {
104        Self::CreationResolved(event)
105    }
106}
107
108impl<E: UserEvent> WorkerCreationEvent for SupervisionEvent<E> {
109    fn worker_creation_resolved(
110        event: WorkerCreationResolved<<E::Addr as Address>::Nonce>,
111    ) -> Option<Self> {
112        Some(Self::WorkerCreationResolved(event))
113    }
114}
115
116impl<E: UserEvent> crate::EventInput<WorkerCreationResolved<<E::Addr as Address>::Nonce>>
117    for SupervisionEvent<E>
118{
119    fn inject(event: WorkerCreationResolved<<E::Addr as Address>::Nonce>) -> Self {
120        Self::WorkerCreationResolved(event)
121    }
122}
123
124impl<E: UserEvent> ChildEvent for SupervisionEvent<E> {
125    fn child_stopped(event: ChildStopped<E::Addr>) -> Option<Self> {
126        Some(Self::ChildStopped(event))
127    }
128}
129
130impl<E: UserEvent> crate::EventInput<ChildStopped<E::Addr>> for SupervisionEvent<E> {
131    fn inject(event: ChildStopped<E::Addr>) -> Self {
132        Self::ChildStopped(event)
133    }
134}
135
136impl<E: UserEvent> WorkerEvent for SupervisionEvent<E> {
137    fn worker_stopped(event: WorkerStopped<E::Addr>) -> Option<Self> {
138        Some(Self::WorkerStopped(event))
139    }
140}
141
142impl<E: UserEvent> crate::EventInput<WorkerStopped<E::Addr>> for SupervisionEvent<E> {
143    fn inject(event: WorkerStopped<E::Addr>) -> Self {
144        Self::WorkerStopped(event)
145    }
146}
147
148impl<E: UserEvent> UserEvent for SupervisionEvent<E> {
149    type Addr = E::Addr;
150    type Message = E::Message;
151
152    fn user(from: Self::Addr, message: Self::Message) -> Self {
153        Self::Inner(E::user(from, message))
154    }
155
156    fn into_user(self) -> Result<User<Self::Addr, Self::Message>, Self> {
157        match self {
158            Self::Inner(event) => event.into_user().map_err(Self::Inner),
159            service @ (Self::ChildStopped(_)
160            | Self::WorkerStopped(_)
161            | Self::CreationResolved(_)
162            | Self::WorkerCreationResolved(_)) => Err(service),
163        }
164    }
165}
166
167forward_event_lane!(
168    SupervisionEvent,
169    TimeEvent,
170    time_reached,
171    crate::TimerElapsed
172);
173forward_event_lane!(
174    SupervisionEvent,
175    PeerEvent,
176    peer_stopped,
177    crate::PeerStopped<E::Addr>
178);
179forward_event_lane!(
180    SupervisionEvent,
181    ShutdownEvent,
182    shutdown_requested,
183    crate::ShutdownRequested
184);
185
186/// Commands accepted by a stable proxy.
187#[derive(Debug)]
188pub enum ProxyCommand<C: Behavior> {
189    Forward(C::Msg),
190    Replace(C),
191}