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