1use std::any::Any;
2use std::fmt;
3
4pub use mm1_proto::Message;
5
6pub struct AnyMessage(
7 Box<dyn Any + Send + 'static>,
8 #[cfg(debug_assertions)] &'static str,
9);
10
11pub struct Priority(pub AnyMessage);
12
13impl AnyMessage {
14 pub(crate) fn new<T>(value: T) -> Self
15 where
16 T: Message,
17 {
18 Self(
19 Box::new(value),
20 #[cfg(debug_assertions)]
21 std::any::type_name::<T>(),
22 )
23 }
24
25 pub(crate) fn peek<T>(&self) -> Option<&T>
26 where
27 T: Message,
28 {
29 self.0.downcast_ref()
30 }
31
32 pub(crate) fn cast<T>(self) -> Result<T, Self>
33 where
34 T: Message,
35 {
36 self.0.downcast().map(|b| *b).map_err(|value| {
37 Self(
38 value,
39 #[cfg(debug_assertions)]
40 self.1,
41 )
42 })
43 }
44}
45
46impl fmt::Debug for AnyMessage {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 let mut s = f.debug_struct("AnyMessage");
49
50 #[cfg(debug_assertions)]
51 s.field("type_name", &self.1);
52
53 s.field("type_id", &(*self.0).type_id()).finish()
54 }
55}