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§
- Stackless
Fn - A
StacklessFn<'stack, T>
is used to represent an asynchronous closure. The “entry point” of a recursive function should implement this. The parameterstack
of typeStack
can be used to run deeply recursive functions without blowing up the stack.