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
//! The [`Message`] trait and the internal [`Envelope`] used by the bus.
use ;
use Debug;
use ;
/// Any value that can flow through a [`MessageBus`](crate::MessageBus).
///
/// Messages are erased into `Box<dyn Message>` at send time and may sit in a channel
/// across threads before being delivered, so the trait requires `'static + Send + Sync +
/// Debug`. Most implementors only need to provide [`as_any`](Message::as_any) and
/// [`as_any_mut`](Message::as_any_mut); the remaining methods carry sensible defaults.
///
/// Handlers see messages as `&dyn Message` and use [`as_any`](Message::as_any) plus
/// `downcast_ref` to recover the concrete type.
///
/// # Examples
///
/// Defining a message type and downcasting it inside a handler:
///
/// ```
/// use std::any::Any;
/// use barker::{Message, MessageHandler};
///
/// #[derive(Debug)]
/// struct Ping(&'static str);
///
/// impl Message for Ping {
/// fn as_any(&self) -> &dyn Any { self }
/// fn as_any_mut(&mut self) -> &mut dyn Any { self }
/// }
///
/// struct Printer;
/// impl MessageHandler for Printer {
/// fn call(&self, msg: &dyn Message) {
/// if let Some(ping) = msg.as_any().downcast_ref::<Ping>() {
/// println!("ping: {}", ping.0);
/// }
/// }
/// }
/// ```
// Internal wrapper that pairs a message with the instant it was enqueued.
// The timestamp is used by the drain to enforce TTL.
pub