moirai-async 0.7.0

Async/await support for Moirai concurrency library
Documentation
//! Async/await support for Moirai concurrency library.
//!
//! This module provides async runtime integration for Moirai, enabling seamless
//! interop between sync and async tasks while maintaining high performance.
//!
//! Following SLAP principle, this module now serves as a facade that re-exports
//! functionality from focused, single-responsibility modules.

#![deny(missing_docs)]
// Focused modules following SLAP principle. The thread-based executor and
// descriptor-backed networking use native synchronization and OS sockets;
// browser callers use the PAL's event-loop handles instead.
mod blocking;
#[cfg(not(target_arch = "wasm32"))]
pub mod executor;
pub mod fs;
pub mod io;
#[cfg(any(unix, windows))]
pub mod net;
pub mod sync;
pub mod timer;

// Re-export io functionality
pub use io::{
    AsyncBufRead, AsyncLength, AsyncMemReader, AsyncRead, AsyncReadAt, AsyncReadExt, AsyncWrite,
    AsyncWriteExt, MoiraiCompat, TokioCompat,
};

// Re-export async executor functionality on native targets.
#[cfg(not(target_arch = "wasm32"))]
pub use executor::{AsyncExecutor, AsyncHandle, ExecutorStats};

// Re-export timer functionality
pub use timer::{
    Delay, Interval, RateLimiter, RatePermit, Timeout, TimeoutError, TimerCommand, TimerWheel,
    interval, interval_at, sleep, timeout,
};

// Re-export networking functionality on native targets.
#[cfg(any(unix, windows))]
pub use net::{
    ConnectionInfo, ConnectionPool, ConnectionStats, TcpListener, TcpServerConfig, TcpServerStats,
    TcpStream, UdpConfig, UdpSocket, UdpSocketStats,
};

// Re-export file I/O functionality
pub use fs::{
    File, FileOpenOptions, FileStats, append, append_str, copy, create_dir, create_dir_all,
    metadata, read, read_to_string, remove_dir, remove_dir_all, remove_file, rename, write,
    write_str,
};

// Re-export sync primitives
pub use sync::{
    Broadcast, BroadcastError, BroadcastReceiver, BroadcastSender, Condvar, Mutex, MutexGuard,
    Notify, RwLock, Semaphore, SemaphorePermit, Watch, WatchError, WatchReceiver, WatchSender,
};

// The generated entry point constructs the native executor, so keep it out of
// the browser-facing API where that executor is unavailable.
#[cfg(not(target_arch = "wasm32"))]
pub use moirai_async_macros::main;

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    #[test]
    fn test_integration_async_executor() {
        let executor = AsyncExecutor::new().expect("Failed to create executor");

        let _handle = executor.spawn(async { "async task completed" });

        // Run one iteration manually for testing
        // In production, executor.run() would be called
        let stats_before = executor.stats();
        assert_eq!(stats_before.tasks_spawned, 1);
        assert_eq!(stats_before.tasks_pending, 1);

        // For now, we can't test the full execution without running the executor
        // This test validates the spawning mechanism works
    }

    #[test]
    fn test_integration_executor_stats() {
        let executor = AsyncExecutor::new().expect("Failed to create executor");

        let stats = executor.stats();
        assert_eq!(stats.tasks_spawned, 0);
        assert_eq!(stats.tasks_completed, 0);
        assert_eq!(stats.tasks_pending, 0);
        assert_eq!(stats.io_operations, 0);
    }

    #[test]
    fn test_integration_reactor_access() {
        let executor = AsyncExecutor::new().expect("Failed to create executor");

        // Verify we can access the underlying reactor
        let _reactor = executor.reactor();

        // This confirms the integration with PAL reactor
    }
}