Crate vuot

Source
Expand description

Run recursive async functions without overflowing the stack.

use std::future::Future;
use vuot::{run, StacklessFn, Stack};

async fn count(stack: Stack<'_>, n: u64) -> u64 {
    if n == 0 {
        n
    } else {
        stack.run(count(stack, n - 1)).await + 1
    }
}

struct Count(u64);
impl<'a> StacklessFn<'a, u64> for Count {
    fn call(self, stack: Stack<'a>) -> impl Future<Output = u64> {
        count(stack, self.0)
    }
}

assert_eq!(1_000_000, run(Count(1_000_000)).await);

Structs§

Stack
This is a “handle” to the virtual call stack where futures are stored and polled.

Traits§

StacklessFn
A StacklessFn<'stack, T> is used to represent an asynchronous closure. The “entry point” of a recursive function should implement this. The parameter stack of type Stack can be used to run deeply recursive functions without blowing up the stack.

Functions§

run
Run the given function with a virtual Stack that can be used to call functions without growing the actual call stack.