Skip to main content

moirai_async/
lib.rs

1//! Async/await support for Moirai concurrency library.
2//!
3//! This module provides async runtime integration for Moirai, enabling seamless
4//! interop between sync and async tasks while maintaining high performance.
5//!
6//! Following SLAP principle, this module now serves as a facade that re-exports
7//! functionality from focused, single-responsibility modules.
8
9#![deny(missing_docs)]
10// Focused modules following SLAP principle. The thread-based executor and
11// descriptor-backed networking use native synchronization and OS sockets;
12// browser callers use the PAL's event-loop handles instead.
13mod blocking;
14#[cfg(not(target_arch = "wasm32"))]
15pub mod executor;
16pub mod fs;
17pub mod io;
18#[cfg(any(unix, windows))]
19pub mod net;
20pub mod sync;
21pub mod timer;
22
23// Re-export io functionality
24pub use io::{
25    AsyncBufRead, AsyncLength, AsyncMemReader, AsyncRead, AsyncReadAt, AsyncReadExt, AsyncWrite,
26    AsyncWriteExt, MoiraiCompat, TokioCompat,
27};
28
29// Re-export async executor functionality on native targets.
30#[cfg(not(target_arch = "wasm32"))]
31pub use executor::{AsyncExecutor, AsyncHandle, ExecutorStats};
32
33// Re-export timer functionality
34pub use timer::{
35    Delay, Interval, RateLimiter, RatePermit, Timeout, TimeoutError, TimerCommand, TimerWheel,
36    interval, interval_at, sleep, timeout,
37};
38
39// Re-export networking functionality on native targets.
40#[cfg(any(unix, windows))]
41pub use net::{
42    ConnectionInfo, ConnectionPool, ConnectionStats, TcpListener, TcpServerConfig, TcpServerStats,
43    TcpStream, UdpConfig, UdpSocket, UdpSocketStats,
44};
45
46// Re-export file I/O functionality
47pub use fs::{
48    File, FileOpenOptions, FileStats, append, append_str, copy, create_dir, create_dir_all,
49    metadata, read, read_to_string, remove_dir, remove_dir_all, remove_file, rename, write,
50    write_str,
51};
52
53// Re-export sync primitives
54pub use sync::{
55    Broadcast, BroadcastError, BroadcastReceiver, BroadcastSender, Condvar, Mutex, MutexGuard,
56    Notify, RwLock, Semaphore, SemaphorePermit, Watch, WatchError, WatchReceiver, WatchSender,
57};
58
59// The generated entry point constructs the native executor, so keep it out of
60// the browser-facing API where that executor is unavailable.
61#[cfg(not(target_arch = "wasm32"))]
62pub use moirai_async_macros::main;
63
64#[cfg(all(test, not(target_arch = "wasm32")))]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_integration_async_executor() {
70        let executor = AsyncExecutor::new().expect("Failed to create executor");
71
72        let _handle = executor.spawn(async { "async task completed" });
73
74        // Run one iteration manually for testing
75        // In production, executor.run() would be called
76        let stats_before = executor.stats();
77        assert_eq!(stats_before.tasks_spawned, 1);
78        assert_eq!(stats_before.tasks_pending, 1);
79
80        // For now, we can't test the full execution without running the executor
81        // This test validates the spawning mechanism works
82    }
83
84    #[test]
85    fn test_integration_executor_stats() {
86        let executor = AsyncExecutor::new().expect("Failed to create executor");
87
88        let stats = executor.stats();
89        assert_eq!(stats.tasks_spawned, 0);
90        assert_eq!(stats.tasks_completed, 0);
91        assert_eq!(stats.tasks_pending, 0);
92        assert_eq!(stats.io_operations, 0);
93    }
94
95    #[test]
96    fn test_integration_reactor_access() {
97        let executor = AsyncExecutor::new().expect("Failed to create executor");
98
99        // Verify we can access the underlying reactor
100        let _reactor = executor.reactor();
101
102        // This confirms the integration with PAL reactor
103    }
104}