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
#![deny(missing_docs, warnings)]
use std::any::Any;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use actix::{Actor, ActorFuture, ActorStream, AsyncContext, ResponseActFuture};
use futures::Stream;
use pin_project::pin_project;
use scoped_tls_hkt::scoped_thread_local;
use self::local_handle::local_handle;
mod local_handle;
scoped_thread_local!(static mut CURRENT_ACTOR_CTX: for<'a> (&'a mut dyn Any, &'a mut dyn Any));
fn set_actor_context<A, F, R>(actor: &mut A, ctx: &mut A::Context, f: F) -> R
where
A: Actor,
F: FnOnce() -> R,
{
CURRENT_ACTOR_CTX.set((actor, ctx), f)
}
pub fn with_ctx<A, F, R>(f: F) -> R
where
A: Actor + 'static,
F: FnOnce(&mut A, &mut A::Context) -> R,
{
CURRENT_ACTOR_CTX.with(|(actor, ctx)| {
let actor = actor
.downcast_mut()
.expect("Future was spawned onto the wrong actor");
let ctx = ctx
.downcast_mut()
.expect("Future was spawned onto the wrong actor");
f(actor, ctx)
})
}
pub fn critical_section<A: Actor, F: Future>(f: F) -> impl Future<Output = F::Output>
where
A::Context: AsyncContext<A>,
F: 'static,
{
let (f, handle) = local_handle(f);
with_ctx(|actor: &mut A, ctx: &mut A::Context| ctx.wait(f.interop_actor(actor)));
handle
}
#[pin_project]
#[derive(Debug)]
pub struct FutureInteropWrap<A: Actor, F> {
#[pin]
inner: F,
phantom: PhantomData<fn(&mut A, &mut A::Context)>,
}
impl<A: Actor, F: Future> FutureInteropWrap<A, F> {
fn new(inner: F) -> Self {
Self {
inner,
phantom: PhantomData,
}
}
}
impl<A: Actor, F: Future> ActorFuture for FutureInteropWrap<A, F> {
type Actor = A;
type Output = F::Output;
fn poll(
self: Pin<&mut Self>,
actor: &mut A,
ctx: &mut A::Context,
task: &mut Context,
) -> Poll<Self::Output> {
set_actor_context(actor, ctx, || self.project().inner.poll(task))
}
}
pub trait FutureInterop<A: Actor>: Future + Sized {
fn interop_actor(self, actor: &A) -> FutureInteropWrap<A, Self>;
fn interop_actor_boxed(self, actor: &A) -> ResponseActFuture<A, Self::Output>
where
Self: 'static,
{
Box::new(self.interop_actor(actor))
}
}
impl<A: Actor, F: Future> FutureInterop<A> for F {
fn interop_actor(self, _actor: &A) -> FutureInteropWrap<A, Self> {
FutureInteropWrap::new(self)
}
}
#[pin_project]
#[derive(Debug)]
pub struct StreamInteropWrap<A: Actor, S> {
#[pin]
inner: S,
phantom: PhantomData<fn(&mut A, &mut A::Context)>,
}
impl<A: Actor, S: Stream> StreamInteropWrap<A, S> {
fn new(inner: S) -> Self {
Self {
inner,
phantom: PhantomData,
}
}
}
impl<A: Actor, S: Stream> ActorStream for StreamInteropWrap<A, S> {
type Actor = A;
type Item = S::Item;
fn poll_next(
self: Pin<&mut Self>,
actor: &mut A,
ctx: &mut A::Context,
task: &mut Context,
) -> Poll<Option<Self::Item>> {
set_actor_context(actor, ctx, || self.project().inner.poll_next(task))
}
}
pub trait StreamInterop<A: Actor>: Stream + Sized {
fn interop_actor(self, actor: &A) -> StreamInteropWrap<A, Self>;
fn interop_actor_boxed(self, actor: &A) -> Box<dyn ActorStream<Item = Self::Item, Actor = A>>
where
Self: 'static,
{
Box::new(self.interop_actor(actor))
}
}
impl<A: Actor, S: Stream> StreamInterop<A> for S {
fn interop_actor(self, _actor: &A) -> StreamInteropWrap<A, Self> {
StreamInteropWrap::new(self)
}
}
#[cfg(test)]
mod tests {
use super::{critical_section, with_ctx, FutureInterop};
use actix::prelude::*;
#[derive(Message)]
#[rtype(result = "Result<i32, ()>")]
struct Sum(i32);
struct Summator {
field: i32,
}
impl Actor for Summator {
type Context = Context<Self>;
}
impl Handler<Sum> for Summator {
type Result = ResponseActFuture<Self, Result<i32, ()>>;
fn handle(&mut self, msg: Sum, _ctx: &mut Context<Self>) -> Self::Result {
async move {
let accum = critical_section::<Self, _>(async {
with_ctx(move |a: &mut Self, _| {
a.field += msg.0;
a.field
})
})
.await;
Ok(accum)
}
.interop_actor_boxed(self)
}
}
impl StreamHandler<i32> for Summator {
fn handle(&mut self, msg: i32, _ctx: &mut Context<Self>) {
self.field += msg;
}
fn finished(&mut self, _ctx: &mut Context<Self>) {
assert_eq!(self.field, 10);
System::current().stop();
}
}
#[test]
fn can_run_future() {
let mut system = System::new("test");
let res = system
.block_on(async {
let addr = Summator { field: 0 }.start();
addr.send(Sum(3)).await.unwrap().unwrap();
addr.send(Sum(4)).await
})
.unwrap();
assert_eq!(res, Ok(7));
}
#[test]
fn can_run_stream() {
let system = System::new("test");
Summator::create(|ctx| {
ctx.add_stream(futures::stream::iter(1..5));
Summator { field: 0 }
});
system.run().unwrap();
}
}