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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Asynchronous inter-component communication library
//!
//! ## Example
//!
//! ```rust
//! use intercomm::{broadcast, notification, request};
//!
//! intercomm::declare! {
//! request Sum((i32, i32)) -> i32;
//! request Mul((i32, i32)) -> i32;
//!
//! notification[2] Ready(());
//! broadcast[1] Close(());
//! }
//!
//! async fn sum_listener() {
//! let mut listener = request::listen::<Sum>().await.expect("Sum listen twice");
//! let mut close = broadcast::subscribe::<Close>().await;
//! Ready::notify(()).await.expect("Cannot send Ready");
//!
//! loop {
//! tokio::select! {
//! _ = close.recv() => {
//! listener.close().await;
//! close.close().await;
//! return;
//! }
//! _ = listener.accept(|(a, b)| async move {
//! println!("Sum requested with: ({}, {})", a, b);
//! a + b
//! }) => {}
//! }
//! }
//! }
//!
//! async fn mul_listener() {
//! let mut listener = request::listen::<Mul>().await.expect("Mul listen twice");
//! let mut close = broadcast::subscribe::<Close>().await;
//! Ready::notify(()).await.expect("Cannot send Ready");
//!
//! loop {
//! tokio::select! {
//! _ = close.recv() => {
//! listener.close().await;
//! close.close().await;
//! return;
//! }
//! _ = listener.accept(|(a, b)| async move {
//! println!("Mul requested with: ({}, {})", a, b);
//! a * b
//! }) => {}
//! }
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! let mut ready = notification::subscribe::<Ready>()
//! .await
//! .expect("Ready subscribed twice");
//! let sum_join = tokio::spawn(sum_listener());
//! let mul_join = tokio::spawn(mul_listener());
//! for _ in 0..2 {
//! ready.recv().await;
//! }
//! ready.close().await;
//!
//! let sum = Sum::request((5, 10)).await.expect("Cannot request Sum");
//! println!("5 + 10 = {}", sum);
//!
//! let mul = Mul::request((5, 10)).await.expect("Cannot request Mul");
//! println!("5 * 10 = {}", mul);
//!
//! Close::notify(()).await;
//! sum_join.await.expect("sum_listener panicked");
//! mul_join.await.expect("mul_listener panicked");
//! }
//! ```
/// Declare types for
/// [Broadcast](crate::broadcast::Broadcast),
/// [Notification](crate::notification::Notification),
/// [Request](crate::request::Request)
///
/// ## Syntax
///
/// `<visibility>? broadcast[<buffer size>] <name>(<payload type>);` \
/// `<visibility>? notification[<buffer size>] <name>(<payload type>);` \
/// `<visibility>? request[<buffer size>] <name>(<payload type>) -> <response type>;` \
///
/// `<buffer size>` is optional for `notification` & `request` and required for `broadcast`
///
///
/// ## Example
///
/// ```rust
/// intercomm::declare! {
/// /// B1 broadcast
/// broadcast[4] B1(i32);
/// /// B2 broadcast
/// pub(crate) broadcast[8] B2(i32);
/// /// B3 broadcast
/// pub broadcast[16] B3(i32);
///
/// /// N1 notification
/// notification N1(i32);
/// /// N2 notification
/// pub(crate) notification[1] N2(i32);
/// /// N3 notification
/// pub notification[4] N3(i32);
///
/// /// R1 request
/// request R1((i32, i32)) -> i32;
/// /// R2 request
/// pub(crate) request[4] R2((i32, i32)) -> i32;
/// /// R3 request
/// pub request[4] R3((i32, i32)) -> i32;
/// }
/// ```
$cratedeclare!;
};
=> ;
=> ;
=> ;
=> ;
}