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
use std::{sync::atomic::AtomicUsize, collections::HashMap};
use async_trait::async_trait;
use tokio::sync::{oneshot, Notify};
use crate::{Message, BoxedMessage, ActorId, WeakErasedAddr};
use lazy_static::lazy_static;
#[async_trait]
pub trait Forwarder<M: Message> {
async fn forward(&self, msg: WrappedMessage<M>);
}
pub type BoxedErasedTx = Box<dyn ErasedTx>;
pub static SHUTDOWN_SIGNAL: Notify = Notify::const_new();
pub static ACTORS_ALIVE: AtomicUsize = AtomicUsize::new(0);
lazy_static! {
pub static ref REGISTRY: tokio::sync::Mutex<HashMap<ActorId, WeakErasedAddr>> = Default::default();
}
pub struct WrappedMessage<M: Message> {
pub msg: M,
pub result_channel: Option<oneshot::Sender<anyhow::Result<M::Result>>>,
}
pub struct HandlerError {
pub inner: anyhow::Error,
pub msg_name: &'static str,
}
impl HandlerError {
pub fn new(msg_name: &'static str, err: anyhow::Error) -> Self {
Self { msg_name, inner: err }
}
pub fn msg_name(&self) -> &'static str {
self.msg_name
}
}
impl ::core::fmt::Debug for HandlerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
#[async_trait]
pub trait ErasedTx: Send + Sync {
async fn erased_send(&self, msg: BoxedMessage<()>);
}