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 instead of providing your own executor instance. (by default not enabled, omit thesmoldependency) -
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}");With a custom executor:
use orb_smol::SmolRT;
use orb::prelude::*;
use std::sync::Arc;
use async_executor::Executor;
let executor = Arc::new(Executor::new());
let rt = SmolRT::new_with_executor(executor);
rt.block_on(async move {
for _ in 0..3 {
SmolRT::sleep(std::time::Duration::from_secs(1)).await;
}
println!("background task will stop once the block_on is finish");
});With the smol global thread (requires the global feature):
use orb_smol::SmolRT;
#[cfg(feature = "global")]
let rt = SmolRT::new_global();Structs§
- Blocking
Join Handle - SmolFD
- Associate type for SmolRT
- Smol
Interval - Associate type for SmolRT
- Smol
Join Handle - AsyncHandle implementation for smol
- SmolRT
- The SmolRT implements AsyncRuntime trait