co-actor 0.1.0

Very lightweight actor abstraction over tokio channels.
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 1io BRANDGUARDIAN GmbH

use super::ActorHandle;
use crate::{epic::actions::ActionsEpic, TaskSpawner};
use co_primitives::Tags;
use futures::{
	pin_mut,
	stream::{self, BoxStream, Empty},
	Stream, StreamExt,
};
use std::{
	any::type_name,
	fmt::Debug,
	marker::{PhantomData, Send},
	sync::Arc,
};
use tokio_util::sync::CancellationToken;

mod action_dispatch;
mod actions;

pub use action_dispatch::ActionDispatch;
pub use actions::Actions;

/// Epic.
///
/// Defines side effects for actions which will produce other actions over time.
pub trait Epic<A, S, C> {
	/// Run the epic.
	///
	/// # Arguments
	/// - `state`: The state after the action has been applied.
	fn epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + Send + 'static>;

	/// Whether this epic is terminated and should be not be called futher.
	fn is_terminated(&self) -> bool {
		false
	}
}

/// Fn impl for epics.
impl<A, S, C, O, F> Epic<A, S, C> for F
where
	O: Stream<Item = Result<A, anyhow::Error>> + Send + 'static,
	F: FnMut(&Actions<A, S, C>, &A, &S, &C) -> Option<O>,
{
	fn epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + Send + 'static> {
		self(actions, action, state, context)
	}
}

pub trait EpicExt<A, S, C>: Epic<A, S, C> {
	/// Join two Epics.
	///
	/// # Notes
	/// This will join on the stack.
	/// If you want to join dozens of epics the heap should be used.
	/// See: [`MergeEpic`].
	fn join<E>(self, other: E) -> JoinEpic<Self, E>
	where
		Self: Sized,
		A: Send + 'static,
	{
		JoinEpic(self, other)
	}

	fn switch(self) -> SwitchEpic<Self>
	where
		Self: Sized + Send + 'static,
	{
		SwitchEpic(self, None)
	}

	fn boxed(self) -> BoxEpic<'static, A, S, C>
	where
		Self: Sized + Send + 'static,
	{
		Box::new(self)
	}
}
impl<T, A, S, C> EpicExt<A, S, C> for T where
	T: Epic<A, S, C> + ?Sized + Send + 'static /* A: Send + Clone + 'static,
	                                            * S: Send + Clone + 'static,
	                                            * C: Send + Clone + 'static, */
{
}

pub type BoxEpic<'a, A, S, C> = Box<dyn BoxStreamEpic<A, S, C> + Send + 'a>;

/// Dynamic dispatchable epic.
pub trait BoxStreamEpic<A, S, C> {
	fn box_epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<BoxStream<'static, Result<A, anyhow::Error>>>;

	fn box_is_terminated(&self) -> bool;
}
impl<T, A, S, C> BoxStreamEpic<A, S, C> for T
where
	T: Epic<A, S, C>,
{
	fn box_epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<BoxStream<'static, Result<A, anyhow::Error>>> {
		self.epic(actions, action, state, context).map(|stream| stream.boxed())
	}

	fn box_is_terminated(&self) -> bool {
		self.is_terminated()
	}
}

/// Epic runtime to be uses as actor state.
/// Expected to be called after the message has been applied to the state.
pub struct EpicRuntime<M, A, S, C> {
	actions: Actions<A, S, C>,
	epic: BoxEpic<'static, A, S, C>,
	error: Arc<dyn Fn(anyhow::Error) -> Option<A> + Sync + Send + 'static>,
	_actor: PhantomData<fn(M, A, S, C)>,
}
impl<M, A, S, C> EpicRuntime<M, A, S, C>
where
	M: Send + 'static,
	A: Clone + Send + 'static + Into<M>,
	S: Send + 'static,
	C: Send + 'static,
{
	pub fn new(
		epic: impl EpicExt<A, S, C> + Send + 'static,
		error: impl Fn(anyhow::Error) -> Option<A> + Sync + Send + 'static,
	) -> Self {
		let actions_epic = ActionsEpic::default();
		let actions = actions_epic.actions();
		Self { actions, epic: epic.join(actions_epic).boxed(), _actor: Default::default(), error: Arc::new(error) }
	}

	pub fn handle(&mut self, spawner: &TaskSpawner, actor: &ActorHandle<M>, action: &A, state: &S, context: &C) {
		let stream = self.epic.box_epic(&self.actions, action, state, context);
		if let Some(stream) = stream {
			let actor = actor.clone();
			let error = self.error.clone();
			spawner.spawn_named(type_name::<A>(), async move {
				let stream = stream.take_until(actor.closed());
				pin_mut!(stream);
				while let Some(action) = stream.next().await {
					match action {
						Ok(action) => {
							actor.dispatch(action).ok();
						},
						Err(err) => {
							if let Some(action) = (error)(err) {
								actor.dispatch(action).ok();
							}
						},
					}
				}
			});
		}
	}
}
impl<M, A, S, C> Debug for EpicRuntime<M, A, S, C> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("EpicRuntime").field("_actor", &self._actor).finish()
	}
}

/// Joins two epics into one.
pub struct JoinEpic<E1, E2>(E1, E2);
impl<E1, E2, A, S, C> Epic<A, S, C> for JoinEpic<E1, E2>
where
	A: Send + 'static,
	E1: Epic<A, S, C>,
	E2: Epic<A, S, C>,
{
	fn epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + Send + 'static> {
		let s0 = if self.0.is_terminated() { None } else { self.0.epic(actions, action, state, context) };
		let s1 = if self.1.is_terminated() { None } else { self.1.epic(actions, action, state, context) };
		let s0 = async_stream::stream! {
			if let Some(stream) = s0 {
				for await item in stream {
					yield item;
				}
			}
		};
		let s1 = async_stream::stream! {
			if let Some(stream) = s1 {
				for await item in stream {
					yield item;
				}
			}
		};
		Some(futures::stream::select(s0, s1))
	}
}

/// Merge BoxEpic into one.
pub struct MergeEpic<A, S, C>(Vec<BoxEpic<'static, A, S, C>>);
impl<A, S, C> Default for MergeEpic<A, S, C> {
	fn default() -> Self {
		Self(Default::default())
	}
}
impl<A, S, C> MergeEpic<A, S, C> {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn join(mut self, epic: impl EpicExt<A, S, C> + Send + 'static) -> Self {
		self.0.push(epic.boxed());
		self
	}

	pub fn push(&mut self, epic: impl EpicExt<A, S, C> + Send + 'static) {
		self.0.push(epic.boxed());
	}

	pub fn box_push(&mut self, epic: BoxEpic<'static, A, S, C>) {
		self.0.push(epic);
	}

	pub fn drain_terminated(&mut self) {
		self.0.retain(|epic| !epic.box_is_terminated());
	}
}
impl<A, S, C> Epic<A, S, C> for MergeEpic<A, S, C>
where
	A: Send + 'static,
{
	fn epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + Send + 'static> {
		let streams: Vec<_> = self
			.0
			.iter_mut()
			.filter(|epic| !epic.box_is_terminated())
			.filter_map(|epic| epic.box_epic(actions, action, state, context))
			.collect();
		if !streams.is_empty() {
			Some(stream::iter(streams).flatten_unordered(None))
		} else {
			None
		}
	}
}

/// Trace actions and state as debug messages.
pub struct TracingEpic(Tags);
impl TracingEpic {
	pub fn new(tags: Tags) -> Self {
		Self(tags)
	}
}
impl<A, S, C> Epic<A, S, C> for TracingEpic
where
	A: Debug + Send + 'static,
	S: Debug + Send + 'static,
{
	fn epic(
		&mut self,
		_actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		_context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + 'static> {
		tracing::debug!(?action, ?state, tags = ?self.0, "action");
		Option::<Empty<_>>::None
	}
}

/// Only allow to run epic once.
/// Once the epic returns another stream the previous will be dropped.
pub struct SwitchEpic<E>(E, Option<CancellationToken>);
impl<E, A, S, C> Epic<A, S, C> for SwitchEpic<E>
where
	E: Epic<A, S, C>,
	A: Debug + Send + 'static,
	S: Debug + Send + 'static,
{
	fn epic(
		&mut self,
		actions: &Actions<A, S, C>,
		action: &A,
		state: &S,
		context: &C,
	) -> Option<impl Stream<Item = Result<A, anyhow::Error>> + 'static> {
		let next = self.0.epic(actions, action, state, context);
		match next {
			Some(stream) => {
				// cancel previous
				if let Some(cancel) = self.1.take() {
					cancel.cancel();
				}

				// create next
				let token = CancellationToken::new();
				self.1 = Some(token.clone());
				Some(stream.take_until(token.cancelled_owned()))
			},
			None => None,
		}
	}
}

#[cfg(test)]
mod tests {
	use crate::{epic::Actions, Epic, EpicExt};
	use futures::{stream, Stream, TryStreamExt};

	#[derive(Debug, Clone, PartialEq)]
	enum TestAction {
		Hello,
		World,
	}
	struct Test {}
	impl Epic<TestAction, (), ()> for Test {
		fn epic(
			&mut self,
			_actions: &Actions<TestAction, (), ()>,
			action: &TestAction,
			_state: &(),
			_context: &(),
		) -> Option<impl Stream<Item = Result<TestAction, anyhow::Error>> + Send + 'static> {
			match action {
				TestAction::Hello => Some(stream::once(async { Ok(TestAction::World) })),
				_ => None,
			}
		}
	}

	#[tokio::test]
	async fn test_hello() {
		let actions = Actions::default();
		let mut epic = Test {};
		let result: Vec<TestAction> = epic
			.epic(&actions, &TestAction::Hello, &(), &())
			.expect("a stream")
			.try_collect()
			.await
			.expect("no error");
		assert_eq!(result, vec![TestAction::World]);
	}

	#[tokio::test]
	async fn test_fn_epic() {
		fn test(
			_actions: &Actions<TestAction, (), ()>,
			action: &TestAction,
			_state: &(),
			_context: &(),
		) -> Option<impl Stream<Item = Result<TestAction, anyhow::Error>> + Send + 'static> {
			match action {
				TestAction::Hello => Some(stream::once(async { Ok(TestAction::World) })),
				_ => None,
			}
		}
		let actions = Actions::default();
		let result: Vec<TestAction> = test
			.epic(&actions, &TestAction::Hello, &(), &())
			.expect("a stream")
			.try_collect()
			.await
			.expect("no error");
		assert_eq!(result, vec![TestAction::World]);
	}

	#[tokio::test]
	async fn test_box_epic() {
		let actions = Actions::default();
		let mut epic = Test {}.boxed();
		let result: Vec<TestAction> = epic
			.box_epic(&actions, &TestAction::Hello, &(), &())
			.expect("a stream")
			.try_collect()
			.await
			.expect("no error");
		assert_eq!(result, vec![TestAction::World]);
	}
}