nexus_acto_rs/actor/
process.rs1use std::fmt::Debug;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5
6use crate::actor::actor::ExtendedPid;
7use crate::actor::message::MessageHandle;
8
9pub mod process_registry;
10mod process_registry_test;
11
12#[async_trait]
13pub trait Process: Debug + Send + Sync + 'static {
14 async fn send_user_message(&self, pid: Option<&ExtendedPid>, message_handle: MessageHandle);
15 async fn send_system_message(&self, pid: &ExtendedPid, message_handle: MessageHandle);
16 async fn stop(&self, pid: &ExtendedPid);
17
18 fn set_dead(&self);
19
20 fn as_any(&self) -> &dyn std::any::Any;
22}
23
24#[derive(Debug, Clone)]
25pub struct ProcessHandle(Arc<dyn Process>);
26
27impl PartialEq for ProcessHandle {
28 fn eq(&self, other: &Self) -> bool {
29 Arc::ptr_eq(&self.0, &other.0)
30 }
31}
32
33impl Eq for ProcessHandle {}
34
35impl std::hash::Hash for ProcessHandle {
36 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
37 (self.0.as_ref() as *const dyn Process).hash(state);
38 }
39}
40
41impl ProcessHandle {
42 pub fn new_arc(process: Arc<dyn Process>) -> Self {
43 ProcessHandle(process)
44 }
45
46 pub fn new<P>(process: P) -> Self
47 where
48 P: Process + 'static, {
49 ProcessHandle(Arc::new(process))
50 }
51}
52
53#[async_trait]
54impl Process for ProcessHandle {
55 async fn send_user_message(&self, pid: Option<&ExtendedPid>, message_handle: MessageHandle) {
56 self.0.send_user_message(pid, message_handle).await;
57 }
58
59 async fn send_system_message(&self, pid: &ExtendedPid, message_handle: MessageHandle) {
60 self.0.send_system_message(pid, message_handle).await;
61 }
62
63 async fn stop(&self, pid: &ExtendedPid) {
64 self.0.stop(pid).await;
65 }
66
67 fn set_dead(&self) {
68 self.0.set_dead();
69 }
70
71 fn as_any(&self) -> &dyn std::any::Any {
72 self.0.as_any()
73 }
74}