Expand description
§Smol Runtime adapter for Orb framework
This crate provides a Smol-based implementation of the Orb async runtime traits. It allows users to leverage Smol’s lightweight async runtime with the unified Orb interface.
The main type provided is SmolRT, which implements the core runtime functionality.
See the Orb crate for more information.
§Features
-
global: Enables the global executor feature, which allows usingsmoldefault global executor which require polling in smol dependency, and requires setting (by default not enabled, ourSmolRT::multi()is more convenient) -
unwind: Use AssertUnwindSafe to capture panic inside the task, and return Err(()) to the task join handle. (by default not enabled, panic terminates the program)
§Usage
With multi thread runtime
use orb_smol::SmolRT;
use orb::prelude::*;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
use std::time::Duration;
let rt = SmolRT::multi(0); // spawn background thread with cpu number
let counter = Arc::new(AtomicUsize::new(0));
let _counter = counter.clone();
rt.spawn(async move {
loop {
SmolRT::sleep(Duration::from_secs(1)).await;
_counter.fetch_add(1, Ordering::SeqCst);
}
});
// background task will continue to run until rt is drop
std::thread::sleep(Duration::from_secs(3));
drop(rt);
let count = counter.load(Ordering::SeqCst);
assert!(count >= 2 && count <= 4, "{count}");§Static Spawn
This runtime supports static spawn methods through the AsyncRuntime trait
that use the current runtime context:
use orb::AsyncRuntime;
use orb::runtime::AsyncExec;
fn example<RT: AsyncRuntime>() {
let rt = RT::multi(2);
rt.block_on(async {
// Spawn a task using the static method - uses current runtime context
let handle = RT::spawn(async {
42
});
let result = handle.await.unwrap();
assert_eq!(result, 42);
// Spawn and detach a task
RT::spawn_detach(async {
println!("detached task running");
});
});
}The static spawn methods (AsyncRuntime::spawn, AsyncRuntime::spawn_detach) automatically use
the runtime context of the current thread. This is implemented using thread-local
storage that is registered when entering block_on or when worker threads are spawned.
This feature provides a unified interface across different runtime implementations
(smol, tokio, etc.) and fills the gap in async-executor’s native functionality.
Structs§
- Blocking
Join Handle - Smol
Exec - The SmolRT implements AsyncRuntime trait
- SmolFD
- Associate type for SmolRT
- Smol
Interval - Associate type for SmolRT
- Smol
Join Handle - AsyncJoiner implementation for smol
- SmolRT