Home

Awesome

open-coroutine

crates.io docs.rs LICENSE Build Status Codecov Average time to resolve an issue Percentage of issues still open

The open-coroutine is a simple, efficient and generic stackful-coroutine library.

πŸš€ Features

πŸ•Š Roadmap

πŸ“– Quick Start

step1: add dependency to your Cargo.toml

[dependencies]
# check https://crates.io/crates/open-coroutine
open-coroutine = "x.y.z"

step2: add macro

#[open_coroutine::main]
fn main() {
    //......
}

step3: create a task

#[open_coroutine::main]
fn main() {
    let task = open_coroutine::task!(|param| {
        assert_eq!(param, 1);
    }, 1);
    task.timeout_join(std::time::Duration::from_secs(1)).expect("timeout");
}

step4: scalable stack(optional)

#[open_coroutine::main]
fn main() {
    _ = open_coroutine::task!(|_| {
        fn recurse(i: u32, p: &mut [u8; 10240]) {
            open_coroutine::maybe_grow!(|| {
                // Ensure the stack allocation isn't optimized away.
                unsafe { _ = std::ptr::read_volatile(&p) };
                if i > 0 {
                    recurse(i - 1, &mut [0; 10240]);
                }
            })
            .expect("allocate stack failed")
        }
        println!("[coroutine] launched");
        // Use ~500KB of stack.
        recurse(50, &mut [0; 10240]);
    }, ());
}

βš“ Learn

ζˆ‘ζœ‰ζ•…δΊ‹,δ½ ζœ‰ι…’ε—?