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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#![crate_name = "riker"]
#![feature(
async_await,
await_macro,
arbitrary_self_types,
bind_by_move_pattern_guards
)]
#[allow(unused_imports)]
extern crate log;
mod validate;
pub mod actor;
pub mod kernel;
pub mod system;
use std::env;
use std::fmt::Debug;
use std::fmt;
use std::any::Any;
use config::{Config, File};
use crate::actor::BasicActorRef;
pub fn load_config() -> Config {
let mut cfg = Config::new();
cfg.set_default("debug", true).unwrap();
cfg.set_default("log.level", "debug").unwrap();
cfg.set_default("log.log_format", "{date} {time} {level} [{module}] {body}").unwrap();
cfg.set_default("log.date_format", "%Y-%m-%d").unwrap();
cfg.set_default("log.time_format", "%H:%M:%S%:z").unwrap();
cfg.set_default("mailbox.msg_process_limit", 1000).unwrap();
cfg.set_default("dispatcher.pool_size", 4).unwrap();
cfg.set_default("scheduler.frequency_millis", 50).unwrap();
let path = env::var("RIKER_CONF").unwrap_or("config/riker.toml".into());
cfg.merge(File::with_name(&format!("{}", path)).required(false)).unwrap();
let path = env::var("APP_CONF").unwrap_or("config/app".into());
cfg.merge(File::with_name(&format!("{}", path)).required(false)).unwrap();
cfg
}
#[derive(Debug, Clone)]
pub struct Envelope<T: Message> {
pub sender: Option<BasicActorRef>,
pub msg: T,
}
unsafe impl<T: Message> Send for Envelope<T> {}
pub trait Message: Debug + Clone + Send + 'static {}
impl<T: Debug + Clone + Send + 'static> Message for T {}
pub struct AnyMessage {
pub one_time: bool,
pub msg: Option<Box<dyn Any + Send>>,
}
impl AnyMessage {
pub fn new<T>(msg: T, one_time: bool) -> Self
where T: Any + Message
{
AnyMessage {
one_time,
msg: Some(Box::new(msg))
}
}
pub fn take<T>(&mut self) -> Result<T, ()>
where T: Any + Message
{
if self.one_time {
match self.msg.take() {
Some(m) if m.is::<T>() => Ok(*m.downcast::<T>().unwrap()),
Some(_) => Err(()),
None => Err(())
}
} else {
match self.msg.as_ref() {
Some(ref m) if m.is::<T>() => Ok(m.downcast_ref::<T>().map(|t| t.clone()).unwrap()),
Some(_) => Err(()),
None => Err(())
}
}
}
}
impl Clone for AnyMessage {
fn clone(&self) -> Self {
panic!("Can't clone a message of type `AnyMessage`");
}
}
impl Debug for AnyMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("AnyMessage")
}
}
pub mod actors {
pub use crate::{Message, AnyMessage};
pub use crate::actor::*;
pub use crate::system::{
ActorSystem, SystemBuilder, SystemMsg,
SystemEvent, Run, Timer
};
}