call-recursion 0.1.0

Hack async to do recursion on the heap.
Documentation
  • Coverage
  • 20%
    1 out of 5 items documented1 out of 5 items with examples
  • Size
  • Source code size: 18.63 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.38 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Kirby0717/call-recursion
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Kirby0717

call-recursion

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());
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.