rust-tango 0.1.2

A lock-free, high-performance IPC channel inspired by Firedancer's Tango
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
//! A lock-free, high-performance IPC channel inspired by Firedancer's Tango.
//!
//! This crate provides a single-producer single-consumer (SPSC) channel optimized
//! for low-latency, high-throughput message passing. It uses lock-free algorithms
//! with busy-polling for minimal latency.
//!
//! # Features
//!
//! - **Zero-copy reads**: Access message payloads directly without allocation
//! - **Lock-free**: No mutexes, just atomic operations with careful memory ordering
//! - **Backpressure**: Optional credit-based flow control via [`Fctl`]
//! - **Overrun detection**: Consumers detect when they've been lapped by producers
//! - **Metrics**: Built-in observability with [`Metrics`]
//! - **`no_std` support**: Works in embedded/kernel environments (disable `std` feature)
//!
//! # Architecture
//!
//! - [`MCache`]: Ring buffer of fragment metadata with sequence-based validation
//! - [`DCache`]: Fixed-size chunk storage for payloads
//! - [`Producer`] / [`Consumer`]: Ergonomic publish and consume APIs
//! - [`Fctl`]: Credit counter for backpressure
//! - [`Fseq`]: Shared sequence counter
//!
//! # Quick Start
//!
//! ```
//! use rust_tango::{Consumer, DCache, Fseq, MCache, Producer};
//!
//! // Create the channel components
//! let mcache = MCache::<64>::new();      // 64-slot metadata ring buffer
//! let dcache = DCache::<64, 256>::new(); // 64 chunks of 256 bytes each
//! let fseq = Fseq::new(1);               // Sequence counter starting at 1
//!
//! let producer = Producer::new(&mcache, &dcache, &fseq);
//! let mut consumer = Consumer::new(&mcache, &dcache, 1);
//!
//! // Publish a message
//! producer.publish(b"hello", 0, 0, 0).unwrap();
//!
//! // Consume it (zero-copy)
//! if let Ok(Some(fragment)) = consumer.poll() {
//!     assert_eq!(fragment.payload.as_slice(), b"hello");
//! }
//! ```
//!
//! # With Flow Control
//!
//! Use [`Fctl`] to prevent the producer from overwriting unconsumed messages:
//!
//! ```
//! use rust_tango::{Consumer, DCache, Fctl, Fseq, MCache, Producer};
//!
//! let mcache = MCache::<64>::new();
//! let dcache = DCache::<64, 256>::new();
//! let fseq = Fseq::new(1);
//! let fctl = Fctl::new(64); // 64 credits = buffer capacity
//!
//! let producer = Producer::with_flow_control(&mcache, &dcache, &fseq, &fctl);
//! let mut consumer = Consumer::with_flow_control(&mcache, &dcache, &fctl, 1);
//!
//! // Producer blocks when buffer is full (returns NoCredits error)
//! // Consumer automatically releases credits after consuming
//! ```
//!
//! # With Metrics
//!
//! Track throughput, lag, and errors:
//!
//! ```
//! use rust_tango::{Consumer, DCache, Fseq, MCache, Metrics, Producer};
//!
//! let mcache = MCache::<64>::new();
//! let dcache = DCache::<64, 256>::new();
//! let fseq = Fseq::new(1);
//! let metrics = Metrics::new();
//!
//! let producer = Producer::new(&mcache, &dcache, &fseq)
//!     .with_metrics(&metrics);
//! let mut consumer = Consumer::new(&mcache, &dcache, 1)
//!     .with_metrics(&metrics);
//!
//! // ... publish and consume ...
//! # producer.publish(b"test", 0, 0, 0).unwrap();
//! # consumer.poll().unwrap();
//!
//! let snapshot = metrics.snapshot();
//! println!("Lag: {} messages", snapshot.lag());
//! ```
//!
//! # Performance Characteristics
//!
//! Benchmarked on Apple M3 Pro (1000 samples, 10K messages, 64-byte payload):
//!
//! | Scenario | Tango | std | crossbeam | ringbuf |
//! |----------|-------|-----|-----------|---------|
//! | SPSC throughput | **26.6M msg/s** | 9.1M msg/s | 7.3M msg/s | 10.7M msg/s |
//! | Ping-pong latency (100 trips) | **90 µs** | 380 µs | 105 µs | 100 µs |
//! | Large payload (1KB) | **12.4 GiB/s** | - | 6.9 GiB/s | 9.4 GiB/s |
//!
//! Best suited for:
//! - Single-producer single-consumer scenarios
//! - Latency-sensitive applications
//! - High-throughput message passing
//! - When you can dedicate a core to busy-polling
//!
//! # Memory Ordering
//!
//! The lock-free protocol:
//! 1. Producer writes payload to [`DCache`]
//! 2. Producer writes metadata to [`MCache`] slot
//! 3. Producer stores sequence number with `Release` ordering
//! 4. Consumer loads sequence with `Acquire`, reads metadata, re-checks sequence
//!
//! This double-read validation detects overwrites without locks.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
extern crate std;

// Conditional imports for loom testing vs normal operation
#[cfg(loom)]
use loom::cell::UnsafeCell;
#[cfg(loom)]
use loom::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};

#[cfg(not(loom))]
use core::cell::UnsafeCell;
#[cfg(not(loom))]
use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};

use core::fmt;
use core::mem::size_of;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(C, align(16))]
pub struct FragmentMetadata {
    /// Monotonic sequence number used for ordering and overwrite detection.
    pub seq: u64,
    /// Signature or tag used for identification or filtering.
    pub sig: u64,
    /// Index into the data cache for the payload bytes.
    pub chunk: u32,
    /// Payload size in bytes.
    pub size: u32,
    /// Control bits for application-specific signaling.
    pub ctl: u16,
    /// Reserved bits for future expansion.
    pub reserved: u16,
    /// Timestamp or timing metadata.
    pub ts: u32,
}

const _: () = {
    assert!(size_of::<FragmentMetadata>() == 32);
};

impl fmt::Display for FragmentMetadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Fragment {{ seq={}, sig={:#x}, chunk={}, size={}, ctl={}, ts={} }}",
            self.seq, self.sig, self.chunk, self.size, self.ctl, self.ts
        )
    }
}

/// Errors that can occur during tango operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[must_use = "this error should be handled"]
pub enum TangoError {
    /// The DCache has no more chunks available (only without flow control).
    DcacheFull,
    /// The chunk index is out of range.
    ChunkOutOfRange(u32),
    /// Consumer was too slow and was lapped by the producer.
    Overrun,
    /// No credits available (flow control backpressure).
    NoCredits,
}

impl fmt::Display for TangoError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TangoError::DcacheFull => write!(f, "dcache is out of capacity"),
            TangoError::ChunkOutOfRange(idx) => write!(f, "chunk index {} out of range", idx),
            TangoError::Overrun => write!(f, "consumer overrun: producer lapped the consumer"),
            TangoError::NoCredits => write!(f, "no credits available for backpressure"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for TangoError {}

/// Result of attempting to read from the MCache.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[must_use = "this `ReadResult` may contain data that should be handled"]
pub enum ReadResult<T> {
    /// Successfully read the data.
    Ok(T),
    /// The sequence number has not been published yet.
    NotReady,
    /// The consumer was too slow and the slot was overwritten.
    Overrun,
}

impl<T> ReadResult<T> {
    /// Returns `true` if the result is `Ok`.
    #[inline]
    pub fn is_ok(&self) -> bool {
        matches!(self, ReadResult::Ok(_))
    }

    /// Returns `true` if the result is `NotReady`.
    #[inline]
    pub fn is_not_ready(&self) -> bool {
        matches!(self, ReadResult::NotReady)
    }

    /// Returns `true` if the result is `Overrun`.
    #[inline]
    pub fn is_overrun(&self) -> bool {
        matches!(self, ReadResult::Overrun)
    }

    /// Converts to `Option<T>`, discarding the error variant.
    #[inline]
    pub fn ok(self) -> Option<T> {
        match self {
            ReadResult::Ok(v) => Some(v),
            _ => None,
        }
    }
}

impl<T: fmt::Debug> fmt::Display for ReadResult<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReadResult::Ok(v) => write!(f, "Ok({:?})", v),
            ReadResult::NotReady => write!(f, "NotReady"),
            ReadResult::Overrun => write!(f, "Overrun"),
        }
    }
}

/// Metrics for observability and monitoring.
///
/// All counters are atomically updated and can be read from any thread.
/// Use `snapshot()` to get a consistent point-in-time view.
#[derive(Debug)]
pub struct Metrics {
    /// Total messages published.
    published: AtomicU64,
    /// Total messages consumed.
    consumed: AtomicU64,
    /// Total overruns detected (consumer was lapped).
    overruns: AtomicU64,
    /// Total times producer was blocked due to no credits.
    backpressure_events: AtomicU64,
}

/// A point-in-time snapshot of metrics.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[must_use = "this snapshot contains metrics data that should be used"]
pub struct MetricsSnapshot {
    /// Total messages published.
    pub published: u64,
    /// Total messages consumed.
    pub consumed: u64,
    /// Total overruns detected.
    pub overruns: u64,
    /// Total backpressure events.
    pub backpressure_events: u64,
}

impl MetricsSnapshot {
    /// Returns the current consumer lag (published - consumed).
    #[inline]
    pub fn lag(&self) -> u64 {
        self.published.saturating_sub(self.consumed)
    }
}

impl fmt::Display for MetricsSnapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "published={}, consumed={}, lag={}, overruns={}, backpressure={}",
            self.published,
            self.consumed,
            self.lag(),
            self.overruns,
            self.backpressure_events
        )
    }
}

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

impl Metrics {
    /// Create a new metrics instance with all counters at zero.
    pub fn new() -> Self {
        Self {
            published: AtomicU64::new(0),
            consumed: AtomicU64::new(0),
            overruns: AtomicU64::new(0),
            backpressure_events: AtomicU64::new(0),
        }
    }

    /// Record a published message.
    #[inline]
    pub fn record_publish(&self) {
        self.published.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a consumed message.
    #[inline]
    pub fn record_consume(&self) {
        self.consumed.fetch_add(1, Ordering::Relaxed);
    }

    /// Record an overrun event.
    #[inline]
    pub fn record_overrun(&self) {
        self.overruns.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a backpressure event (producer blocked on credits).
    #[inline]
    pub fn record_backpressure(&self) {
        self.backpressure_events.fetch_add(1, Ordering::Relaxed);
    }

    /// Get a consistent snapshot of all metrics.
    pub fn snapshot(&self) -> MetricsSnapshot {
        // Use Acquire to ensure we see all prior increments
        MetricsSnapshot {
            published: self.published.load(Ordering::Acquire),
            consumed: self.consumed.load(Ordering::Acquire),
            overruns: self.overruns.load(Ordering::Acquire),
            backpressure_events: self.backpressure_events.load(Ordering::Acquire),
        }
    }

    /// Reset all counters to zero.
    pub fn reset(&self) {
        self.published.store(0, Ordering::Release);
        self.consumed.store(0, Ordering::Release);
        self.overruns.store(0, Ordering::Release);
        self.backpressure_events.store(0, Ordering::Release);
    }
}

/// Command-and-control state for coordinating threads.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CncState {
    Boot = 0,
    Run = 1,
    Halt = 2,
}

impl CncState {
    fn from_u8(value: u8) -> Self {
        match value {
            0 => CncState::Boot,
            1 => CncState::Run,
            _ => CncState::Halt,
        }
    }
}

impl fmt::Display for CncState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CncState::Boot => write!(f, "Boot"),
            CncState::Run => write!(f, "Run"),
            CncState::Halt => write!(f, "Halt"),
        }
    }
}

#[derive(Debug)]
pub struct Cnc {
    state: AtomicU8,
}

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

impl Cnc {
    pub fn new() -> Self {
        Self {
            state: AtomicU8::new(CncState::Boot as u8),
        }
    }

    pub fn state(&self) -> CncState {
        CncState::from_u8(self.state.load(Ordering::Acquire))
    }

    pub fn set_state(&self, state: CncState) {
        self.state.store(state as u8, Ordering::Release);
    }
}

#[derive(Debug)]
pub struct Fseq {
    next: AtomicU64,
}

impl Fseq {
    pub fn new(initial: u64) -> Self {
        Self {
            next: AtomicU64::new(initial),
        }
    }

    pub fn next(&self) -> u64 {
        self.next.fetch_add(1, Ordering::AcqRel)
    }

    pub fn current(&self) -> u64 {
        self.next.load(Ordering::Acquire)
    }
}

#[derive(Debug)]
pub struct Fctl {
    credits: AtomicU64,
}

impl Fctl {
    pub fn new(initial: u64) -> Self {
        Self {
            credits: AtomicU64::new(initial),
        }
    }

    #[must_use = "returns whether the credits were successfully acquired"]
    pub fn acquire(&self, amount: u64) -> bool {
        let mut current = self.credits.load(Ordering::Acquire);
        loop {
            if current < amount {
                return false;
            }
            match self.credits.compare_exchange(
                current,
                current - amount,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return true,
                Err(next) => current = next,
            }
        }
    }

    pub fn release(&self, amount: u64) {
        self.credits.fetch_add(amount, Ordering::AcqRel);
    }

    pub fn available(&self) -> u64 {
        self.credits.load(Ordering::Acquire)
    }
}

const fn is_power_of_two(value: usize) -> bool {
    value != 0 && (value & (value - 1)) == 0
}

/// Lock-free tag cache using a hashed bitset.
#[derive(Debug)]
pub struct Tcache<const WORDS: usize> {
    bits: [AtomicU64; WORDS],
    mask: u64,
}

impl<const WORDS: usize> Default for Tcache<WORDS> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const WORDS: usize> Tcache<WORDS> {
    const BIT_COUNT: usize = WORDS * 64;
    const ASSERT_POWER_OF_TWO: () = assert!(is_power_of_two(Self::BIT_COUNT));

    /// Create a tag cache backed by a power-of-two number of bits.
    pub fn new() -> Self {
        let () = Self::ASSERT_POWER_OF_TWO;
        Self {
            bits: core::array::from_fn(|_| AtomicU64::new(0)),
            mask: (Self::BIT_COUNT - 1) as u64,
        }
    }

    pub fn check_and_insert(&self, tag: u64) -> bool {
        let bit = tag.wrapping_mul(0x9E37_79B9_7F4A_7C15) & self.mask;
        let word_idx = (bit / 64) as usize;
        let bit_mask = 1u64 << (bit % 64);
        let prev = self.bits[word_idx].fetch_or(bit_mask, Ordering::AcqRel);
        (prev & bit_mask) == 0
    }

    pub fn len(&self) -> usize {
        self.bits
            .iter()
            .map(|word| word.load(Ordering::Acquire).count_ones() as usize)
            .sum()
    }

    pub fn is_empty(&self) -> bool {
        self.bits
            .iter()
            .all(|word| word.load(Ordering::Acquire) == 0)
    }
}

/// Cache line size for padding to prevent false sharing.
const CACHE_LINE_SIZE: usize = 64;

/// A single entry in the MCache ring buffer.
///
/// Layout is carefully designed to prevent false sharing:
/// - `seq` is on its own cache line (read by consumer, written by producer)
/// - `meta` is on a separate cache line (written by producer, read by consumer)
#[repr(C, align(64))]
struct MCacheEntry {
    /// Sequence number - atomically updated by producer, read by consumer.
    seq: AtomicU64,
    /// Padding to push metadata to a separate cache line.
    _pad: [u8; CACHE_LINE_SIZE - size_of::<AtomicU64>()],
    /// Fragment metadata - written by producer before seq update.
    meta: UnsafeCell<FragmentMetadata>,
}

impl fmt::Debug for MCacheEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MCacheEntry")
            .field("seq", &self.seq.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

// SAFETY: MCacheEntry is Sync because:
// - `seq` is atomic and provides synchronization
// - `meta` is only written before `seq` is updated (Release) and read after
//   `seq` is loaded (Acquire), establishing a happens-before relationship
unsafe impl Sync for MCacheEntry {}

impl MCacheEntry {
    fn new() -> Self {
        Self {
            seq: AtomicU64::new(0),
            _pad: [0u8; CACHE_LINE_SIZE - size_of::<AtomicU64>()],
            meta: UnsafeCell::new(FragmentMetadata::default()),
        }
    }
}

#[derive(Debug)]
pub struct MCache<const DEPTH: usize> {
    mask: u64,
    entries: [MCacheEntry; DEPTH],
    running: AtomicBool,
}

impl<const DEPTH: usize> Default for MCache<DEPTH> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const DEPTH: usize> MCache<DEPTH> {
    const ASSERT_POWER_OF_TWO: () = assert!(is_power_of_two(DEPTH));

    /// Create a ring buffer with a power-of-two number of slots.
    pub fn new() -> Self {
        let () = Self::ASSERT_POWER_OF_TWO;
        Self {
            mask: (DEPTH - 1) as u64,
            entries: core::array::from_fn(|_| MCacheEntry::new()),
            running: AtomicBool::new(true),
        }
    }

    pub fn stop(&self) {
        self.running.store(false, Ordering::Release);
    }

    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::Acquire)
    }

    /// Publish a fragment's metadata into the ring.
    pub fn publish(&self, meta: FragmentMetadata) {
        let idx = (meta.seq & self.mask) as usize;
        let entry = &self.entries[idx];
        // SAFETY: This write is safe because:
        // 1. We have exclusive write access as the single producer
        // 2. The subsequent Release store on `seq` ensures this write is visible
        //    to any consumer that loads `seq` with Acquire ordering
        // 3. The mask ensures idx is always within bounds
        unsafe {
            *entry.meta.get() = meta;
        }
        entry.seq.store(meta.seq, Ordering::Release);
    }

    /// Busy-wait for a specific sequence number to appear.
    ///
    /// Returns:
    /// - `ReadResult::Ok(meta)` if the sequence was successfully read
    /// - `ReadResult::Overrun` if the consumer was lapped by the producer
    /// - `ReadResult::NotReady` if the mcache was stopped before the sequence appeared
    pub fn wait(&self, seq: u64) -> ReadResult<FragmentMetadata> {
        while self.is_running() {
            match self.try_read(seq) {
                ReadResult::Ok(meta) => return ReadResult::Ok(meta),
                ReadResult::Overrun => return ReadResult::Overrun,
                ReadResult::NotReady => core::hint::spin_loop(),
            }
        }
        ReadResult::NotReady
    }

    /// Attempt a lock-free read of the metadata at a specific sequence.
    ///
    /// Returns:
    /// - `ReadResult::Ok(meta)` if the sequence was successfully read
    /// - `ReadResult::NotReady` if the sequence has not been published yet
    /// - `ReadResult::Overrun` if the consumer was lapped by the producer
    pub fn try_read(&self, seq: u64) -> ReadResult<FragmentMetadata> {
        let idx = (seq & self.mask) as usize;
        let entry = &self.entries[idx];

        let seq_before = entry.seq.load(Ordering::Acquire);

        // Not published yet
        if seq_before < seq {
            return ReadResult::NotReady;
        }

        // Slot has been overwritten - consumer was too slow
        if seq_before > seq {
            return ReadResult::Overrun;
        }

        // seq_before == seq: attempt to read
        // SAFETY: This read is safe because:
        // 1. The Acquire load above synchronizes with the Release store in publish(),
        //    establishing a happens-before relationship that ensures the metadata
        //    write is complete and visible
        // 2. We verify seq hasn't changed after reading (double-read validation)
        //    to detect concurrent overwrites
        // 3. FragmentMetadata is Copy, so we get a snapshot that won't be affected
        //    by subsequent writes
        let meta = unsafe { *entry.meta.get() };

        // Double-check the sequence hasn't changed during our read
        let seq_after = entry.seq.load(Ordering::Acquire);
        if seq_before == seq_after {
            ReadResult::Ok(meta)
        } else {
            // Producer overwrote while we were reading
            ReadResult::Overrun
        }
    }
}

/// A single chunk in the DCache.
#[repr(C, align(64))]
struct DcacheChunk<const CHUNK_SIZE: usize> {
    data: UnsafeCell<[u8; CHUNK_SIZE]>,
}

impl<const CHUNK_SIZE: usize> fmt::Debug for DcacheChunk<CHUNK_SIZE> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DcacheChunk")
            .field("size", &CHUNK_SIZE)
            .finish_non_exhaustive()
    }
}

impl<const CHUNK_SIZE: usize> DcacheChunk<CHUNK_SIZE> {
    fn new() -> Self {
        Self {
            data: UnsafeCell::new([0u8; CHUNK_SIZE]),
        }
    }
}

// SAFETY: DcacheChunk is Sync because access is synchronized through the
// MCache sequence protocol - writes happen before sequence update (Release),
// reads happen after sequence load (Acquire).
unsafe impl<const CHUNK_SIZE: usize> Sync for DcacheChunk<CHUNK_SIZE> {}

#[derive(Debug, Clone, Copy)]
pub struct DcacheView<'a, const CHUNK_SIZE: usize> {
    chunk: &'a DcacheChunk<CHUNK_SIZE>,
    size: usize,
}

impl<'a, const CHUNK_SIZE: usize> DcacheView<'a, CHUNK_SIZE> {
    /// Zero-copy access to the payload bytes.
    #[inline]
    pub fn as_slice(&self) -> &'a [u8] {
        // SAFETY: This read is safe because:
        // 1. DcacheView can only be obtained through Consumer::poll() or
        //    Consumer::wait(), which validate via the MCache sequence protocol
        // 2. The Acquire load in MCache::try_read synchronizes with the Release
        //    store in MCache::publish, ensuring the payload write in
        //    DCache::write_chunk is fully visible before we can read it
        // 3. The lifetime 'a is tied to the DCache, ensuring the data remains
        //    valid for the duration of the borrow
        // 4. self.size is validated to be <= CHUNK_SIZE when the view is created
        let data = unsafe { &*self.chunk.data.get() };
        &data[..self.size]
    }

    /// Copy the payload into a new Vec.
    ///
    /// Prefer `as_slice()` for zero-copy access when possible.
    ///
    /// Only available with the `std` feature.
    #[cfg(feature = "std")]
    pub fn read(&self) -> std::vec::Vec<u8> {
        self.as_slice().to_vec()
    }

    /// Access the payload through a closure.
    ///
    /// Prefer `as_slice()` for direct zero-copy access.
    pub fn with_reader<T>(&self, f: impl FnOnce(&[u8]) -> T) -> T {
        f(self.as_slice())
    }

    /// Returns the size of the payload in bytes.
    #[inline]
    pub fn len(&self) -> usize {
        self.size
    }

    /// Returns true if the payload is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }
}

#[derive(Debug)]
pub struct DCache<const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> {
    chunks: [DcacheChunk<CHUNK_SIZE>; CHUNK_COUNT],
    next: AtomicU64,
    mask: u64,
}

const _: () = {
    // DCache CHUNK_COUNT must be a power of two for ring buffer masking
    // This is checked at runtime in new() via the same pattern as MCache
};

impl<const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> Default
    for DCache<CHUNK_COUNT, CHUNK_SIZE>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> DCache<CHUNK_COUNT, CHUNK_SIZE> {
    const ASSERT_POWER_OF_TWO: () = assert!(is_power_of_two(CHUNK_COUNT));

    /// Create a fixed-size cache of payload chunks.
    ///
    /// CHUNK_COUNT must be a power of two.
    pub fn new() -> Self {
        let () = Self::ASSERT_POWER_OF_TWO;
        Self {
            chunks: core::array::from_fn(|_| DcacheChunk::new()),
            next: AtomicU64::new(0),
            mask: (CHUNK_COUNT - 1) as u64,
        }
    }

    /// Allocate a chunk index for a new payload (ring buffer style).
    ///
    /// This wraps around, so callers must use flow control (Fctl) to ensure
    /// chunks are not overwritten before consumers are done with them.
    pub fn allocate(&self) -> u32 {
        let seq = self.next.fetch_add(1, Ordering::AcqRel);
        (seq & self.mask) as u32
    }

    /// Returns the number of chunks in the cache.
    pub fn capacity(&self) -> usize {
        CHUNK_COUNT
    }

    /// Write payload bytes into a chunk, truncating to the chunk size.
    pub fn write_chunk(&self, chunk: u32, payload: &[u8]) -> Result<usize, TangoError> {
        let idx = chunk as usize;
        let Some(target) = self.chunks.get(idx) else {
            return Err(TangoError::ChunkOutOfRange(chunk));
        };
        let size = payload.len().min(CHUNK_SIZE);
        // SAFETY: This write is safe because:
        // 1. We have exclusive write access as the single producer
        // 2. The chunk index is bounds-checked via .get() above
        // 3. The subsequent MCache::publish() with Release ordering ensures
        //    this write is visible to consumers before they can read it
        // 4. Consumers only read after validating the sequence number
        unsafe {
            let data = &mut *target.data.get();
            data[..size].copy_from_slice(&payload[..size]);
        }
        Ok(size)
    }

    /// Read a view of a chunk with the provided size limit.
    pub fn read_chunk(
        &self,
        chunk: u32,
        size: usize,
    ) -> Result<DcacheView<'_, CHUNK_SIZE>, TangoError> {
        let idx = chunk as usize;
        let Some(target) = self.chunks.get(idx) else {
            return Err(TangoError::ChunkOutOfRange(chunk));
        };
        Ok(DcacheView {
            chunk: target,
            size: size.min(CHUNK_SIZE),
        })
    }

    /// Return the configured chunk size in bytes.
    pub fn chunk_size(&self) -> usize {
        CHUNK_SIZE
    }
}

#[derive(Clone, Copy)]
pub struct Producer<
    'a,
    const MCACHE_DEPTH: usize,
    const CHUNK_COUNT: usize,
    const CHUNK_SIZE: usize,
> {
    mcache: &'a MCache<MCACHE_DEPTH>,
    dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
    fseq: &'a Fseq,
    fctl: Option<&'a Fctl>,
    metrics: Option<&'a Metrics>,
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> fmt::Debug
    for Producer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Producer")
            .field("has_flow_control", &self.fctl.is_some())
            .field("has_metrics", &self.metrics.is_some())
            .finish()
    }
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize>
    Producer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    /// Create a producer for a shared mcache/dcache pair.
    ///
    /// Without flow control, this producer will freely overwrite chunks.
    /// Use `with_flow_control` for backpressure.
    pub fn new(
        mcache: &'a MCache<MCACHE_DEPTH>,
        dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
        fseq: &'a Fseq,
    ) -> Self {
        Self {
            mcache,
            dcache,
            fseq,
            fctl: None,
            metrics: None,
        }
    }

    /// Create a producer with credit-based flow control.
    ///
    /// The producer will acquire a credit before allocating a chunk.
    /// Initialize `fctl` with `CHUNK_COUNT` credits.
    pub fn with_flow_control(
        mcache: &'a MCache<MCACHE_DEPTH>,
        dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
        fseq: &'a Fseq,
        fctl: &'a Fctl,
    ) -> Self {
        Self {
            mcache,
            dcache,
            fseq,
            fctl: Some(fctl),
            metrics: None,
        }
    }

    /// Attach metrics tracking to this producer.
    ///
    /// Returns a new producer with the same configuration plus metrics.
    pub fn with_metrics(mut self, metrics: &'a Metrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Publish a payload fragment and its metadata.
    ///
    /// If flow control is enabled, returns `TangoError::NoCredits` when
    /// no credits are available (consumer hasn't caught up).
    #[must_use = "publishing may fail; check the result"]
    pub fn publish(
        &self,
        payload: &[u8],
        sig: u64,
        ctl: u16,
        ts: u32,
    ) -> Result<FragmentMetadata, TangoError> {
        // Acquire credit if flow control is enabled
        if let Some(fctl) = self.fctl {
            if !fctl.acquire(1) {
                if let Some(metrics) = self.metrics {
                    metrics.record_backpressure();
                }
                return Err(TangoError::NoCredits);
            }
        }

        let seq = self.fseq.next();
        let chunk = self.dcache.allocate();
        let size = self.dcache.write_chunk(chunk, payload)? as u32;
        let meta = FragmentMetadata {
            seq,
            sig,
            chunk,
            size,
            ctl,
            reserved: 0,
            ts,
        };
        self.mcache.publish(meta);

        if let Some(metrics) = self.metrics {
            metrics.record_publish();
        }

        Ok(meta)
    }

    /// Try to publish, spinning until credits are available or mcache stops.
    ///
    /// Only useful when flow control is enabled.
    #[must_use = "publishing may fail; check the result"]
    pub fn publish_blocking(
        &self,
        payload: &[u8],
        sig: u64,
        ctl: u16,
        ts: u32,
    ) -> Result<FragmentMetadata, TangoError> {
        loop {
            match self.publish(payload, sig, ctl, ts) {
                Ok(meta) => return Ok(meta),
                Err(TangoError::NoCredits) => {
                    if !self.mcache.is_running() {
                        return Err(TangoError::NoCredits);
                    }
                    core::hint::spin_loop();
                }
                Err(e) => return Err(e),
            }
        }
    }

    /// Publish multiple payloads in a batch.
    ///
    /// Returns the number of successfully published messages.
    /// Stops on the first error (e.g., `NoCredits`).
    ///
    /// This can be more efficient than calling `publish()` in a loop
    /// as it reduces function call overhead.
    pub fn publish_batch(&self, payloads: &[&[u8]], sig: u64, ctl: u16) -> usize {
        let mut published = 0;
        for (i, payload) in payloads.iter().enumerate() {
            match self.publish(payload, sig, ctl, i as u32) {
                Ok(_) => published += 1,
                Err(_) => break,
            }
        }
        published
    }
}

#[derive(Debug)]
pub struct Fragment<'a, const CHUNK_SIZE: usize> {
    pub meta: FragmentMetadata,
    pub payload: DcacheView<'a, CHUNK_SIZE>,
}

pub struct Consumer<
    'a,
    const MCACHE_DEPTH: usize,
    const CHUNK_COUNT: usize,
    const CHUNK_SIZE: usize,
> {
    mcache: &'a MCache<MCACHE_DEPTH>,
    dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
    fctl: Option<&'a Fctl>,
    metrics: Option<&'a Metrics>,
    next_seq: u64,
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> fmt::Debug
    for Consumer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Consumer")
            .field("next_seq", &self.next_seq)
            .field("has_flow_control", &self.fctl.is_some())
            .field("has_metrics", &self.metrics.is_some())
            .finish()
    }
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize>
    Consumer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    /// Create a consumer starting at the given sequence number.
    pub fn new(
        mcache: &'a MCache<MCACHE_DEPTH>,
        dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
        initial_seq: u64,
    ) -> Self {
        Self {
            mcache,
            dcache,
            fctl: None,
            metrics: None,
            next_seq: initial_seq,
        }
    }

    /// Create a consumer with credit-based flow control.
    ///
    /// The consumer will release a credit after consuming each fragment.
    /// Use the same `fctl` instance as the producer.
    pub fn with_flow_control(
        mcache: &'a MCache<MCACHE_DEPTH>,
        dcache: &'a DCache<CHUNK_COUNT, CHUNK_SIZE>,
        fctl: &'a Fctl,
        initial_seq: u64,
    ) -> Self {
        Self {
            mcache,
            dcache,
            fctl: Some(fctl),
            metrics: None,
            next_seq: initial_seq,
        }
    }

    /// Attach metrics tracking to this consumer.
    ///
    /// Returns a new consumer with the same configuration plus metrics.
    pub fn with_metrics(mut self, metrics: &'a Metrics) -> Self {
        self.metrics = Some(metrics);
        self
    }

    /// Poll for the next fragment without blocking.
    ///
    /// Returns:
    /// - `Ok(Some(fragment))` if a fragment was available
    /// - `Ok(None)` if the sequence is not ready yet
    /// - `Err(TangoError::Overrun)` if the consumer was lapped
    #[must_use = "polling may return data or an error; check the result"]
    pub fn poll(&mut self) -> Result<Option<Fragment<'a, CHUNK_SIZE>>, TangoError> {
        let seq = self.next_seq;
        match self.mcache.try_read(seq) {
            ReadResult::Ok(meta) => {
                let payload = self.dcache.read_chunk(meta.chunk, meta.size as usize)?;
                self.next_seq = seq + 1;

                // Release credit after consuming
                if let Some(fctl) = self.fctl {
                    fctl.release(1);
                }

                if let Some(metrics) = self.metrics {
                    metrics.record_consume();
                }

                Ok(Some(Fragment { meta, payload }))
            }
            ReadResult::NotReady => Ok(None),
            ReadResult::Overrun => {
                if let Some(metrics) = self.metrics {
                    metrics.record_overrun();
                }
                Err(TangoError::Overrun)
            }
        }
    }

    /// Busy-wait for the next fragment.
    ///
    /// Returns:
    /// - `Ok(Some(fragment))` if a fragment was received
    /// - `Ok(None)` if the mcache was stopped
    /// - `Err(TangoError::Overrun)` if the consumer was lapped
    #[must_use = "waiting may return data or an error; check the result"]
    pub fn wait(&mut self) -> Result<Option<Fragment<'a, CHUNK_SIZE>>, TangoError> {
        let seq = self.next_seq;
        match self.mcache.wait(seq) {
            ReadResult::Ok(meta) => {
                let payload = self.dcache.read_chunk(meta.chunk, meta.size as usize)?;
                self.next_seq = seq + 1;

                // Release credit after consuming
                if let Some(fctl) = self.fctl {
                    fctl.release(1);
                }

                if let Some(metrics) = self.metrics {
                    metrics.record_consume();
                }

                Ok(Some(Fragment { meta, payload }))
            }
            ReadResult::NotReady => Ok(None),
            ReadResult::Overrun => {
                if let Some(metrics) = self.metrics {
                    metrics.record_overrun();
                }
                Err(TangoError::Overrun)
            }
        }
    }

    /// Return the next sequence number the consumer expects.
    pub fn next_seq(&self) -> u64 {
        self.next_seq
    }

    /// Manually release credits (useful for batch processing).
    ///
    /// Call this after you're done processing a batch of fragments
    /// if you want to delay credit release for better throughput.
    pub fn release_credits(&self, count: u64) {
        if let Some(fctl) = self.fctl {
            fctl.release(count);
        }
    }

    /// Poll for multiple fragments at once, up to `max_count`.
    ///
    /// Returns a vector of fragments (up to `max_count`) that were available.
    /// Stops on the first `NotReady` or error.
    ///
    /// This is more efficient than calling `poll()` in a loop when you expect
    /// multiple messages to be available.
    ///
    /// Only available with the `std` feature.
    #[cfg(feature = "std")]
    pub fn poll_batch(
        &mut self,
        max_count: usize,
    ) -> Result<std::vec::Vec<Fragment<'a, CHUNK_SIZE>>, TangoError> {
        let mut fragments = std::vec::Vec::with_capacity(max_count);
        for _ in 0..max_count {
            match self.poll() {
                Ok(Some(fragment)) => fragments.push(fragment),
                Ok(None) => break,
                Err(e) => {
                    if fragments.is_empty() {
                        return Err(e);
                    }
                    break;
                }
            }
        }
        Ok(fragments)
    }
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> IntoIterator
    for Consumer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    type Item = Result<Fragment<'a, CHUNK_SIZE>, TangoError>;
    type IntoIter = ConsumerIter<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>;

    /// Convert this consumer into an iterator that busy-waits for messages.
    ///
    /// The iterator yields `Result<Fragment, TangoError>` and will:
    /// - Yield `Ok(fragment)` for each successfully consumed message
    /// - Yield `Err(TangoError::Overrun)` if the consumer was lapped
    /// - Return `None` when the MCache is stopped
    ///
    /// # Example
    ///
    /// ```ignore
    /// for result in consumer {
    ///     match result {
    ///         Ok(fragment) => println!("Got: {:?}", fragment.payload.as_slice()),
    ///         Err(e) => eprintln!("Error: {}", e),
    ///     }
    /// }
    /// ```
    fn into_iter(self) -> Self::IntoIter {
        ConsumerIter { consumer: self }
    }
}

/// An iterator over fragments from a [`Consumer`].
///
/// Created by [`Consumer::into_iter`]. This iterator busy-waits for messages
/// and yields `Result<Fragment, TangoError>`.
pub struct ConsumerIter<
    'a,
    const MCACHE_DEPTH: usize,
    const CHUNK_COUNT: usize,
    const CHUNK_SIZE: usize,
> {
    consumer: Consumer<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>,
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> fmt::Debug
    for ConsumerIter<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConsumerIter")
            .field("consumer", &self.consumer)
            .finish()
    }
}

impl<'a, const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> Iterator
    for ConsumerIter<'a, MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    type Item = Result<Fragment<'a, CHUNK_SIZE>, TangoError>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.consumer.wait() {
            Ok(Some(fragment)) => Some(Ok(fragment)),
            Ok(None) => None, // MCache stopped
            Err(e) => Some(Err(e)),
        }
    }
}

/// Builder for creating tango channels with ergonomic configuration.
///
/// # Example
///
/// ```
/// use rust_tango::ChannelBuilder;
///
/// // Create channel components with the builder
/// let (mcache, dcache, fseq, fctl, metrics) = ChannelBuilder::<64, 64, 256>::new()
///     .with_flow_control()
///     .with_metrics()
///     .build();
///
/// // Create producer and consumer from the components
/// use rust_tango::{Producer, Consumer};
/// let producer = Producer::with_flow_control(&mcache, &dcache, &fseq, fctl.as_ref().unwrap());
/// let mut consumer = Consumer::with_flow_control(&mcache, &dcache, fctl.as_ref().unwrap(), 1);
/// ```
#[derive(Debug)]
pub struct ChannelBuilder<
    const MCACHE_DEPTH: usize,
    const CHUNK_COUNT: usize,
    const CHUNK_SIZE: usize,
> {
    initial_seq: u64,
    flow_control: bool,
    metrics: bool,
}

impl<const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize> Default
    for ChannelBuilder<MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const MCACHE_DEPTH: usize, const CHUNK_COUNT: usize, const CHUNK_SIZE: usize>
    ChannelBuilder<MCACHE_DEPTH, CHUNK_COUNT, CHUNK_SIZE>
{
    /// Create a new channel builder with default settings.
    pub fn new() -> Self {
        Self {
            initial_seq: 1,
            flow_control: false,
            metrics: false,
        }
    }

    /// Set the initial sequence number (default: 1).
    pub fn initial_seq(mut self, seq: u64) -> Self {
        self.initial_seq = seq;
        self
    }

    /// Enable credit-based flow control.
    pub fn with_flow_control(mut self) -> Self {
        self.flow_control = true;
        self
    }

    /// Enable metrics tracking.
    pub fn with_metrics(mut self) -> Self {
        self.metrics = true;
        self
    }

    /// Build the channel components.
    ///
    /// Returns a tuple of:
    /// - `MCache` - the metadata ring buffer
    /// - `DCache` - the data chunk storage
    /// - `Fseq` - the sequence counter
    /// - `Option<Fctl>` - flow control (if enabled)
    /// - `Option<Metrics>` - metrics (if enabled)
    #[must_use = "this returns the channel components that should be used"]
    pub fn build(
        self,
    ) -> (
        MCache<MCACHE_DEPTH>,
        DCache<CHUNK_COUNT, CHUNK_SIZE>,
        Fseq,
        Option<Fctl>,
        Option<Metrics>,
    ) {
        let mcache = MCache::new();
        let dcache = DCache::new();
        let fseq = Fseq::new(self.initial_seq);
        let fctl = if self.flow_control {
            Some(Fctl::new(CHUNK_COUNT as u64))
        } else {
            None
        };
        let metrics = if self.metrics {
            Some(Metrics::new())
        } else {
            None
        };
        (mcache, dcache, fseq, fctl, metrics)
    }
}

#[cfg(all(test, feature = "std", not(loom)))]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::thread;

    const MCACHE_DEPTH: usize = 8;
    const CHUNK_COUNT: usize = 8;
    const CHUNK_SIZE: usize = 64;

    #[test]
    fn publish_and_consume() {
        let mcache = MCache::<MCACHE_DEPTH>::new();
        let dcache = DCache::<CHUNK_COUNT, CHUNK_SIZE>::new();
        let fseq = Fseq::new(1);
        let producer = Producer::new(&mcache, &dcache, &fseq);
        let mut consumer = Consumer::new(&mcache, &dcache, 1);

        let meta = producer.publish(b"hello", 42, 7, 1234).expect("publish");
        assert_eq!(meta.seq, 1);

        let fragment = consumer.poll().expect("poll").expect("fragment");
        assert_eq!(fragment.meta.sig, 42);
        assert_eq!(fragment.payload.read(), b"hello");
    }

    #[test]
    fn publish_and_consume_with_flow_control() {
        let mcache = MCache::<MCACHE_DEPTH>::new();
        let dcache = DCache::<CHUNK_COUNT, CHUNK_SIZE>::new();
        let fseq = Fseq::new(1);
        let fctl = Fctl::new(CHUNK_COUNT as u64);

        let producer = Producer::with_flow_control(&mcache, &dcache, &fseq, &fctl);
        let mut consumer = Consumer::with_flow_control(&mcache, &dcache, &fctl, 1);

        // Should be able to publish up to CHUNK_COUNT messages
        for i in 0..CHUNK_COUNT {
            producer
                .publish(b"test", i as u64, 0, 0)
                .expect("publish should succeed");
        }

        // Next publish should fail - no credits
        assert!(matches!(
            producer.publish(b"fail", 0, 0, 0),
            Err(TangoError::NoCredits)
        ));

        // Consume one message - releases a credit
        let _ = consumer.poll().expect("poll").expect("fragment");

        // Now we can publish again
        producer
            .publish(b"success", 0, 0, 0)
            .expect("publish should succeed after credit release");
    }

    #[test]
    fn detect_overrun() {
        let mcache = MCache::<4>::new();
        let dcache = DCache::<8, 64>::new();
        let fseq = Fseq::new(1);

        let producer = Producer::new(&mcache, &dcache, &fseq);
        let mut consumer = Consumer::new(&mcache, &dcache, 1);

        // Publish more messages than mcache depth, causing overwrite
        for i in 0..8u64 {
            producer.publish(b"msg", i, 0, 0).expect("publish");
        }

        // Consumer at seq=1 should detect overrun (slot was overwritten)
        assert!(matches!(consumer.poll(), Err(TangoError::Overrun)));
    }

    #[test]
    fn read_result_not_ready() {
        let mcache = MCache::<8>::new();

        // Try to read seq=1 before anything is published
        assert!(matches!(mcache.try_read(1), ReadResult::NotReady));
    }

    #[test]
    fn publish_and_consume_across_threads() {
        let mcache = MCache::<64>::new();
        let dcache = DCache::<64, 64>::new();
        let fseq = Fseq::new(1);
        let producer = Producer::new(&mcache, &dcache, &fseq);
        let consumer = Consumer::new(&mcache, &dcache, 1);
        let received = AtomicUsize::new(0);

        thread::scope(|scope| {
            scope.spawn(|| {
                let mut consumer = consumer;
                while received.load(Ordering::Acquire) < 3 {
                    match consumer.poll() {
                        Ok(Some(fragment)) => {
                            let payload = fragment.payload.read();
                            println!("received: {:?}", String::from_utf8_lossy(&payload));
                            assert!(payload.starts_with(b"msg-"));
                            received.fetch_add(1, Ordering::AcqRel);
                        }
                        Ok(None) => thread::yield_now(),
                        Err(e) => panic!("unexpected error: {}", e),
                    }
                }
            });

            scope.spawn(|| {
                for idx in 0..3u8 {
                    let payload = [b'm', b's', b'g', b'-', b'0' + idx];
                    producer
                        .publish(&payload, 0xAA, 0, idx as u32)
                        .expect("publish");
                }
            });
        });

        assert_eq!(received.load(Ordering::Acquire), 3);
    }

    #[test]
    fn flow_control_across_threads() {
        let mcache = MCache::<64>::new();
        let dcache = DCache::<64, 64>::new();
        let fseq = Fseq::new(1);
        let fctl = Fctl::new(64);

        let producer = Producer::with_flow_control(&mcache, &dcache, &fseq, &fctl);
        let consumer = Consumer::with_flow_control(&mcache, &dcache, &fctl, 1);
        let received = AtomicUsize::new(0);

        thread::scope(|scope| {
            scope.spawn(|| {
                let mut consumer = consumer;
                while received.load(Ordering::Acquire) < 100 {
                    match consumer.poll() {
                        Ok(Some(_)) => {
                            received.fetch_add(1, Ordering::AcqRel);
                        }
                        Ok(None) => thread::yield_now(),
                        Err(e) => panic!("unexpected error: {}", e),
                    }
                }
            });

            scope.spawn(|| {
                for i in 0..100u32 {
                    // Use blocking publish since consumer might be slow
                    producer
                        .publish_blocking(b"test", i as u64, 0, i)
                        .expect("publish");
                }
            });
        });

        assert_eq!(received.load(Ordering::Acquire), 100);
    }

    #[test]
    fn metrics_tracking() {
        let mcache = MCache::<8>::new();
        let dcache = DCache::<16, 64>::new();
        let fseq = Fseq::new(1);
        let fctl = Fctl::new(8);
        let metrics = Metrics::new();

        let producer =
            Producer::with_flow_control(&mcache, &dcache, &fseq, &fctl).with_metrics(&metrics);
        let mut consumer =
            Consumer::with_flow_control(&mcache, &dcache, &fctl, 1).with_metrics(&metrics);

        // Publish 5 messages
        for i in 0..5 {
            producer.publish(b"test", i, 0, 0).expect("publish");
        }

        // Consume 3 messages
        for _ in 0..3 {
            consumer.poll().expect("poll").expect("fragment");
        }

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.published, 5);
        assert_eq!(snapshot.consumed, 3);
        assert_eq!(snapshot.lag(), 2);
        assert_eq!(snapshot.overruns, 0);
        assert_eq!(snapshot.backpressure_events, 0);
    }

    #[test]
    fn metrics_backpressure_tracking() {
        let mcache = MCache::<8>::new();
        let dcache = DCache::<8, 64>::new();
        let fseq = Fseq::new(1);
        let fctl = Fctl::new(2); // Only 2 credits
        let metrics = Metrics::new();

        let producer =
            Producer::with_flow_control(&mcache, &dcache, &fseq, &fctl).with_metrics(&metrics);

        // Publish 2 messages (uses all credits)
        producer.publish(b"1", 1, 0, 0).expect("first");
        producer.publish(b"2", 2, 0, 0).expect("second");

        // Third publish should fail and record backpressure
        assert!(producer.publish(b"3", 3, 0, 0).is_err());

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.published, 2);
        assert_eq!(snapshot.backpressure_events, 1);
    }

    #[test]
    fn zero_copy_read() {
        let mcache = MCache::<8>::new();
        let dcache = DCache::<8, 64>::new();
        let fseq = Fseq::new(1);
        let producer = Producer::new(&mcache, &dcache, &fseq);
        let mut consumer = Consumer::new(&mcache, &dcache, 1);

        producer.publish(b"hello world", 42, 0, 0).expect("publish");

        let fragment = consumer.poll().expect("poll").expect("fragment");

        // Zero-copy access
        let slice = fragment.payload.as_slice();
        assert_eq!(slice, b"hello world");
        assert_eq!(fragment.payload.len(), 11);
        assert!(!fragment.payload.is_empty());
    }
}