[][src]Crate async_wormhole

async-wormhole allows you to call .await async calls across non-async functions, like extern "C" or JIT generated code.

Motivation

Sometimes, when running inside an async environment you need to call into JIT generated code (e.g. wasm) and .await from there. Because the JIT code is not available at compile time, the Rust compiler can't do their "create a state machine" magic. In the end you can't have .await statements in non-async functions.

This library creates a special stack for executing the JIT code, so it's possible to suspend it at any point of the execution. Once you pass it a closure inside AsyncWormhole::new you will get back a future that you can .await on. The passed in closure is going to be executed on a new stack.

Sometimes you also need to preserve thread local storage as the code inside the closure expects it to stay the same, but the actual execution can be moved between threads. There is a proof of concept API to allow you to move your thread local storage with the closure across threads.

Example

use async_wormhole::{AsyncWormhole, AsyncYielder};
use switcheroo::stack::*;

// non-async function
extern "C" fn non_async(mut yielder: AsyncYielder<u32>) -> u32 {
	// Suspend the runtime until async value is ready.
	// Can contain .await calls.
    yielder.async_suspend(async { 42 })
}

fn main() {
    let stack = EightMbStack::new().unwrap();
    let task = AsyncWormhole::new(stack, |yielder| {
        let result = non_async(yielder);
        assert_eq!(result, 42);
        64
    })
    .unwrap();

    let outside = futures::executor::block_on(task);
    assert_eq!(outside.unwrap(), 64);
}

Modules

pool

Structs

AsyncWormhole

AsyncWormhole captures a stack and a closure. It also implements Future and can be awaited on.

AsyncYielder