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
289
290
//! Backend abstractions used by the generic ring.
//!
//! This module defines the core trait contracts that all backends must implement,
//! including the [`Backend`] trait for pluggable backends and [`AnyBackend`] for
//! enum-based zero-cost dispatch.
use crate;
use crate;
use crateRingConfig;
use Sender;
/// The active backend implementation.
/// Boxed backend trait object (legacy, kept for backwards compatibility).
///
/// This type alias exists for backwards compatibility with code that uses
/// `Box<dyn Backend>` directly. New code should prefer [`AnyBackend`] for
/// better performance through enum dispatch.
pub type BoxedBackend = ;
/// Backend submission payload.
///
/// Carries an operation descriptor from the ring to the backend along with
/// a stable request identifier used for completion routing and cancellation.
/// Backend completion payload.
///
/// Delivered from the backend to the ring when an operation completes,
/// successfully or with an error.
/// Cancellation request handle.
///
/// Passed to [`Backend::cancel`] to request cancellation of an in-flight operation.
/// Pluggable backend interface.
///
/// # Contract
///
/// All implementations must satisfy these behavioral contracts:
///
/// ## Thread Safety
/// - [`Backend`] extends [`Send`] + [`Sync`]: implementations must be safe to
/// share between threads and call concurrently from multiple threads.
///
/// ## Submission (`submit`)
/// - Must be **non-blocking**: the method should return immediately after
/// queueing the work, not wait for completion.
/// - Must accept submissions until [`Backend::shutdown`] is called.
/// - Must return an error if the backend is unable to accept new work.
/// - The backend is responsible for eventually calling the completion callback
/// for every successfully submitted operation.
///
/// ## Cancellation (`cancel`)
/// - Best-effort cancellation of in-flight work identified by request id.
/// - Cancellation may race with completion: both outcomes are valid.
/// - Returns an error if the target request id is unknown or already complete.
///
/// ## Shutdown (`shutdown`)
/// - Must be **idempotent**: calling multiple times should not panic or error.
/// - Must gracefully wait for all in-flight operations to complete or cancel.
/// - After shutdown returns, no new completions should be delivered.
/// - Should signal any worker threads to exit and wait for them to join.
/// Enum dispatch backend for eliminating vtable indirection on the hot path.
///
/// # Enum Dispatch vs Vtable
///
/// This enum uses **enum dispatch** (also known as "inline dispatch") to avoid
/// vtable lookups for the built-in backends:
///
/// | Approach | Pros | Cons |
/// |----------|------|------|
/// | **Enum dispatch** (`AnyBackend`) | Zero-cost: no vtable indirection, better inlining, cache-friendly | Closed set of variants |
/// | **Vtable** (`Box<dyn Backend>`) | Open for extension, dynamic plugin loading | Virtual call overhead, harder to inline |
///
/// ## Migration Path: Adding a New Backend (e.g., kqueue)
///
/// 1. **Create a new crate**: `kqueue/Cargo.toml`
/// 2. **Implement the trait**: `impl Backend for KqueueBackend { ... }`
/// 3. **Add the variant** (optional, for zero-cost):
/// ```rust
/// // In core/src/backend.rs
/// pub enum AnyBackend {
/// Boxed(BoxedBackend),
/// Kqueue(wireshift_kqueue::KqueueBackend), // Zero-cost variant
/// }
/// ```
/// 4. **Feature-flag it** in the workspace Cargo.toml:
/// ```toml
/// [features]
/// kqueue = ["dep:wireshift-kqueue"]
/// ```
///
/// ## Send + Sync Guarantees
///
/// `AnyBackend` is `Send + Sync` because every variant contains only `Send + Sync`
/// types. The compiler automatically derives these traits - no `unsafe impl` needed.
/// If a future variant breaks this contract, the compiler will reject it.
/// Backend construction hook used by [`crate::Ring`].
///
/// Implementations provide a way to create backends with specific configurations.
/// The factory pattern allows rings to be generic over backend selection while
/// still supporting configuration-specific backend creation.
///
/// # Example
///
/// ```rust,ignore
/// #[derive(Default)]
/// struct MyBackendFactory;
///
/// impl BackendFactory for MyBackendFactory {
/// fn create(
/// &self,
/// config: &RingConfig,
/// completion_tx: Sender<BackendCompletion>,
/// ) -> Result<BoxedBackend> {
/// Ok(Box::new(MyBackend::new(config, completion_tx)?))
/// }
/// }
/// ```
/// Returns a shared error when a backend worker channel disconnects.