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
#![cfg_attr(test, deny(warnings))]
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::de::DeserializeOwned;
use serde::Serialize;
pub mod spawn;
pub mod file;
pub trait Reactor: Sized {
type Peer: std::net::ToSocketAddrs;
type Message: Serialize + DeserializeOwned;
type Config;
type Error;
const PERIODIC_INTERVAL: Option<Duration> = None;
fn start(config: Self::Config) -> Result<Self, Self::Error>;
fn receive(
&mut self,
from: Self::Peer,
msg: Self::Message,
) -> Vec<(Self::Peer, Self::Message)>;
fn periodic(&mut self) -> Vec<(Self::Peer, Self::Message)> {
unimplemented!()
}
}
thread_local! {
pub static CONTEXT: RefCell<Arc<Context>> = RefCell::new(Arc::new(Context::default()));
}
fn context() -> Arc<Context> {
CONTEXT.with(|c| c.borrow().clone())
}
fn set_context(context: Arc<Context>) {
CONTEXT.with(|c| *c.borrow_mut() = context);
}
mod context;
use self::context::Context;
pub use self::context::{
now, seed, set_seed, set_time, sleep, thread_rng, Rand,
};