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
//! High-performance conflation slots for latest-value-wins scenarios.
//!
//! Two variants based on reader topology:
//!
//! - [`spsc`] — Single producer, single consumer. Lowest overhead.
//! - [`spmc`] — Single producer, multiple consumers. [`SharedReader`](spmc::SharedReader) is `Clone`.
//!
//! Both use a seqlock internally: the writer increments a sequence counter,
//! copies data via word-at-a-time atomics, and increments again. Readers
//! speculatively copy and retry if the sequence changed.
//!
//! # The `Pod` Trait
//!
//! Types must implement [`Pod`] (Plain Old Data) — no heap allocations,
//! no drop glue, byte-copyable. Any `Copy` type implements `Pod` automatically.
//!
//! ```rust
//! use nexus_slot::Pod;
//!
//! #[repr(C)]
//! struct OrderBook {
//! bids: [f64; 20],
//! asks: [f64; 20],
//! sequence: u64,
//! }
//!
//! // SAFETY: OrderBook is just bytes — no heap allocations
//! unsafe impl Pod for OrderBook {}
//! ```
//!
//! # Examples
//!
//! ```rust
//! #[derive(Copy, Clone, Default)]
//! struct Quote { bid: f64, ask: f64, seq: u64 }
//!
//! // SPSC — single reader
//! let (mut writer, mut reader) = nexus_slot::spsc::slot::<Quote>();
//! writer.write(Quote { bid: 100.0, ask: 100.05, seq: 1 });
//! assert_eq!(reader.read().unwrap().seq, 1);
//! ```
//!
//! ```rust
//! #[derive(Copy, Clone, Default)]
//! struct Quote { bid: f64, ask: f64, seq: u64 }
//!
//! // SPMC — multiple readers
//! let (mut writer, mut reader1) = nexus_slot::spmc::shared_slot::<Quote>();
//! let mut reader2 = reader1.clone();
//!
//! writer.write(Quote { bid: 100.0, ask: 100.05, seq: 1 });
//! assert!(reader1.read().is_some());
//! assert!(reader2.read().is_some()); // independent consumption
//! ```
use ;
use ;
/// Marker trait for types safe to use in a conflated slot.
///
/// # Safety
///
/// Implementor guarantees:
///
/// 1. **No heap allocations**: No `Vec`, `String`, `Box`, `Arc`, etc.
/// 2. **No owned resources**: No `File`, `TcpStream`, `Mutex`, etc.
/// 3. **No drop glue**: `std::mem::needs_drop::<Self>()` returns false.
/// 4. **Byte-copyable**: Safe to memcpy without cleanup.
///
/// Essentially: the type could be `Copy`, but chooses not to.
///
/// # Example
///
/// ```rust
/// use nexus_slot::Pod;
///
/// #[repr(C)]
/// struct OrderBook {
/// bids: [f64; 20],
/// asks: [f64; 20],
/// bid_count: u8,
/// ask_count: u8,
/// sequence: u64,
/// }
///
/// // SAFETY: Just bytes, no heap
/// unsafe impl Pod for OrderBook {}
/// ```
pub unsafe
// Any Copy type is Pod
unsafe
/// Atomically stores `size_of::<T>()` bytes into shared memory.
///
/// Word-at-a-time `AtomicUsize` stores when alignment permits,
/// `AtomicU8` fallback for tail bytes or poorly-aligned types.
/// All stores use `Relaxed` ordering — caller provides fences.
///
/// # Safety
///
/// - `dst` must be valid for `size_of::<T>()` bytes
/// - `dst` must be aligned to `align_of::<T>()`
/// - `dst` must be derived from `UnsafeCell` (shared-mutable provenance)
pub unsafe
/// Atomically loads `size_of::<T>()` bytes from shared memory.
///
/// Word-at-a-time `AtomicUsize` loads when alignment permits,
/// `AtomicU8` fallback for tail bytes or poorly-aligned types.
/// All loads use `Relaxed` ordering — caller provides fences.
///
/// # Safety
///
/// - `src` must be valid for `size_of::<T>()` bytes
/// - `src` must be aligned to `align_of::<T>()`
/// - `src` must be derived from `UnsafeCell` (shared-mutable provenance)
pub unsafe