moduvex_runtime/lib.rs
1#![allow(dead_code)] // Internal framework APIs — used by downstream crates
2//! moduvex-runtime — Custom async runtime for the Moduvex framework.
3//!
4//! Provides a cross-platform async runtime with:
5//! - Platform-native I/O (epoll, kqueue, IOCP)
6//! - Hybrid threading (thread-per-core default, opt-in work-stealing)
7//! - Hierarchical timer wheel
8//! - Async networking (TCP/UDP)
9//! - Synchronization primitives (mpsc, oneshot, mutex)
10//! - Signal handling
11//! - Task-local storage
12
13pub mod executor;
14pub mod net;
15pub mod platform;
16pub mod reactor;
17pub mod runtime;
18pub mod signal;
19pub mod sync;
20pub mod time;
21
22// ── Core re-exports ──────────────────────────────────────────────────────────
23
24pub use executor::task::{JoinError, JoinHandle};
25pub use executor::task_local::{AccessError, TaskLocal};
26pub use runtime::{Runtime, RuntimeBuilder};
27
28// ── Networking re-exports ────────────────────────────────────────────────────
29
30pub use net::{AsyncRead, AsyncWrite};
31pub use net::{TcpListener, TcpStream, UdpSocket};
32
33// ── Sync re-exports ──────────────────────────────────────────────────────────
34
35pub use sync::{Mutex, MutexGuard};
36
37// ── Top-level convenience functions ──────────────────────────────────────────
38
39/// Drive `future` to completion on the current thread, returning its output.
40///
41/// Creates a fresh single-threaded executor and runs the event loop until
42/// the future resolves. Suitable for the top-level entry point of an async
43/// program.
44///
45/// # Example
46/// ```
47/// let result = moduvex_runtime::block_on(async { 42u32 });
48/// assert_eq!(result, 42);
49/// ```
50pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
51 executor::block_on(future)
52}
53
54/// Drive `future` to completion, with `spawn` available inside the context.
55///
56/// Like `block_on` but registers the executor as the thread-local so that
57/// `spawn()` works within the future's async context.
58pub fn block_on_with_spawn<F: std::future::Future>(future: F) -> F::Output {
59 executor::block_on_with_spawn(future)
60}
61
62/// Spawn a future onto the current thread's executor.
63///
64/// Must be called from within a `block_on_with_spawn` or `Runtime::block_on`
65/// context.
66///
67/// # Panics
68/// Panics if called outside of an executor context.
69pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
70where
71 F: std::future::Future + 'static,
72 F::Output: Send + 'static,
73{
74 executor::spawn(future)
75}
76
77/// Sleep for the given duration.
78///
79/// # Example
80/// ```no_run
81/// use std::time::Duration;
82/// moduvex_runtime::block_on(async {
83/// moduvex_runtime::sleep(Duration::from_millis(100)).await;
84/// });
85/// ```
86pub async fn sleep(duration: std::time::Duration) {
87 time::sleep::sleep(duration).await;
88}
89
90/// Create a periodic interval timer.
91pub fn interval(period: std::time::Duration) -> time::interval::Interval {
92 time::interval::interval(period)
93}