Skip to main content

Crate miniloop

Crate miniloop 

Source
Expand description

§Miniloop

Miniloop is an educational Rust crate designed to teach the basics of building executors for asynchronous tasks. It provides a simple and comprehensive executor that helps in understanding how futures and task scheduling work under the hood.

§Features

  • No Standard Library: This crate is #![no_std], making it suitable for embedded and other constrained environments.
  • Simple API: Easy to use API to spawn and run tasks.
  • Educational Purpose: Designed with learning in mind, this crate breaks down the concepts of executors to their simplest form.

§Modules

  • executor: Contains the core executor implementation.
  • helpers: Utility functions and types to assist with task management.
  • task: Definitions and management of tasks.

§Examples

§Spawning and Running a Single Task

use miniloop::executor::Executor;
use miniloop::task::Task;

const TASK_ARRAY_SIZE: usize = 1;
let mut executor = Executor::<TASK_ARRAY_SIZE>::new();

let mut task = Task::new("task", async {
    println!("Hello, world!");
});
let mut handle = task.create_handle();

executor.spawn(&mut task, &mut handle).expect("Failed to spawn task");
executor.run();

§Handling Multiple Tasks

use miniloop::executor::Executor;
use miniloop::task::Task;

const TASK_ARRAY_SIZE: usize = 2;
let mut executor = Executor::<TASK_ARRAY_SIZE>::new();

let mut task1 = Task::new("task1", async {
    println!("Task 1 executed");
});
let mut handle1 = task1.create_handle();

let mut task2 = Task::new("task2", async {
    println!("Task 2 executed");
});
let mut handle2 = task2.create_handle();

executor.spawn(&mut task1, &mut handle1).expect("Failed to spawn task 1");
executor.spawn(&mut task2, &mut handle2).expect("Failed to spawn task 2");

executor.run();

§Testing

This crate includes several tests demonstrating its usage and ensuring its correctness. The tests cover:

  • Running a single future
  • Running multiple futures
  • Handling the scheduling of too many tasks

To run the tests, use the following command:

cargo test

I hope Miniloop helps you understand the fundamentals of asynchronous programming and task scheduling in Rust. Happy learning!

Modules§

executor
Executor implementation
helpers
Helper module
task
Task implementation