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
//! # Round Pipers
//!
//! A high-performance Rust library for streaming data processing with circular buffers and read-only pipes.
//! Designed for simple developers who want to process data without dealing with complex Rust concepts.
//!
//! ## Features
//!
//! - **Circular Buffer Pipes**: High-performance streaming with Linux `memfd` + double-mapped memory
//! - **Read-Only Pipes**: Zero-copy access to pre-populated data using lifetimes
//! - **Write-Only Pipes**: Efficient buffer filling and streaming to external destinations
//! - **Unified API**: All pipe types provide identical reader interfaces
//! - **RAII Memory Management**: Automatic pointer advancement with `ChunkGuard`
//! - **Thread Safety**: Multiple readers, single writer model with `Send + Sync`
//! - **Multi-dimensional Support**: Built on `ndarray` for complex data shapes
//!
//! ## Quick Start
//!
//! ### Circular Buffer Pipes (Streaming Data)
//!
//! ```rust,no_run
//! use round_pipers::Pipe;
//! use std::sync::Arc;
//! use std::path::Path;
//!
//! # fn main() -> round_pipers::Result<()> {
//! // Create a pipe for streaming f64 values
//! let pipe = Arc::new(Pipe::new("my_buffer", 1024, [])?);
//! let writer = pipe.clone().get_writer()?;
//! let reader = pipe.clone().get_reader();
//!
//! // Write data
//! writer.write(100, |mut chunk, _state| {
//! for i in 0..100 {
//! chunk[i] = i as f64;
//! }
//! })?;
//!
//! // Read data with iterator - no lifetime management needed
//! for chunk_result in reader.iter_chunks(25, 25) {
//! let chunk = chunk_result?;
//! // Process chunk - automatically advances when dropped
//! for &value in chunk.iter() {
//! println!("Value: {}", value);
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Read-Only Pipes (Pre-populated Data)
//!
//! ```rust
//! use round_pipers::ReadOnlyPipe;
//!
//! # fn main() -> round_pipers::Result<()> {
//! // Super simple API - just pass your slice
//! let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
//! let pipe = ReadOnlyPipe::new(&data, [])?;
//! let reader = pipe.get_reader();
//!
//! // Same API as regular pipes - no learning curve
//! for chunk_result in reader.iter_chunks(2, 2) {
//! let chunk = chunk_result?;
//! for &value in chunk.iter() {
//! println!("Value: {}", value);
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Core Concepts
//!
//! ### Reading and Writing
//!
//! You write ndarrays of (relatively) arbitrary shape into a pipe by calling the `write` function on a pipe's writer, and pass in a closure
//! that gets an array to write to.
//!
//! You read ndarrays from a pipe by calling the `read` function on
//! a pipe's reader, and pass in a closure with the buffer to process, or by iterating over the
//! pipe using `iter_chunks()`. Generally you specify how much data to transfer (`n_to_read`),
//! and how much to advance your read pointer by (`n_to_consume`).
//!
//! - **Overlapping reads**: `n_to_consume` < `n_to_read` (sliding window)
//! - **Skip data**: `n_to_consume` > `n_to_read`
//! - **Normal processing**: `n_to_consume` = `n_to_read`
//!
//! ## Pipe Types
//!
//! ### 1. Circular Buffer Pipes (for streaming data)
//! A circular shared-memory buffer with support for a single writer and multiple readers.
//! Perfect for audio, signal processing, or streaming pipelines.
//! Uses Linux `memfd` + double-mapped memory for seamless wraparound.
//!
//! ### 2. Read-Only Pipes (for pre-populated data)
//! Access pre-existing data slices with the same API as circular pipes.
//! Simple lifetime-based approach - no data copying. Perfect for processing existing datasets or arrays.
//!
//! ### 3. Write-Only Pipes (for filling buffers and streaming)
//! Two variants for different use cases:
//!
//! #### WriteOnlyPipeBuffer
//! Write data into pre-allocated mutable slices until full.
//! Simple lifetime-based approach - no data copying, returns errors when full.
//! Perfect for filling output buffers or collecting results.
//!
//! #### WriteOnlyPipeStream
//! Write typed data to any std::io::Write implementor (files, sockets, etc.).
//! Owns a growable buffer, converts data to native-endian bytes and writes immediately.
//! Perfect for streaming data to files or network destinations with minimal allocations.
//!
//! ## Advanced Usage
//!
//! ### Multi-dimensional Data
//!
//! ```rust,no_run
//! use round_pipers::Pipe;
//! use ndarray::Ix4;
//! use std::sync::Arc;
//! use std::path::Path;
//!
//! # fn main() -> round_pipers::Result<()> {
//! // Create a 4D pipe for complex data structures
//! let pipe = Arc::new(Pipe::<f64, Ix4, ()>::new(
//! Path::new("multi_dim"),
//! 1000,
//! [10, 20, 30, 40] // Shape: 10×20×30×40
//! )?);
//!
//! let reader = pipe.clone().get_reader();
//! for chunk_result in reader.iter_chunks(5, 5) {
//! let chunk = chunk_result?;
//! // chunk is ArrayView<f64, Ix5> with shape [5, 10, 20, 30, 40]
//! println!("Chunk shape: {:?}", chunk.shape());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Peek Semantics (Sliding Window)
//!
//! ```rust,no_run
//! # use round_pipers::*;
//! # use std::sync::Arc;
//! # use std::path::Path;
//! # fn main() -> Result<()> {
//! # let pipe = Arc::new(Pipe::new("test", 1000, [])?);
//! # let reader = pipe.get_reader();
//! // Read 50 elements but only consume 10 (sliding window)
//! for chunk_result in reader.iter_chunks(50, 10) {
//! let chunk = chunk_result?;
//! // Next iteration will start 10 elements forward,
//! // but read 50 elements (40 overlap)
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Design Philosophy
//!
//! This library is designed for **simple developers** who want to process data without dealing with complex Rust concepts:
//!
//! ### You DON'T Need To Understand:
//! - Complex lifetime annotations
//! - Interior mutability patterns
//! - Trait object dynamics
//! - Memory layout details
//!
//! ### You DO Need To Know:
//! - Data must outlive read-only pipes (Rust will tell you if you get this wrong)
//! - Use `iter_chunks(n_to_read, n_to_consume)` for processing
//! - Chunks are automatically consumed when dropped
//! - All pipe types work the same way from a user perspective
//!
//! ## Performance
//!
//! - **Zero-copy** data access via ArrayView
//! - **Minimal allocations** (just reader state tracking)
//! - **Read-only pipes** have no synchronization overhead
//! - **Circular buffer pipes** handle writer synchronization automatically
//! - **Lock-free reading** in most cases with Fibonacci backoff for contention
//!
//! ## Error Handling
//!
//! - Clear error messages for common mistakes
//! - "Insufficient data" when trying to read past end
//! - "Reader not registered" for invalid reader usage
//! - All errors implement standard Rust error traits
//!
//! ## Requirements
//!
//! - Linux (uses `memfd_create` and `mmap`)
//! - Rust 1.70+
//!
//! # Example FFT Pipeline
//!
//! Here's a complete example demonstrating a multi-threaded audio processing pipeline.
//! You can run this example with: `cargo run --example audio_fft_pipeline`
//!
//! ```rust
//! ```
// Re-export main types and traits
pub use ;
pub use ChunkGuard;
pub use PipeState;
pub use ;
pub use ;
pub use ;
pub use ;