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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/// Specialized channel implementations for advanced use cases.
///
/// This module provides specialized channel types for specific communication patterns
/// including base64 encoding, compression, file backing, rate limiting, prioritization,
/// parallel processing, and persistence.
///
/// # Examples
///
/// Base64 channel for text transport:
/// ```rust
/// use trash_utilities::channels::{core::bounded_queue_3, specialist::Base64Channel};
/// use smol;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Data { value: i32 }
///
/// # smol::block_on(async {
/// let (tx, rx) = bounded_queue_3::<String>(10);
/// let channel = Base64Channel::new(tx);
/// channel.send_base64(&Data { value: 42 }).await.unwrap();
/// let data: Data = Base64Channel::recv_base64(&rx).await.unwrap();
/// assert_eq!(data.value, 42);
/// # });
/// ```
// Standard library imports
use std::{
    io::{BufRead, BufReader, BufWriter, Read, Write},
    sync::Arc,
    time::Duration,
};

// External crate imports
use base64::{Engine, engine::general_purpose};
use brotli::{CompressorWriter, Decompressor};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;

/// Base64-encoded channel for text-based transport (non-blocking).
///
/// Automatically encodes data to base64 before sending and decodes on receive.
/// Useful for text-based protocols or when binary data needs to be transmitted as text.
///
/// # Type Parameters
///
/// * `T` - The type of data to send/receive (must implement Serialize/Deserialize).
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::{core::bounded_queue_3, specialist::Base64Channel};
/// use smol;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Message { id: u32, data: String }
///
/// # smol::block_on(async {
/// let (tx, rx) = bounded_queue_3::<String>(10);
/// let channel = Base64Channel::new(tx);
///
/// let msg = Message { id: 1, data: "hello".to_string() };
/// channel.send_base64(&msg).await.unwrap();
/// let received: Message = Base64Channel::recv_base64(&rx).await.unwrap();
/// assert_eq!(received, msg);
/// # });
/// ```
pub struct Base64Channel {
    inner: crate::channels::core::TxFuture<String>,
}

impl Base64Channel {
    /// Create a new base64 channel
    ///
    /// # Parameters
    ///
    /// * `inner` - The underlying string channel to wrap.
    ///
    /// # Returns
    ///
    /// A new `Base64Channel` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, specialist::Base64Channel};
    ///
    /// let (tx, _) = bounded_queue_3::<String>(10);
    /// let channel = Base64Channel::new(tx);
    /// ```
    #[must_use]
    pub fn new(inner: crate::channels::core::TxFuture<String>) -> Self {
        Self { inner }
    }

    /// Send data encoded as base64 (async, non-blocking)
    ///
    /// Serializes the data to JSON, encodes it as base64, and sends it.
    ///
    /// # Parameters
    ///
    /// * `data` - The data to send.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type of data to send (must implement Serialize).
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or channel send fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, specialist::Base64Channel};
    /// use smol;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct Data { value: i32 }
    ///
    /// # smol::block_on(async {
    /// let (tx, _) = bounded_queue_3::<String>(10);
    /// let channel = Base64Channel::new(tx);
    /// channel.send_base64(&Data { value: 42 }).await.unwrap();
    /// # });
    /// ```
    pub async fn send_base64<T: Serialize>(
        &self,
        data: &T,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let json = serde_json::to_string(data)?;
        let encoded = general_purpose::STANDARD.encode(json.as_bytes());
        self.inner.send(encoded).await?;
        Ok(())
    }

    /// Receive and decode base64 data (async, non-blocking)
    ///
    /// Receives base64-encoded data, decodes it, and deserializes from JSON.
    ///
    /// # Parameters
    ///
    /// * `receiver` - The channel receiver to read from.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to deserialize into (must implement Deserialize).
    ///
    /// # Returns
    ///
    /// The deserialized data on success.
    ///
    /// # Errors
    ///
    /// Returns an error if channel receive, base64 decoding, or deserialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::{core::bounded_queue_3, specialist::Base64Channel};
    /// use smol;
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(Serialize, Deserialize, Debug, PartialEq)]
    /// struct Data { value: i32 }
    ///
    /// # smol::block_on(async {
    /// let (tx, rx) = bounded_queue_3::<String>(10);
    /// let channel = Base64Channel::new(tx);
    /// channel.send_base64(&Data { value: 42 }).await.unwrap();
    /// let data: Data = Base64Channel::recv_base64(&rx).await.unwrap();
    /// assert_eq!(data.value, 42);
    /// # });
    /// ```
    pub async fn recv_base64<T: for<'de> Deserialize<'de>>(
        receiver: &crate::channels::core::RxFuture<String>,
    ) -> Result<T, Box<dyn std::error::Error>> {
        let encoded = receiver.recv().await?;
        let decoded = general_purpose::STANDARD.decode(&encoded)?;
        let json = String::from_utf8(decoded)?;
        let data: T = serde_json::from_str(&json)?;
        Ok(data)
    }
}

/// Compressed channel for bandwidth-efficient communication (non-blocking).
///
/// Automatically compresses data using Brotli before sending and decompresses on receive.
/// Ideal for reducing network bandwidth or storage when dealing with compressible data.
///
/// # Type Parameters
///
/// * `T` - The type of data to send/receive (must implement Serialize/Deserialize).
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::specialist::CompressedChannel;
/// use smol;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct LargeData { content: String }
///
/// # smol::block_on(async {
/// let channel = CompressedChannel::new();
/// let data = LargeData { content: "A".repeat(1000) };
/// channel.send_compressed(&data).await.unwrap();
/// let received: LargeData = channel.recv_decompressed().await.unwrap();
/// assert_eq!(received, data);
/// # });
/// ```
pub struct CompressedChannel {
    tx: crate::channels::core::TxFuture<Vec<u8>>,
    rx: crate::channels::core::RxFuture<Vec<u8>>,
    level: u32,
}

impl CompressedChannel {
    /// Create a new compressed channel with defaults
    ///
    /// Uses capacity of 100 and compression level 6.
    ///
    /// # Returns
    ///
    /// A new `CompressedChannel` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannel;
    ///
    /// let channel = CompressedChannel::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(100, 6)
    }

    /// Create a new compressed channel with custom config
    ///
    /// # Parameters
    ///
    /// * `capacity` - The channel buffer capacity.
    /// * `compression_level` - Brotli compression level (0-11).
    ///
    /// # Returns
    ///
    /// A new `CompressedChannel` instance with the specified configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannel;
    ///
    /// let channel = CompressedChannel::with_config(50, 9);
    /// ```
    #[must_use]
    pub fn with_config(capacity: usize, compression_level: u32) -> Self {
        let (tx, rx) = crate::channels::core::bounded_queue_3(capacity);
        Self {
            tx,
            rx,
            level: compression_level,
        }
    }

    /// Create a builder for advanced configuration
    ///
    /// # Returns
    ///
    /// A `CompressedChannelBuilder` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannel;
    ///
    /// let channel = CompressedChannel::builder()
    ///     .capacity(200)
    ///     .compression_level(11)
    ///     .build();
    /// ```
    #[must_use]
    pub fn builder() -> CompressedChannelBuilder {
        CompressedChannelBuilder::new()
    }

    /// Send data with compression (async, non-blocking)
    ///
    /// Serializes the data to JSON, compresses it with Brotli, and sends it.
    ///
    /// # Parameters
    ///
    /// * `data` - The data to send.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type of data to send (must implement Serialize).
    ///
    /// # Errors
    ///
    /// Returns an error if serialization, compression, or channel send fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannel;
    /// use smol;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct Data { text: String }
    ///
    /// # smol::block_on(async {
    /// let channel = CompressedChannel::new();
    /// let data = Data { text: "compressible text".repeat(100) };
    /// channel.send_compressed(&data).await.unwrap();
    /// # });
    /// ```
    pub async fn send_compressed<T: Serialize>(
        &self,
        data: &T,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let json = serde_json::to_string(data)?;
        let compressed = Self::compress_data(json.as_bytes(), self.level)?;
        self.tx.send(compressed).await?;
        Ok(())
    }

    /// Receive and decompress data (async, non-blocking)
    ///
    /// Receives compressed data, decompresses it, and deserializes from JSON.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to deserialize into (must implement Deserialize).
    ///
    /// # Returns
    ///
    /// The deserialized data on success.
    ///
    /// # Errors
    ///
    /// Returns an error if channel receive, decompression, or deserialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannel;
    /// use smol;
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(Serialize, Deserialize, Debug, PartialEq)]
    /// struct Data { text: String }
    ///
    /// # smol::block_on(async {
    /// let channel = CompressedChannel::new();
    /// let data = Data { text: "test".to_string() };
    /// channel.send_compressed(&data).await.unwrap();
    /// let received: Data = channel.recv_decompressed().await.unwrap();
    /// assert_eq!(received, data);
    /// # });
    /// ```
    pub async fn recv_decompressed<T: for<'de> Deserialize<'de>>(
        &self,
    ) -> Result<T, Box<dyn std::error::Error>> {
        let compressed = self.rx.recv().await?;
        let decompressed = Self::decompress_data(&compressed)?;
        let json = String::from_utf8(decompressed)?;
        let data: T = serde_json::from_str(&json)?;
        Ok(data)
    }

    fn compress_data(data: &[u8], level: u32) -> Result<Vec<u8>, std::io::Error> {
        let mut output = Vec::new();
        {
            let mut compressor = CompressorWriter::new(&mut output, 4096, level, level);
            compressor.write_all(data)?;
            compressor.flush()?;
        }
        Ok(output)
    }

    fn decompress_data(data: &[u8]) -> Result<Vec<u8>, std::io::Error> {
        let mut decompressor = Decompressor::new(data, 4096);
        let mut output = Vec::new();
        decompressor.read_to_end(&mut output)?;
        Ok(output)
    }
}

impl Default for CompressedChannel {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for `CompressedChannel` with ergonomic configuration
pub struct CompressedChannelBuilder {
    capacity: usize,
    compression_level: u32,
}

impl CompressedChannelBuilder {
    /// Create a new builder with defaults
    ///
    /// # Returns
    ///
    /// A new `CompressedChannelBuilder` instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannelBuilder;
    ///
    /// let builder = CompressedChannelBuilder::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            capacity: 100,
            compression_level: 6,
        }
    }

    /// Set the channel capacity
    ///
    /// # Parameters
    ///
    /// * `capacity` - The buffer capacity for the channel.
    ///
    /// # Returns
    ///
    /// The builder instance for chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannelBuilder;
    ///
    /// let builder = CompressedChannelBuilder::new().capacity(200);
    /// ```
    #[must_use]
    pub fn capacity(mut self, capacity: usize) -> Self {
        self.capacity = capacity;
        self
    }

    /// Set the compression level
    ///
    /// # Parameters
    ///
    /// * `level` - Brotli compression level (0-11, higher = better compression but slower).
    ///
    /// # Returns
    ///
    /// The builder instance for chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannelBuilder;
    ///
    /// let builder = CompressedChannelBuilder::new().compression_level(9);
    /// ```
    #[must_use]
    pub fn compression_level(mut self, level: u32) -> Self {
        self.compression_level = level;
        self
    }

    /// Build the `CompressedChannel`
    ///
    /// # Returns
    ///
    /// A new `CompressedChannel` with the configured settings.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::CompressedChannelBuilder;
    ///
    /// let channel = CompressedChannelBuilder::new()
    ///     .capacity(50)
    ///     .compression_level(11)
    ///     .build();
    /// ```
    #[must_use]
    pub fn build(self) -> CompressedChannel {
        CompressedChannel::with_config(self.capacity, self.compression_level)
    }
}

impl Default for CompressedChannelBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// File-backed channel for persistence and large data handling
///
/// Automatically falls back to temporary file storage when the in-memory channel is full.
/// Useful for handling large volumes of data or preventing memory exhaustion.
///
/// # Type Parameters
///
/// * `T` - The type of data to send (must implement Serialize/Deserialize).
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::specialist::FileBackedChannel;
/// use smol;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct LargeData { content: Vec<u8> }
///
/// # smol::block_on(async {
/// let channel = FileBackedChannel::new().unwrap();
/// let data = LargeData { content: vec![0; 1000000] }; // 1MB
/// channel.send(data).await.unwrap();
/// // Data is stored in memory or file as needed
/// # });
/// ```
pub struct FileBackedChannel<T> {
    tx: crate::channels::core::TxFuture<T>,
    temp_file: Arc<Mutex<Option<NamedTempFile>>>,
}

impl<T: Serialize + for<'de> Deserialize<'de> + Send + 'static + Unpin> FileBackedChannel<T> {
    /// Create a new file-backed channel
    ///
    /// Creates a temporary file for overflow storage.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type of data to send.
    ///
    /// # Returns
    ///
    /// A new `FileBackedChannel` instance on success.
    ///
    /// # Errors
    ///
    /// Returns an error if creating the temporary file fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::FileBackedChannel;
    ///
    /// let channel: FileBackedChannel<String> = FileBackedChannel::new().unwrap();
    /// ```
    pub fn new() -> Result<Self, std::io::Error> {
        // expose the receiver so the background writer can persist overflowed messages
        let (tx, file_rx) = crate::channels::core::bounded_queue_3::<T>(100);

        let temp_file = Arc::new(Mutex::new(Some(NamedTempFile::new()?)));
        let temp_file_clone = temp_file.clone();

        // Start file writer task (non-blocking)
        smol::spawn(async move {
            // receive T values from the fallback receiver, serialize to JSON and append to the temp file
            while let Ok(msg) = file_rx.recv().await {
                if let Some(ref mut temp) = *temp_file_clone.lock()
                    && let Ok(json) = serde_json::to_string(&msg)
                {
                    let _ = temp.as_file_mut().write_all(format!("{json}\n").as_bytes());
                    let _ = temp.as_file_mut().flush();
                }
            }
        })
        .detach();

        Ok(Self { tx, temp_file })
    }

    /// Send data (async, non-blocking, memory first then file)
    ///
    /// Attempts to send to the in-memory channel first. If full, serializes and writes to file.
    ///
    /// # Parameters
    ///
    /// * `data` - The data to send.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::FileBackedChannel;
    /// use smol;
    ///
    /// # smol::block_on(async {
    /// let channel: FileBackedChannel<String> = FileBackedChannel::new().unwrap();
    /// channel.send("data".to_string()).await.unwrap();
    /// # });
    /// ```
    pub async fn send(&self, data: T) -> Result<(), Box<dyn std::error::Error>> {
        // Serialize first so we can persist if send fails.
        let json = serde_json::to_string(&data)?;

        // Try to send to memory channel first. If it fails, persist the serialized JSON to file.
        if let Ok(()) = self.tx.send(data).await {
            Ok(())
        } else {
            if let Some(ref mut temp) = *self.temp_file.lock() {
                let _ = temp.as_file_mut().write_all(format!("{json}\n").as_bytes());
                let _ = temp.as_file_mut().flush();
            }
            Ok(())
        }
    }

    /// Flush file data to memory
    ///
    /// Reads all data from the temporary file and returns it as a vector.
    ///
    /// # Returns
    ///
    /// A vector of deserialized data from the file.
    ///
    /// # Errors
    ///
    /// Returns an error if file reading or deserialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use trash_utilities::channels::specialist::FileBackedChannel;
    ///
    /// let channel: FileBackedChannel<String> = FileBackedChannel::new().unwrap();
    /// let data: Vec<String> = channel.flush_to_memory().unwrap();
    /// ```
    pub fn flush_to_memory(&self) -> Result<Vec<T>, Box<dyn std::error::Error>> {
        let mut results = Vec::new();
        if let Some(ref file) = *self.temp_file.lock() {
            // Read from the underlying File of NamedTempFile and iterate lines
            let reader = BufReader::new(file.as_file());
            for line_res in reader.lines() {
                let line = line_res?;
                if !line.trim().is_empty() {
                    let data: T = serde_json::from_str(&line)?;
                    results.push(data);
                }
            }
        }
        Ok(results)
    }
}

impl<T: Serialize + for<'de> Deserialize<'de> + Send + 'static> Default for FileBackedChannel<T> {
    fn default() -> Self {
        let (tx, _) = crate::channels::core::bounded_queue_3(100);
        Self {
            tx,
            temp_file: Arc::new(Mutex::new(None)),
        }
    }
}

/// Rate-limited channel to prevent overwhelming receivers (non-blocking)
///
/// Uses a token bucket algorithm to limit the rate of messages sent.
/// Useful for controlling throughput and preventing system overload.
///
/// # Type Parameters
///
/// * `T` - The type of data to send.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::specialist::RateLimitedChannel;
/// use smol;
///
/// # smol::block_on(async {
/// let channel = RateLimitedChannel::new(10, 10.0, 1.0); // 10 tokens, refill 1/sec
/// channel.send("message".to_string()).await.unwrap();
/// # });
/// ```
pub struct RateLimitedChannel<T> {
    tx: crate::channels::core::TxFuture<T>,
    #[allow(dead_code)]
    rx: crate::channels::core::RxFuture<T>,
    rate_limiter: Arc<Mutex<RateLimiter>>,
}

struct RateLimiter {
    tokens: f64,
    max_tokens: f64,
    refill_rate: f64,
    last_refill: std::time::Instant,
}

impl RateLimiter {
    fn new(max_tokens: f64, refill_rate: f64) -> Self {
        Self {
            tokens: max_tokens,
            max_tokens,
            refill_rate,
            last_refill: std::time::Instant::now(),
        }
    }

    fn acquire(&mut self, tokens: f64) -> bool {
        self.refill();
        if self.tokens >= tokens {
            self.tokens -= tokens;
            true
        } else {
            false
        }
    }

    fn refill(&mut self) {
        let now = std::time::Instant::now();
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.refill_rate).min(self.max_tokens);
        self.last_refill = now;
    }
}

impl<T: Send + 'static> RateLimitedChannel<T> {
    /// Create a new rate-limited channel
    #[must_use]
    pub fn new(capacity: usize, max_tokens: f64, refill_rate: f64) -> Self {
        let (tx, rx) = crate::channels::core::bounded_queue_3(capacity);
        Self {
            tx,
            rx,
            rate_limiter: Arc::new(Mutex::new(RateLimiter::new(max_tokens, refill_rate))),
        }
    }

    /// Send with rate limiting (async, non-blocking)
    ///
    /// # Errors
    ///
    /// Returns an error if rate limit is exceeded or channel is closed/full.
    pub async fn send(&self, msg: T) -> Result<(), Box<dyn std::error::Error>> {
        if self.rate_limiter.lock().acquire(1.0) {
            self.tx.send(msg).await?;
            Ok(())
        } else {
            Err("Rate limit exceeded".into())
        }
    }
}

/// Prioritized channel with multiple priority levels (non-blocking)
///
/// Supports high, normal, and low priority messages. High priority messages are processed first.
/// Useful for systems requiring message prioritization.
///
/// # Type Parameters
///
/// * `T` - The type of data to send/receive.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::channels::specialist::PriorityChannel;
/// use smol;
///
/// # smol::block_on(async {
/// let channel = PriorityChannel::new(10);
/// channel.send_high("urgent".to_string()).await.unwrap();
/// channel.send_normal("normal".to_string()).await.unwrap();
/// let msg = channel.recv().await.unwrap(); // Gets "urgent" first
/// assert_eq!(msg, "urgent");
/// # });
/// ```
pub struct PriorityChannel<T> {
    high_tx: crate::channels::core::TxFuture<T>,
    normal_tx: crate::channels::core::TxFuture<T>,
    low_tx: crate::channels::core::TxFuture<T>,
    rx: crate::channels::core::RxFuture<T>,
}

impl<T: Send + 'static + Unpin + Clone> PriorityChannel<T> {
    /// Create a new priority channel
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        let (high_tx, high_rx) = crate::channels::core::bounded_queue_3(capacity);
        let (normal_tx, normal_rx) = crate::channels::core::bounded_queue_3(capacity);
        let (low_tx, low_rx) = crate::channels::core::bounded_queue_3(capacity);
        let (output_tx, output_rx) = crate::channels::core::bounded_queue_3(capacity);

        // Start priority merger (non-blocking with select-like behavior)
        smol::spawn(async move {
            loop {
                // Try high priority first with non-blocking recv
                if let Ok(msg) = high_rx.try_recv() {
                    let _ = output_tx.send(msg).await;
                    continue;
                }

                // Then normal priority
                if let Ok(msg) = normal_rx.try_recv() {
                    let _ = output_tx.send(msg).await;
                    continue;
                }

                // Finally low priority
                if let Ok(msg) = low_rx.try_recv() {
                    let _ = output_tx.send(msg).await;
                    continue;
                }

                // If nothing available, wait a bit to prevent busy-spinning
                smol::Timer::after(Duration::from_micros(100)).await;
            }
        })
        .detach();

        Self {
            high_tx,
            normal_tx,
            low_tx,
            rx: output_rx,
        }
    }

    /// Send high priority message (async, non-blocking)
    ///
    /// # Errors
    ///
    /// Send high priority message (async, non-blocking)
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    pub async fn send_high(&self, msg: T) -> Result<(), smol::channel::SendError<T>> {
        self.high_tx.send(msg).await
    }

    /// Send normal priority message (async, non-blocking)
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    pub async fn send_normal(&self, msg: T) -> Result<(), smol::channel::SendError<T>> {
        self.normal_tx.send(msg).await
    }

    /// Send low priority message (async, non-blocking)
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or full.
    pub async fn send_low(&self, msg: T) -> Result<(), smol::channel::SendError<T>> {
        self.low_tx.send(msg).await
    }

    /// Receive message (highest priority first, async, non-blocking)
    ///
    /// # Errors
    ///
    /// Returns an error if the channel is closed or empty.
    pub async fn recv(&self) -> Result<T, smol::channel::RecvError> {
        self.rx.recv().await
    }
}

/// Parallel channel processor using `fork_union` (non-blocking)
pub struct ParallelChannelProcessor<T, F, R>
where
    T: Send + 'static,
    F: Fn(T) -> R + Send + Sync + 'static,
    R: Send + 'static,
{
    receivers: Vec<crate::channels::core::RxFuture<T>>,
    processor: Arc<F>,
    results_tx: crate::channels::core::TxFuture<R>,
}

impl<T: Send + 'static, F: Fn(T) -> R + Send + Sync + 'static, R: Send + 'static>
    ParallelChannelProcessor<T, F, R>
{
    /// Create a new parallel processor
    pub fn new(receivers: Vec<crate::channels::core::RxFuture<T>>, processor: F) -> Self {
        let capacity = (receivers.len() * 10).max(1); // Ensure minimum capacity of 1
        let (results_tx, _) = crate::channels::core::bounded_queue_3(capacity);
        Self {
            receivers,
            processor: Arc::new(processor),
            results_tx,
        }
    }

    /// Start parallel processing (spawns non-blocking tasks)
    pub fn start(self) {
        let processor = self.processor.clone();

        for receiver in self.receivers {
            let processor = processor.clone();
            let results_tx = self.results_tx.clone();

            smol::spawn(async move {
                let rx = receiver;
                while let Ok(msg) = rx.recv().await {
                    let result = processor(msg);
                    let _ = results_tx.send(result).await;
                }
            })
            .detach();
        }
    }
}

/// Channel persistence for crash recovery (async, non-blocking)
pub struct PersistentChannel<T: Serialize> {
    tx: crate::channels::core::TxFuture<T>,
    log_file: Arc<Mutex<BufWriter<std::fs::File>>>,
}

impl<T: Serialize + Send + 'static> PersistentChannel<T> {
    /// Create a new persistent channel
    ///
    /// # Errors
    ///
    /// Returns an error if opening the log file fails.
    pub fn new(
        sender: crate::channels::core::TxFuture<T>,
        log_path: &str,
    ) -> Result<Self, std::io::Error> {
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(log_path)?;
        let writer = BufWriter::new(file);

        Ok(Self {
            tx: sender,
            log_file: Arc::new(Mutex::new(writer)),
        })
    }

    /// Send with persistence (async, non-blocking write to disk + channel)
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or channel send fails.
    pub async fn send_persistent(&self, msg: T) -> Result<(), Box<dyn std::error::Error>> {
        let json = serde_json::to_string(&msg)?;

        // Log to file asynchronously
        let log_file = self.log_file.clone();
        smol::spawn(async move {
            let mut writer = log_file.lock();
            let _ = writeln!(writer, "{json}");
            let _ = writer.flush();
        })
        .detach();

        // Send to channel
        self.tx.send(msg).await?;
        Ok(())
    }

    /// Recover messages from log file
    ///
    /// # Errors
    ///
    /// Returns an error if file opening, reading, or deserialization fails.
    pub fn recover_messages<U: for<'de> Deserialize<'de>>(
        log_path: &str,
    ) -> Result<Vec<U>, Box<dyn std::error::Error>> {
        let file = std::fs::File::open(log_path)?;
        let reader = BufReader::new(file);
        let mut messages = Vec::new();

        for line in std::io::BufRead::lines(reader) {
            let line = line?;
            if !line.trim().is_empty() {
                let msg: U = serde_json::from_str(&line)?;
                messages.push(msg);
            }
        }

        Ok(messages)
    }
}