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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::{any::TypeId, fmt::Debug, sync::Arc};
use futures::lock::Mutex;
use crate::{
core::dyn_var::DynVar, event::EventRegister, interface::BusInterface, util::{GeneralRequirements, dyn_debug::DynDebug},
};
#[allow(clippy::module_name_repetitions)]
pub trait BusStop {
fn registered_handlers(h: EventRegister<Self>) -> EventRegister<Self>;
}
mod seal {
pub trait Sealed {}
}
#[async_trait]
pub trait BusStopMech: Sized + seal::Sealed {
async unsafe fn handle_raw_event(
self,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> (Self, DynVar);
fn relevant(&self, event_tag_id: TypeId) -> bool;
}
impl<T> seal::Sealed for T where T: BusStop + Debug + Sized + Send + Sync + 'static {}
#[async_trait]
impl<T> BusStopMech for T
where
T: BusStop + Debug + Sized + Send + Sync + 'static,
{
async unsafe fn handle_raw_event(
mut self,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> (Self, DynVar) {
let mut handlers = T::registered_handlers(EventRegister::new())
.handlers
.into_iter()
.filter(|rh| rh.0 == event_tag_id)
.collect::<Vec<_>>();
debug_assert!(handlers.len() == 1);
let handler = handlers.remove(0);
let mut dyn_self = DynVar::new(self);
let fut = handler.1.call(&mut dyn_self, event, interface);
let res = fut.await;
let typed_self = dyn_self.try_to_unchecked::<Self>();
(typed_self, res)
}
fn relevant(&self, event_tag_id: TypeId) -> bool {
let handlers = T::registered_handlers(EventRegister::new())
.handlers
.into_iter()
.filter(|rh| rh.0 == event_tag_id)
.collect::<Vec<_>>();
debug_assert!(handlers.len() <= 1);
!handlers.is_empty()
}
}
pub struct BusStopMechContainer<B: BusStopMech + GeneralRequirements + Send + Sync + 'static> {
inner: Option<B>,
}
impl<B: BusStopMech + GeneralRequirements + Send + Sync + 'static> BusStopMechContainer<B> {
pub const fn new(inner: B) -> Self {
Self { inner: Some(inner) }
}
pub async unsafe fn handle_raw_event(
&mut self,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> DynVar {
let moved_self = self.inner.take().unwrap();
let (moved_self, res) = moved_self
.handle_raw_event(event_tag_id, event, interface)
.await;
self.inner = Some(moved_self);
res
}
pub fn relevant(&mut self, event_tag_id: TypeId) -> bool {
self.inner.as_mut().unwrap().relevant(event_tag_id)
}
pub fn debug(&self) -> &dyn Debug {
self.inner.as_dbg()
}
}
impl<B: BusStopMech + GeneralRequirements + Send + Sync + 'static> seal::Sealed for BusStopMechContainer<B> {}
#[async_trait]
#[doc(hidden)]
pub trait DynBusStopContainer: seal::Sealed {
async unsafe fn handle_raw_event(
&mut self,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> DynVar;
fn relevant(&mut self, event_tag_id: TypeId) -> bool;
fn debug(&self) -> &dyn Debug;
}
#[async_trait]
impl<B: BusStopMech + GeneralRequirements + Send + Sync + 'static> DynBusStopContainer
for BusStopMechContainer<B>
{
async unsafe fn handle_raw_event(
&mut self,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> DynVar {
Self::handle_raw_event(self, event_tag_id, event, interface).await
}
fn relevant(&mut self, event_tag_id: TypeId) -> bool {
Self::relevant(self, event_tag_id)
}
fn debug(&self) -> &dyn Debug {
self.debug()
}
}
pub trait BusStopReq: DynBusStopContainer + GeneralRequirements {}
impl<T: DynBusStopContainer + GeneralRequirements> BusStopReq for T {}
pub struct BusStopContainer {
pub inner: Mutex<Box<dyn BusStopReq + Send + Sync + 'static>>,
}
impl BusStopContainer {
pub fn new(inner: Box<dyn BusStopReq + Send + Sync + 'static>) -> Self {
Self { inner: Mutex::new(inner) }
}
pub async unsafe fn handle_raw_event(
self: Arc<Self>,
event_tag_id: TypeId,
event: DynVar,
interface: BusInterface,
) -> DynVar {
let ret = self
.inner
.try_lock()
.unwrap()
.handle_raw_event(event_tag_id, event, interface)
.await;
ret
}
pub fn relevant(&mut self, event_tag_id: TypeId) -> bool {
self.inner.try_lock().unwrap().relevant(event_tag_id)
}
pub fn debug(&mut self) -> &dyn Debug {
let i = self.inner.get_mut();
(**i).as_dbg()
}
}
impl Debug for BusStopContainer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BusStopContainer")
.field("inner", self.inner.try_lock().unwrap().as_dbg())
.finish()
}
}