1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Sean Lawlor
//
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree.
//! [DerivedActorRef] wraps an [ActorCell] to send messages that can be converted
//! to its accepted type using [From]. It represents a subset of the messages supported
//! by the original actor.
use Arc;
use crateActorCell;
use crateActorRef;
use crateMessage;
use crateMessagingErr;
/// [DerivedActorRef] wraps an [ActorCell] to send messages that can be converted
/// into its accepted type using [From]. [DerivedActorRef] allows to create isolation
/// between actors by hiding the actual message type.
///
/// ## Example
/// ```
/// // In this example the actor is a ghost kitchen which can accept orders of different types,
/// // representing multiple virtual restaurants at once. More specifically, it can accept
/// // pizza or sushi orders.
/// //
/// // Derived actor allows to hide the order message accepted by the kitchen actor, therefore
/// // we can pass the ref to other components without creating a direct dependency on the
/// // kitchen actor. We can easily replace or split the kitchen actor without affecting
/// // other components communicating with it.
/// use ractor::{Actor, ActorProcessingErr, ActorRef, DerivedActorRef, Message};
///
/// // First we define order types
/// struct PizzaOrder {
/// topping: String,
/// }
///
/// struct SushiOrder {
/// r#type: String,
/// quantity: usize,
/// }
///
/// // The order message which is actually sent to the kitchen actor can be either pizza or sushi
/// enum Order {
/// Pizza(PizzaOrder),
/// Sushi(SushiOrder),
/// }
///
/// // Implementing conversion methods from different order types to the actual order
/// impl From<PizzaOrder> for Order {
/// fn from(value: PizzaOrder) -> Self {
/// Order::Pizza(value)
/// }
/// }
///
/// impl TryFrom<Order> for PizzaOrder {
/// type Error = String;
///
/// fn try_from(value: Order) -> Result<Self, Self::Error> {
/// match value {
/// Order::Pizza(order) => Ok(order),
/// _ => Err("Order has invalid type".to_string()),
/// }
/// }
/// }
///
/// impl From<SushiOrder> for Order {
/// fn from(value: SushiOrder) -> Self {
/// Order::Sushi(value)
/// }
/// }
///
/// impl TryFrom<Order> for SushiOrder {
/// type Error = String;
///
/// fn try_from(value: Order) -> Result<Self, Self::Error> {
/// match value {
/// Order::Sushi(order) => Ok(order),
/// _ => Err("Order has invalid type".to_string()),
/// }
/// }
/// }
///
/// #[cfg(feature = "cluster")]
/// impl Message for Order {
/// fn serializable() -> bool {
/// false
/// }
/// }
///
/// struct Kitchen;
///
/// #[cfg_attr(feature = "async-trait", ractor::async_trait)]
/// impl Actor for Kitchen {
/// type Msg = Order;
/// type State = ();
/// type Arguments = ();
///
/// async fn pre_start(
/// &self,
/// _myself: ActorRef<Self::Msg>,
/// _: (),
/// ) -> Result<Self::State, ActorProcessingErr> {
/// Ok(())
/// }
///
/// async fn handle(
/// &self,
/// _myself: ActorRef<Self::Msg>,
/// message: Self::Msg,
/// _state: &mut Self::State,
/// ) -> Result<(), ActorProcessingErr> {
/// match message {
/// Order::Pizza(order) => {
/// println!("Preparing pizza with topping {}", order.topping);
/// }
/// Order::Sushi(order) => {
/// println!(
/// "Preparing {} sushi of type {}",
/// order.quantity, order.r#type
/// );
/// }
/// }
/// Ok(())
/// }
/// }
///
/// async fn example() {
/// let (kitchen_actor_ref, kitchen_actor_handle) = Actor::spawn(None, Kitchen, ()).await.unwrap();
///
/// // derived actor ref can be passed to the pizza restaurant actor which accepts pizza orders from delivery apps
/// let pizza_restaurant: DerivedActorRef<PizzaOrder> = kitchen_actor_ref.get_derived();
/// pizza_restaurant.send_message(PizzaOrder {
/// topping: String::from("pepperoni"),
/// }).expect("Failed to order pizza");
///
/// // same way, we can also get a derived actor ref which can only accept sushi orders
/// let sushi_restaurant: DerivedActorRef<SushiOrder> = kitchen_actor_ref.get_derived();
/// sushi_restaurant.send_message(SushiOrder {
/// r#type: String::from("sashimi"),
/// quantity: 3,
/// }).expect("Failed to order sushi");
///
/// kitchen_actor_handle.await.unwrap();
/// }
/// ```
// Allows all the functionality of ActorCell on DerivedActorRef