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
use super::*;
use crate::scheduler::Shared;
use log::warn;
use slab::Slab;
pub(crate) type Last = bool;
pub(crate) type SharedOutputSlab<AGN> = Shared<Slab<Option<Callback<<AGN as Agent>::Output>>>>;
pub(crate) fn locate_callback_and_respond<AGN: Agent>(
slab: &SharedOutputSlab<AGN>,
id: HandlerId,
output: AGN::Output,
) {
let callback = {
let slab = slab.borrow();
match slab.get(id.raw_id()).cloned() {
Some(callback) => callback,
None => {
warn!("Id of handler does not exist in the slab: {}.", id.raw_id());
return;
}
}
};
match callback {
Some(callback) => callback.emit(output),
None => warn!("The Id of the handler: {}, while present in the slab, is not associated with a callback.", id.raw_id()),
}
}
pub struct Dispatcher<T>(pub(crate) Box<dyn Bridge<T>>);
impl<T> fmt::Debug for Dispatcher<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Dispatcher<_>")
}
}
impl<T> Deref for Dispatcher<T> {
type Target = dyn Bridge<T>;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<T> DerefMut for Dispatcher<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}
pub trait Dispatched: Agent + Sized + 'static {
fn dispatcher() -> Dispatcher<Self>;
}
#[doc(hidden)]
pub trait Dispatchable {}
impl<T> Dispatched for T
where
T: Agent,
<T as Agent>::Reach: Discoverer<Agent = T>,
<T as Agent>::Reach: Dispatchable,
{
fn dispatcher() -> Dispatcher<T> {
Dispatcher(Self::Reach::spawn_or_join(None))
}
}