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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate::*;
use futures::{Future, FutureExt};
use std::{fmt::Debug, mem::ManuallyDrop, sync::Arc, time::Duration};
use tokio::task::JoinHandle;
#[derive(Debug)]
pub struct Child<E, C = dyn AnyChannel>
where
E: Send + 'static,
C: DynChannel + ?Sized,
{
pub(super) handle: Option<JoinHandle<E>>,
pub(super) channel: Arc<C>,
pub(super) link: Link,
pub(super) is_aborted: bool,
}
impl<E, C> Child<E, C>
where
E: Send + 'static,
C: DynChannel + ?Sized,
{
pub(crate) fn new(channel: Arc<C>, join_handle: JoinHandle<E>, link: Link) -> Self {
Self {
handle: Some(join_handle),
link,
channel,
is_aborted: false,
}
}
fn into_parts(self) -> (Arc<C>, JoinHandle<E>, Link, bool) {
let no_drop = ManuallyDrop::new(self);
unsafe {
let mut handle = std::ptr::read(&no_drop.handle);
let channel = std::ptr::read(&no_drop.channel);
let link = std::ptr::read(&no_drop.link);
let is_aborted = std::ptr::read(&no_drop.is_aborted);
(channel, handle.take().unwrap(), link, is_aborted)
}
}
pub fn into_tokio_joinhandle(self) -> JoinHandle<E> {
self.into_parts().1
}
pub fn abort(&mut self) -> bool {
self.channel.close();
let was_aborted = self.is_aborted;
self.is_aborted = true;
self.handle.as_ref().unwrap().abort();
!was_aborted
}
pub fn is_finished(&self) -> bool {
self.handle.as_ref().unwrap().is_finished()
}
pub fn into_pool(self) -> ChildPool<E, C> {
let (channel, handle, link, is_aborted) = self.into_parts();
ChildPool {
channel,
handles: Some(vec![handle]),
link,
is_aborted,
}
}
pub fn downcast<M: Send + 'static>(self) -> Result<Child<E, Channel<M>>, Self>
where
C: AnyChannel,
{
let (channel, handle, link, is_aborted) = self.into_parts();
match channel.clone().into_any().downcast::<Channel<M>>() {
Ok(channel) => Ok(Child {
handle: Some(handle),
channel,
link,
is_aborted,
}),
Err(_) => Err(Child {
handle: Some(handle),
channel,
link,
is_aborted,
}),
}
}
pub async fn shutdown(&mut self, timeout: Duration) -> Result<E, ExitError> {
self.halt();
match tokio::time::timeout(timeout, &mut *self).await {
Ok(res) => res,
Err(_) => {
self.abort();
self.await
}
}
}
pub fn get_address(&self) -> Address<C> {
self.channel.add_address();
Address::from_channel(self.channel.clone())
}
gen::child_methods!();
gen::dyn_channel_methods!();
}
impl<E, M> Child<E, Channel<M>>
where
E: Send + 'static,
M: Send + 'static,
{
pub fn into_dyn(self) -> Child<E> {
let parts = self.into_parts();
Child {
handle: Some(parts.1),
channel: parts.0,
link: parts.2,
is_aborted: parts.3,
}
}
gen::send_methods!();
}
#[cfg(feature = "internals")]
impl<E, C> Child<E, C>
where
E: Send + 'static,
C: DynChannel + ?Sized,
{
pub fn transform_channel<C2: DynChannel + ?Sized>(
self,
func: fn(Arc<C>) -> Arc<C2>,
) -> Child<E, C2> {
let (channel, handle, link, is_aborted) = self.into_parts();
Child {
channel: func(channel),
handle: Some(handle),
link,
is_aborted,
}
}
pub fn channel_ref(&self) -> &C {
&self.channel
}
}
impl<E: Send + 'static, C: DynChannel + ?Sized> Drop for Child<E, C> {
fn drop(&mut self) {
if let Link::Attached(abort_timer) = self.link {
if !self.is_aborted && !self.is_finished() {
if abort_timer.is_zero() {
self.abort();
} else {
self.halt();
let handle = self.handle.take().unwrap();
tokio::task::spawn(async move {
tokio::time::sleep(abort_timer).await;
handle.abort();
});
}
}
}
}
}
impl<E: Send + 'static, C: DynChannel + ?Sized> Unpin for Child<E, C> {}
impl<E: Send + 'static, C: DynChannel + ?Sized> Future for Child<E, C> {
type Output = Result<E, ExitError>;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.handle
.as_mut()
.unwrap()
.poll_unpin(cx)
.map_err(|e| e.into())
}
}
#[cfg(test)]
mod test {
use std::{future::pending, time::Duration};
use crate::*;
#[tokio::test]
async fn downcast() {
let (child, _addr) = spawn(Config::default(), basic_actor!());
assert!(matches!(child.into_dyn().downcast::<()>(), Ok(_)));
}
#[tokio::test]
async fn abort() {
let (mut child, _addr) = spawn(Config::default(), basic_actor!());
assert!(!child.is_aborted());
child.abort();
assert!(child.is_aborted());
assert!(matches!(child.await, Err(ExitError::Abort)));
}
#[tokio::test]
async fn is_finished() {
let (mut child, _addr) = spawn(Config::default(), basic_actor!());
child.abort();
let _ = (&mut child).await;
assert!(child.is_finished());
}
#[tokio::test]
async fn into_childpool() {
let (child, _addr) = spawn(Config::default(), basic_actor!());
let pool = child.into_pool();
assert_eq!(pool.task_count(), 1);
assert_eq!(pool.process_count(), 1);
assert_eq!(pool.is_aborted(), false);
let (mut child, _addr) = spawn(Config::default(), basic_actor!());
child.abort();
let pool = child.into_pool();
assert_eq!(pool.is_aborted(), true);
}
#[tokio::test]
async fn shutdown_success() {
let (mut child, _addr) = spawn(Config::default(), basic_actor!());
assert!(child.shutdown(Duration::from_millis(5)).await.is_ok());
}
#[tokio::test]
async fn shutdown_failure() {
let (mut child, _addr) = spawn(Config::default(), |_inbox: Inbox<()>| async {
pending::<()>().await;
});
assert!(matches!(
child.shutdown(Duration::from_millis(5)).await,
Err(ExitError::Abort)
));
}
}