Crates.io | miniloop |
lib.rs | miniloop |
version | 0.2.1 |
source | src |
created_at | 2024-11-14 22:59:28.026298 |
updated_at | 2024-12-02 16:38:16.609186 |
description | The simpliest async executor without heap memory allocation |
homepage | |
repository | https://github.com/vpetrigo/sntpc |
max_upload_size | |
id | 1448413 |
size | 38,837 |
This repository is created as an attempt to clarify some more low-level details about how things work in Rust asynchronous world.
The miniloop
executor creates a statically allocated list of tasks. That number should be available upon a crate
build:
MINILOOP_TASK_ARRAY_SIZE
: default value is 1
which means you can schedule a single task within the executor. To
override that just define an environment variable with the number of tasks you plan to use in your application.You can set up the environment variable in a shell prior to running the cargo build
command:
export MINILOOP_TASK_ARRAY_SIZE=10
$env:MINILOOP_TASK_ARRAY_SIZE = 10
Or you can use configurable environment feature by
creating a .cargo/config.toml
file with the following content:
[env]
MINILOOP_TASK_ARRAY_SIZE = "10"
Create your tasks on the stack, add them to the executor and enjoy!
use miniloop::executor::Executor;
use miniloop::helpers::yield_me;
fn sleep(s: u64) {
std::thread::sleep(std::time::Duration::from_secs(s));
}
async fn dummy_func(data: &str) {
const TICKS: usize = 4;
let mut counter = 0usize;
while counter != TICKS {
sleep(2);
let now = get_timestamp_sec();
println!("{now}: {data}");
yield_me().await;
counter += 1;
}
}
fn get_timestamp_sec() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
}
fn pending_print(task_name: &str) {
let now = get_timestamp_sec();
println!("{now}: Task {task_name} is pending. Waiting for the next tick...");
}
fn main() {
let mut executor = Executor::new();
executor.set_pending_callback(pending_print);
let mut binding1 = async {
dummy_func("hello").await;
};
let mut binding2 = async {
dummy_func("world").await;
};
let _ = executor.spawn("hello", &mut binding1);
let _ = executor.spawn("world", &mut binding2);
executor.run();
println!("Done!");
}