actors_rs/
lib.rs

1#![crate_name = "actors_rs"]
2#![deny(clippy::all)]
3#![deny(clippy::pedantic)]
4#![deny(clippy::nursery)]
5#![allow(clippy::fallible_impl_from)]
6#![allow(clippy::module_name_repetitions)]
7#![allow(clippy::pub_enum_variant_names)]
8#![allow(clippy::missing_errors_doc)]
9mod validate;
10
11pub mod actor;
12pub mod kernel;
13pub mod system;
14pub use crate::actor::actor_ref::*;
15pub use crate::actor::*;
16pub use crate::system::ActorSystem;
17pub use crate::system::SystemBuilder;
18pub use crate::system::SystemEvent;
19pub use crate::system::SystemMsg;
20
21use std::any::Any;
22use std::env;
23use std::fmt;
24use std::fmt::Debug;
25
26use config::{Config, File};
27
28#[must_use]
29pub fn load_config() -> Config {
30    let mut cfg = Config::new();
31
32    cfg.set_default("debug", true).unwrap();
33    cfg.set_default("log.level", "debug").unwrap();
34    cfg.set_default("log.log_format", "{date} {time} {level} [{module}] {body}")
35        .unwrap();
36    cfg.set_default("log.date_format", "%Y-%m-%d").unwrap();
37    cfg.set_default("log.time_format", "%H:%M:%S%:z").unwrap();
38    cfg.set_default("mailbox.msg_process_limit", 1000).unwrap();
39    cfg.set_default("dispatcher.pool_size", 4).unwrap();
40    cfg.set_default("scheduler.frequency_millis", 50).unwrap();
41
42    // load the system config
43    // riker.toml contains settings for anything related to the actor framework and its modules
44    let path = env::var("RIKER_CONF").unwrap_or_else(|_| "config/riker.toml".into());
45    cfg.merge(File::with_name(&path).required(false)).unwrap();
46
47    // load the user application config
48    // app.toml or app.yaml contains settings specific to the user application
49    let path = env::var("APP_CONF").unwrap_or_else(|_| "config/app".into());
50    cfg.merge(File::with_name(&path).required(false)).unwrap();
51    cfg
52}
53
54/// Wraps message and sender
55#[derive(Debug, Clone)]
56pub struct Envelope<T: Message> {
57    pub sender: Option<BasicActorRef>,
58    pub msg: T,
59}
60
61unsafe impl<T: Message> Send for Envelope<T> {}
62
63pub trait Message: Debug + Clone + Send + 'static {}
64impl<T: Debug + Clone + Send + 'static> Message for T {}
65
66pub struct AnyMessage {
67    pub one_time: bool,
68    pub msg: Option<Box<dyn Any + Send>>,
69}
70
71impl AnyMessage {
72    pub fn new<T>(msg: T, one_time: bool) -> Self
73    where
74        T: Any + Message,
75    {
76        Self {
77            one_time,
78            msg: Some(Box::new(msg)),
79        }
80    }
81
82    pub fn take<T>(&mut self) -> Result<T, ()>
83    where
84        T: Any + Message,
85    {
86        if self.one_time {
87            match self.msg.take() {
88                Some(m) => {
89                    if m.is::<T>() {
90                        Ok(*m.downcast::<T>().unwrap())
91                    } else {
92                        Err(())
93                    }
94                }
95                None => Err(()),
96            }
97        } else {
98            match self.msg.as_ref() {
99                Some(m) if m.is::<T>() => Ok(m.downcast_ref::<T>().cloned().unwrap()),
100                Some(_) | None => Err(()),
101            }
102        }
103    }
104}
105
106impl Clone for AnyMessage {
107    fn clone(&self) -> Self {
108        panic!("Can't clone a message of type `AnyMessage`");
109    }
110}
111
112impl Debug for AnyMessage {
113    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114        f.write_str("AnyMessage")
115    }
116}