#async #wormhole

async-wormhole

Async calls across non-async functions

10 releases

0.3.3 Feb 4, 2021
0.3.2 Feb 1, 2021
0.3.1 Jan 30, 2021
0.2.2 Jan 27, 2021
0.1.1 Sep 4, 2020
Download history 8/week @ 2020-11-19 29/week @ 2020-11-26 15/week @ 2020-12-03 53/week @ 2020-12-10 13/week @ 2020-12-17 34/week @ 2020-12-24 6/week @ 2020-12-31 2/week @ 2021-01-07 24/week @ 2021-01-14 47/week @ 2021-01-21 62/week @ 2021-01-28 40/week @ 2021-02-04 26/week @ 2021-02-11 50/week @ 2021-02-18 14/week @ 2021-02-25

103 downloads per month

Apache-2.0/MIT

58KB
858 lines

async-wormhole

Documentation

This library is still super experimental, I use it to prototype the foundation for Lunatic .

Currently only works in Rust nightly, as it depends on switcheroo.

async-wormhole allows you to .await async calls in non-async functions, like extern "C" or JIT generated code. It runs on Windows, MacOs and Linux (x64 & AArch64).

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.

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

Performance

There should be almost no performance overhead to .await calls inside the closure passed to AsyncWormhole::new and caught by async_suspend. But instantiating a new AsyncWormhole will require one memory allocation. And of course you are not going to get perfectly sized stacks.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies