trash_parallelism 0.1.102

Azzybana Raccoon's comprehensive parallelism library.
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/// Channel parsing and aggregation utilities.
///
/// This module provides efficient message parsing using memchr, channel aggregation
/// for combining multiple inputs, batching channels, and filtered channels.
///
/// # Examples
///
/// Fast message parsing:
/// ```rust
/// use trash_utilities::channels::parsers::FastMessageParser;
///
/// let parser = FastMessageParser::new('\n');
/// let buffer = b"msg1\nmsg2\nmsg3";
/// let messages = parser.parse_messages(buffer);
/// assert_eq!(messages.len(), 3);
/// ```
// Standard library imports
use std::sync::Arc;

// External crate imports
use memchr::memchr;
use parking_lot::Mutex;

/// Efficient message parser using memchr
///
/// Parses messages from byte buffers using fast delimiter-based splitting.
/// Zero-copy where possible for high performance.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::parsers::FastMessageParser;
///
/// let parser = FastMessageParser::new('\n');
/// let data = b"line1\nline2\nline3";
/// let messages = parser.parse_messages(data);
/// assert_eq!(messages, vec![b"line1", b"line2", b"line3"]);
/// ```
pub struct FastMessageParser {
    delimiter: u8,
}

impl FastMessageParser {
    /// Create a new parser
    ///
    /// # Parameters
    ///
    /// * `delimiter` - The character to split messages on.
    ///
    /// # Returns
    ///
    /// A new `FastMessageParser` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::FastMessageParser;
    ///
    /// let parser = FastMessageParser::new(',');
    /// ```
    #[must_use]
    pub fn new(delimiter: char) -> Self {
        Self {
            delimiter: delimiter as u8,
        }
    }

    /// Parse messages from a buffer (zero-copy slicing)
    ///
    /// Splits the buffer into message slices using the delimiter.
    ///
    /// # Parameters
    ///
    /// * `buffer` - The byte buffer to parse.
    ///
    /// # Returns
    ///
    /// A vector of byte slices, each representing a message.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::FastMessageParser;
    ///
    /// let parser = FastMessageParser::new('|');
    /// let buffer = b"msg1|msg2|msg3";
    /// let messages = parser.parse_messages(buffer);
    /// assert_eq!(messages, vec![&b"msg1"[..], &b"msg2"[..], &b"msg3"[..]]);
    /// ```
    #[must_use]
    pub fn parse_messages<'a>(&self, buffer: &'a [u8]) -> Vec<&'a [u8]> {
        let mut messages = Vec::new();
        let mut start = 0;

        while let Some(pos) = memchr(self.delimiter, &buffer[start..]) {
            let abs_pos = start + pos;
            if abs_pos > start {
                messages.push(&buffer[start..abs_pos]);
            }
            start = abs_pos + 1;
        }

        if start < buffer.len() {
            messages.push(&buffer[start..]);
        }

        messages
    }

    /// Parse JSON messages efficiently
    ///
    /// Parses messages and deserializes each as JSON.
    ///
    /// # Parameters
    ///
    /// * `buffer` - The byte buffer containing JSON messages.
    ///
    /// # Returns
    ///
    /// A vector of parsed JSON values.
    ///
    /// # Errors
    ///
    /// Returns an error if JSON parsing fails for any message slice.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::FastMessageParser;
    ///
    /// let parser = FastMessageParser::new('\n');
    /// let buffer = b"{\"name\":\"Alice\"}\n{\"name\":\"Bob\"}";
    /// let messages = parser.parse_json_messages(buffer).unwrap();
    /// assert_eq!(messages.len(), 2);
    /// ```
    pub fn parse_json_messages(
        &self,
        buffer: &[u8],
    ) -> Result<Vec<serde_json::Value>, serde_json::Error> {
        let message_slices = self.parse_messages(buffer);
        let mut results = Vec::new();

        for slice in message_slices {
            if let Ok(s) = std::str::from_utf8(slice)
                && let Ok(value) = serde_json::from_str(s.trim())
            {
                results.push(value);
            }
        }

        Ok(results)
    }
}

/// Channel aggregator for combining multiple channels (non-blocking)
///
/// Merges messages from multiple input channels into a single output channel.
/// Useful for fan-in patterns where multiple producers feed into one consumer.
///
/// # Type Parameters
///
/// * `T` - The type of messages.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::{core::bounded_queue_3, parsers::ChannelAggregator};
/// use smol;
///
/// # smol::block_on(async {
/// let (tx1, rx1) = bounded_queue_3::<String>(5);
/// let (tx2, rx2) = bounded_queue_3::<String>(5);
/// let (tx_out, rx_out) = bounded_queue_3::<String>(10);
///
/// let aggregator = ChannelAggregator::new(vec![rx1, rx2], tx_out);
/// aggregator.start();
///
/// tx1.send("from channel 1".to_string()).await.unwrap();
/// tx2.send("from channel 2".to_string()).await.unwrap();
///
/// let msg1 = rx_out.recv().await.unwrap();
/// let msg2 = rx_out.recv().await.unwrap();
/// // Messages arrive in arbitrary order
/// # });
/// ```
pub struct ChannelAggregator<T> {
    inputs: Vec<crate::channels::core::RxFuture<T>>,
    output: crate::channels::core::TxFuture<T>,
}

impl<T: Send + 'static + Clone> ChannelAggregator<T> {
    /// Create a new aggregator
    ///
    /// # Parameters
    ///
    /// * `inputs` - Vector of input channel receivers.
    /// * `output` - The output channel sender.
    ///
    /// # Returns
    ///
    /// A new `ChannelAggregator` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, parsers::ChannelAggregator};
    ///
    /// let (tx1, rx1) = bounded_queue_3::<i32>(5);
    /// let (tx2, rx2) = bounded_queue_3::<i32>(5);
    /// let (tx_out, rx_out) = bounded_queue_3::<i32>(10);
    ///
    /// let aggregator = ChannelAggregator::new(vec![rx1, rx2], tx_out);
    /// ```
    #[must_use]
    pub fn new(
        inputs: Vec<crate::channels::core::RxFuture<T>>,
        output: crate::channels::core::TxFuture<T>,
    ) -> Self {
        Self { inputs, output }
    }

    /// Start aggregating messages (spawns non-blocking tasks)
    ///
    /// Begins forwarding messages from all input channels to the output channel.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, parsers::ChannelAggregator};
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let (tx1, rx1) = bounded_queue_3::<String>(5);
    /// let (tx_out, rx_out) = bounded_queue_3::<String>(10);
    ///
    /// let aggregator = ChannelAggregator::new(vec![rx1], tx_out);
    /// aggregator.start();
    ///
    /// tx1.send("message".to_string()).await.unwrap();
    /// let received = rx_out.recv().await.unwrap();
    /// assert_eq!(received, "message");
    /// # });
    /// ```
    pub fn start(self) {
        for receiver in self.inputs {
            let output = self.output.clone();
            smol::spawn(async move {
                let rx = receiver;
                while let Ok(msg) = rx.recv().await {
                    let _ = output.send(msg).await;
                }
            })
            .detach();
        }
    }
}

/// Channel with automatic batching (non-blocking)
///
/// Accumulates individual items into batches before sending them.
/// Useful for reducing overhead when processing many small messages.
///
/// # Type Parameters
///
/// * `T` - The type of individual items.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::parsers::BatchingChannel;
/// use smol;
///
/// # smol::block_on(async {
/// let channel = BatchingChannel::new(3, 10); // Batch size 3
/// channel.send(1).await.unwrap();
/// channel.send(2).await.unwrap();
/// channel.send(3).await.unwrap(); // Triggers batch send
///
/// let batch_sender = channel.batch_sender();
/// // The batch [1,2,3] is now available on batch_sender
/// # });
/// ```
pub struct BatchingChannel<T> {
    tx: crate::channels::core::TxFuture<Vec<T>>,
    rx: crate::channels::core::RxFuture<Vec<T>>,
    batch_size: usize,
    current_batch: Arc<Mutex<Vec<T>>>,
}

impl<T: Clone + Send + 'static> BatchingChannel<T> {
    /// Create a new batching channel
    ///
    /// # Parameters
    ///
    /// * `batch_size` - Number of items to accumulate before sending a batch.
    /// * `capacity` - Capacity of the batch output channel.
    ///
    /// # Returns
    ///
    /// A new `BatchingChannel` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::BatchingChannel;
    ///
    /// let channel = BatchingChannel::new(10, 5); // Batch 10 items, channel capacity 5
    /// ```
    #[must_use]
    pub fn new(batch_size: usize, capacity: usize) -> Self {
        let (tx, rx) = crate::channels::core::bounded_queue_3(capacity);
        Self {
            tx,
            rx,
            batch_size,
            current_batch: Arc::new(Mutex::new(Vec::with_capacity(batch_size))),
        }
    }

    /// Send item (batches automatically, async, non-blocking)
    ///
    /// Adds the item to the current batch. When the batch is full, it's sent automatically.
    ///
    /// # Parameters
    ///
    /// * `item` - The item to add to the batch.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::BatchingChannel;
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let channel = BatchingChannel::new(2, 5);
    /// channel.send("item1").await.unwrap();
    /// channel.send("item2").await.unwrap(); // Batch sent here
    /// # });
    /// ```
    pub async fn send(&self, item: T) -> Result<(), Box<dyn std::error::Error>> {
        let should_flush = {
            let mut batch = self.current_batch.lock();
            batch.push(item);
            batch.len() >= self.batch_size
        };

        if should_flush {
            self.flush_batch().await?;
        }

        Ok(())
    }

    /// Flush current batch (async, non-blocking)
    ///
    /// Sends the current batch immediately, even if not full.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::BatchingChannel;
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let channel = BatchingChannel::new(10, 5);
    /// channel.send(1).await.unwrap();
    /// channel.send(2).await.unwrap();
    /// channel.flush_batch().await.unwrap(); // Send partial batch
    /// # });
    /// ```
    pub async fn flush_batch(&self) -> Result<(), Box<dyn std::error::Error>> {
        let batch = {
            let mut current = self.current_batch.lock();
            std::mem::take(&mut *current)
        };

        if !batch.is_empty() {
            self.tx.send(batch).await?;
        }

        Ok(())
    }

    /// Get the batch receiver
    ///
    /// Returns the channel receiver that receives completed batches.
    ///
    /// # Returns
    ///
    /// The batch output channel receiver.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::parsers::BatchingChannel;
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let channel = BatchingChannel::new(2, 10);
    /// let batch_receiver = channel.batch_receiver();
    /// channel.send("item1").await.unwrap();
    /// channel.send("item2").await.unwrap();
    /// let batch = batch_receiver.recv().await.unwrap();
    /// assert_eq!(batch.len(), 2);
    /// # });
    /// ```
    #[must_use]
    pub fn batch_receiver(&self) -> crate::channels::core::RxFuture<Vec<T>> {
        self.rx.clone()
    }
}

/// Channel with message filtering (non-blocking)
///
/// Only sends messages that pass through a filter function.
/// Useful for conditional message processing and routing.
///
/// # Type Parameters
///
/// * `T` - The type of messages.
/// * `F` - The type of the filter function.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::{core::bounded_queue_3, parsers::FilteredChannel};
/// use smol;
///
/// # smol::block_on(async {
/// let (tx, rx) = bounded_queue_3::<i32>(10);
/// let filtered = FilteredChannel::new(tx, |&num| num > 0); // Only positive numbers
///
/// filtered.send_filtered(5).await.unwrap();  // Sent
/// filtered.send_filtered(-1).await.unwrap(); // Filtered out
/// filtered.send_filtered(10).await.unwrap(); // Sent
///
/// let positive = rx.recv().await.unwrap();
/// assert_eq!(positive, 5);
/// # });
/// ```
pub struct FilteredChannel<T, F> {
    tx: crate::channels::core::TxFuture<T>,
    filter: F,
}

impl<T: Send + 'static, F: Fn(&T) -> bool + Send + Sync + 'static> FilteredChannel<T, F> {
    /// Create a new filtered channel
    ///
    /// # Parameters
    ///
    /// * `sender` - The underlying channel sender.
    /// * `filter` - Function that returns true for messages to send.
    ///
    /// # Returns
    ///
    /// A new `FilteredChannel` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, parsers::FilteredChannel};
    ///
    /// let (tx, _) = bounded_queue_3::<String>(10);
    /// let filtered = FilteredChannel::new(tx, |msg| msg.len() > 3);
    /// ```
    pub fn new(sender: crate::channels::core::TxFuture<T>, filter: F) -> Self {
        Self { tx: sender, filter }
    }

    /// Send message if it passes the filter (async, non-blocking)
    ///
    /// Only sends the message if the filter function returns true.
    ///
    /// # Parameters
    ///
    /// * `msg` - The message to potentially send.
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, parsers::FilteredChannel};
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let (tx, rx) = bounded_queue_3::<&str>(10);
    /// let filtered = FilteredChannel::new(tx, |msg| msg.starts_with("ok"));
    ///
    /// filtered.send_filtered("ok message").await.unwrap();     // Sent
    /// filtered.send_filtered("error message").await.unwrap();  // Filtered
    ///
    /// let msg = rx.recv().await.unwrap();
    /// assert_eq!(msg, "ok message");
    /// # });
    /// ```
    pub async fn send_filtered(&self, msg: T) -> Result<(), smol::channel::SendError<T>> {
        if (self.filter)(&msg) {
            self.tx.send(msg).await
        } else {
            Ok(()) // Silently drop filtered messages
        }
    }
}