shakespeare/
lib.rs

1#![warn(missing_copy_implementations)]
2#![warn(missing_debug_implementations)]
3//#![warn(missing_docs)]
4#![warn(unused)]
5#![warn(nonstandard_style)]
6#![warn(clippy::pedantic)]
7#![warn(clippy::missing_panics_doc)]
8#![warn(clippy::dbg_macro)]
9//#![warn(unused_crate_dependencies)]
10#![forbid(unsafe_code)]
11#![forbid(clippy::todo)]
12#![forbid(clippy::unimplemented)]
13
14use std::any::Any;
15use std::future::Future;
16use std::sync::Arc;
17
18#[doc(hidden)]
19pub use ::tokio as tokio_export;
20use futures::{Stream, StreamExt};
21pub use shakespeare_macro::{actor, performance, role};
22#[doc(hidden)]
23pub use tokio::TokioUnbounded;
24
25mod core;
26mod tokio;
27
28pub use core::{
29	ActorHandle, ActorOutcome, ActorShell, ActorSpawn, Channel, Role, RoleReceiver, RoleSender,
30};
31
32#[doc(hidden)]
33pub type Role2Payload<R> = <R as Role>::Payload;
34#[doc(hidden)]
35pub type Role2Receiver<R> = <<R as Role>::Channel as Channel>::Receiver;
36#[doc(hidden)]
37pub type Role2Sender<R> = <<R as Role>::Channel as Channel>::Sender;
38#[doc(hidden)]
39pub type Role2SendError<R> = <Role2Sender<R> as RoleSender<<R as Role>::Payload>>::Error;
40
41#[doc(hidden)]
42pub fn catch_future<T>(fut: T) -> impl Future<Output = Result<T::Output, Box<dyn Any + Send>>>
43where
44	T: Future,
45{
46	futures::future::FutureExt::catch_unwind(std::panic::AssertUnwindSafe(fut))
47}
48
49pub fn add_stream<R, S>(actor: Arc<R>, stream: S)
50where
51	R: Role + ?Sized,
52	S: Stream + Send + 'static,
53	R::Payload: From<S::Item>,
54	<S as Stream>::Item: Send,
55{
56	tokio_export::spawn(async move {
57		stream
58			.for_each(|msg| async {
59				let _ = actor.clone_sender().send(msg.into()).await;
60			})
61			.await;
62	});
63}
64
65pub fn add_future<R, F>(actor: Arc<R>, fut: F)
66where
67	R: Role + ?Sized,
68	F: Future + Send + 'static,
69	R::Payload: From<F::Output>,
70{
71	tokio_export::spawn(async move {
72		let actor = actor;
73		let _ = actor.clone_sender().send(fut.await.into()).await;
74	});
75}