Crate call_recursion

Crate call_recursion 

Source
Expand description

§Do recursion on the heap

This crate provides a method to avoid stack overflows by converting async functions into state machines and doing recursion on the heap.

§Usage

// Import trait
use call_recursion::FutureRecursion;

// Writing deeply recursive functions async
async fn pow_mod(base: usize, n: usize, r#mod: usize) -> usize {
    if n == 0 {
        1
    }
    else {
        // Call 'recurse' method to recurse over the heap
        // 'recurse' return Future
        (base * pow_mod(base, n - 1, r#mod).recurse().await) % r#mod
    }
}

fn main() {
    // Call 'start_recursion' method at the beginning of the recursion.
    // Return value of 'start_recursion' is not changed
    println!("{}", pow_mod(2, 10_000_000, 1_000_000).start_recursion());
}

Structs§

Output

Traits§

FutureRecursion