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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! Core trait abstractions for pub/sub messaging with typed and untyped interfaces.
//!
//! This module provides a set of traits that abstract over different messaging
//! backends and transport layers. The traits are organized into two main
//! categories:
//!
//! ## Typed Traits
//!
//! These traits work with strongly-typed items:
//! - [`PubTrait`]: Publish strongly-typed items that implement [`serde::Serialize`].
//! - [`SubTrait`]: Subscribe to a stream of strongly-typed items that implement
//! [`serde::de::DeserializeOwned`].
//! - [`AckTrait`]: Acknowledge receipt of a message.
//! - [`UnSubTrait`]: Cancel a subscription.
//!
//! ## Untyped (Context) Traits
//!
//! These traits work with raw byte payloads and topics:
//! - [`PubCtxTrait`]: Publish raw byte payloads to specific topics.
//! - [`SubCtxTrait`]: Subscribe to a stream of raw byte messages from a topic.
//!
//! ## Additional Traits
//!
//! - [`SubOptTrait`]: Configure subscription options like auto-acknowledgment and format.
//!
//! # Dynamic Dispatch Usage
//!
//! These traits are designed to work seamlessly with dynamic dispatch (trait objects),
//! enabling flexible, runtime polymorphism. Here are common patterns:
//!
//! ## Publishing with Dynamic Dispatch
//!
//! ```rust
//! use std::sync::Arc;
//! use object_transfer::traits::PubTrait;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct MyMessage {
//! data: String,
//! }
//!
//! async fn send_message(publisher: Arc<dyn PubTrait<Item = MyMessage>>) {
//! let msg = MyMessage { data: "hello".to_string() };
//! publisher.publish(&msg).await.ok();
//! }
//! ```
//!
//! ## Subscribing with Dynamic Dispatch
//!
//! ```rust
//! use std::sync::Arc;
//! use object_transfer::traits::{SubTrait, AckTrait};
//! use serde::Deserialize;
//! use futures::stream::StreamExt;
//!
//! #[derive(Deserialize)]
//! struct MyMessage {
//! data: String,
//! }
//!
//! async fn receive_messages(
//! subscriber: Arc<dyn SubTrait<Item = MyMessage>>
//! ) {
//! if let Ok(mut stream) = subscriber.subscribe().await {
//! while let Some(Ok((msg, ack))) = stream.next().await {
//! // Process the message
//! let _ = ack.ack().await;
//! }
//! }
//! }
//! ```
//!
//! ## Working with Raw Payloads
//!
//! For scenarios requiring lower-level control, use context traits:
//!
//! ```rust
//! use std::sync::Arc;
//! use object_transfer::traits::PubCtxTrait;
//! use bytes::Bytes;
//!
//! async fn send_raw(ctx: Arc<dyn PubCtxTrait>) {
//! let payload = Bytes::from("raw data");
//! ctx.publish("topic/name", payload).await.ok();
//! }
//! ```
//!
//! # Static Dispatch Usage
//!
//! For maximum performance and compile-time guarantees, use static dispatch with
//! generic trait bounds. This approach leverages monomorphization to eliminate
//! runtime overhead and enable inlining.
//!
//! ## Publishing with Static Dispatch
//!
//! ```rust
//! use object_transfer::traits::PubTrait;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct MyMessage {
//! data: String,
//! }
//!
//! async fn send_message<P: PubTrait<Item = MyMessage>>(publisher: &P) {
//! let msg = MyMessage { data: "hello".to_string() };
//! publisher.publish(&msg).await.ok();
//! }
//! ```
//!
//! ## Subscribing with Static Dispatch
//!
//! ```rust
//! use object_transfer::traits::{SubTrait, AckTrait};
//! use serde::Deserialize;
//! use futures::stream::StreamExt;
//!
//! #[derive(Deserialize)]
//! struct MyMessage {
//! data: String,
//! }
//!
//! async fn receive_messages<S: SubTrait<Item = MyMessage>>(subscriber: &S) {
//! if let Ok(mut stream) = subscriber.subscribe().await {
//! while let Some(Ok((msg, ack))) = stream.next().await {
//! // Process the message
//! let _ = ack.ack().await;
//! }
//! }
//! }
//! ```
//!
//! ## Generic Over Multiple Trait Implementations
//!
//! Static dispatch excels when working with multiple trait implementations:
//!
//! ```rust
//! use object_transfer::traits::{PubTrait, SubTrait, AckTrait};
//! use serde::{Serialize, Deserialize};
//! use futures::stream::StreamExt;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Event {
//! id: u64,
//! }
//!
//! async fn relay_events<P, S>(publisher: &P, subscriber: &S)
//! where
//! P: PubTrait<Item = Event>,
//! S: SubTrait<Item = Event>,
//! {
//! if let Ok(mut stream) = subscriber.subscribe().await {
//! while let Some(Ok((event, ack))) = stream.next().await {
//! publisher.publish(&event).await.ok();
//! let _ = ack.ack().await;
//! }
//! }
//! }
//! ```
//!
use Bytes;
use Arc;
use async_trait;
use BoxStream;
use ;
use crate;
use crateFormat;
use crateTestEntity;
use automock;
/// Abstraction for publishing typed items.
///
/// Implementors handle serialization and delivery to a concrete backend.
/// Acknowledge receipt of a message.
/// Subscription interface returning a stream of decoded items and ack handles.
/// Allows canceling a subscription.
/// Context capable of publishing raw byte payloads.
/// Context capable of producing a stream of raw messages with ack handles.
/// Options that influence subscription behavior such as auto-ack and format.