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
//! # Moirai - Weaving the Threads of Fate
//!
//! Moirai is a high-performance hybrid concurrency library for Rust that seamlessly
//! blends asynchronous and parallel execution models. Named after the Greek Fates
//! who controlled the threads of life, Moirai weaves together the best principles
//! from async task scheduling and parallel work-stealing into a unified framework.
//!
//! ## Core Design Principles
//!
//! Moirai follows elite programming practices:
//! - **SOLID**: Single responsibility, open/closed, Liskov substitution, interface segregation, dependency inversion
//! - **CUPID**: Composable, Unix philosophy, predictable, idiomatic, domain-centric
//! - **GRASP**: Information expert, creator, controller, low coupling, high cohesion
//! - **ACID**: Atomicity, consistency, isolation, durability in task execution
//!
//! ## Features
//!
//! - **Zero-cost abstractions**: All abstractions compile away to optimal code
//! - **Hybrid execution**: Seamlessly mix async and parallel tasks
//! - **Work-stealing scheduler**: Intelligent load balancing across CPU cores
//! - **Memory safety**: Leverage Rust's ownership system for safe concurrency
//! - **High performance**: Sub-microsecond task scheduling overhead
//! - **NUMA awareness**: Optimize for modern multi-socket systems
//! - **Rich iterator combinators**: Parallel and async iterator processing
//! - **IPC**: Inter-process communication (optional)
//! - **Metrics**: Performance monitoring (optional)
//! - **Distributed transport feature gates**: Optional transport and iterator helpers without a
//! facade-level remote-closure API
//!
//! ## Performance Characteristics
//!
//! - **Task scheduling overhead**: < 1μs per task
//! - **Memory efficiency**: Zero-copy task passing where possible
//! - **Scalability**: Linear scaling up to CPU core count
//! - **SIMD optimization**: 4-8x performance improvement for vectorizable workloads
//! - **NUMA awareness**: Reduced memory latency on multi-socket systems
//!
//! ## Safety Guarantees
//!
//! - **Memory safety**: All operations are memory-safe by construction
//! - **Data race freedom**: Rust's ownership system prevents data races
//! - **Deadlock prevention**: Lock-free data structures where possible
//! - **Resource cleanup**: Automatic resource cleanup on task completion
//! - **Error handling**: Comprehensive error types with recovery mechanisms
//!
//! ## Quick Start Example
//!
//! ```rust
//! use moirai::Moirai;
//! use std::sync::atomic::{AtomicU32, Ordering};
//! use std::sync::Arc;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a new runtime with optimal configuration
//! let runtime = Moirai::builder()
//! .worker_threads(4)
//! .build()?;
//!
//! // CPU-bound parallel computation
//! let counter = Arc::new(AtomicU32::new(0));
//! let counter_clone = counter.clone();
//! let parallel_handle = runtime.spawn_fn(move || {
//! // Simulate intensive computation
//! for i in 0..1000 {
//! counter_clone.fetch_add(i % 100, Ordering::Relaxed);
//! }
//! counter_clone.load(Ordering::Relaxed)
//! });
//!
//! // Another parallel task
//! let critical_handle = runtime.spawn_fn(move || "critical task executed");
//!
//! // Tasks execute concurrently with optimal scheduling
//! let parallel_result = parallel_handle.join().unwrap().unwrap();
//! let critical_result = critical_handle.join().unwrap().unwrap();
//!
//! println!("Parallel result: {}", parallel_result);
//! println!("Critical result: {}", critical_result);
//!
//! // Graceful shutdown with resource cleanup
//! runtime.shutdown();
//! # Ok(())
//! # }
//! ```
//!
//! ## Advanced Usage Patterns
//!
//! ### Task Chaining and Composition
//!
//! ```rust
//! use moirai::Moirai;
//!
//! # fn chaining_example() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = Moirai::new()?;
//!
//! // Chain tasks with dependencies using regular closures
//! let handle1 = runtime.spawn_fn(|| 42);
//! let result1 = handle1.join().unwrap().unwrap();
//!
//! let handle2 = runtime.spawn_fn(move || result1 * 2);
//! let result2 = handle2.join().unwrap().unwrap();
//!
//! let handle3 = runtime.spawn_fn(move || result2 + 10);
//! let result = handle3.join().unwrap().unwrap();
//!
//! assert_eq!(result, 94); // (42 * 2) + 10
//! # Ok(())
//! # }
//! ```
//!
//! ### Distributed Boundary
//!
//! ```rust
//! use moirai::Moirai;
//!
//! # fn boundary_example() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = Moirai::builder()
//! .worker_threads(2)
//! .build()?;
//!
//! // Execute task locally through the verified scheduler facade.
//! let handle = runtime.spawn_fn(move || "computed locally");
//! let result = handle.join().unwrap().unwrap();
//! println!("Result: {}", result);
//!
//! // Cross-machine execution uses fixed-format capability tokens; arbitrary
//! // remote closure execution is intentionally outside the public facade.
//! # Ok(())
//! # }
//! ```
//!
//! ## Migration Guide
//!
//! ### From `std::thread`
//!
//! ```rust
//! # fn expensive_computation() -> i32 { 42 }
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Before: std::thread
//! let handle = std::thread::spawn(|| {
//! expensive_computation()
//! });
//! let result = handle.join().unwrap();
//!
//! // After: Moirai
//! let runtime = moirai::Moirai::new()?;
//! let handle = runtime.spawn_fn(|| {
//! expensive_computation()
//! });
//! let result = handle.join().unwrap().unwrap();
//! # Ok(())
//! # }
//! ```
//!
//! ### From Tokio
//!
//! ```rust
//! # fn async_operation() -> String { "result".to_string() }
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Before: std::thread (since tokio requires async context)
//! let handle = std::thread::spawn(|| {
//! async_operation()
//! });
//! let result = handle.join().unwrap();
//!
//! // After: Moirai
//! let runtime = moirai::Moirai::new()?;
//! let handle = runtime.spawn_fn(|| {
//! async_operation()
//! });
//! let result = handle.join().unwrap().unwrap();
//! # Ok(())
//! # }
//! ```
//!
//! ### From Rayon
//!
//! ```rust
//! # fn expensive_transform(x: &i32) -> i32 { x * 2 }
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let data = vec![1, 2, 3, 4, 5];
//!
//! // Before: Sequential processing
//! let result: Vec<_> = data.iter()
//! .map(|x| expensive_transform(x))
//! .collect();
//!
//! // After: Moirai parallel processing
//! let runtime = moirai::Moirai::new()?;
//! let handles: Vec<_> = data.iter()
//! .map(|&x| runtime.spawn_fn(move || expensive_transform(&x)))
//! .collect();
//! let result: Result<Vec<_>, _> = handles.into_iter()
//! .map(|h| h.join().unwrap())
//! .collect();
//! # Ok(())
//! # }
//! ```
// Re-export core functionality (avoiding ExecutorStats conflict)
pub use ;
// Re-export executor functionality
pub use ;
pub use ;
/// Completion-only borrowing scope for jobs submitted to the unified scheduler.
pub type MoiraiScope<'scope> = ;
// Re-export transport functionality. The typed cross-boundary channel is the
// rkyv-style archive pair (`ArchivedUniversalSender`/`ArchivedUniversalReceiver`);
// the old non-functional `Universal*` placeholders were removed.
/// Shared-memory same-machine IPC transport (Unix/Windows only).
pub use IpcTransport;
pub use ;
pub use ;
pub use ;
pub use ;
// Re-export channel functionality from core
pub use channel;
pub use TcpTransport;
// Re-export synchronization primitives
pub use ;
// Re-export metrics functionality
pub use MetricsCollector;
// Re-export async functionality (specific imports to avoid conflicts)
pub use ;
pub use ;
pub use ;
// Re-export iterator functionality
pub use ;
// Re-export GPU functionality
pub use *;
// Synchronous data-parallel primitives (rayon-replacement surface), provided by
// the `moirai-parallel` domain crate: monomorphized ExecutionPolicy + the
// adaptive `par_*` helpers.
pub use *;
pub use *;
// Submodules
/// Convenience functions for common operations.
///
/// Common imports for Moirai users.
// Facade re-exports
pub use MoiraiBuilder;
pub use ;
pub use Moirai;