async_as_symmetric_coroutines

Crates.ioasync_as_symmetric_coroutines
lib.rsasync_as_symmetric_coroutines
version0.0.1
sourcesrc
created_at2024-06-24 05:03:57.393629
updated_at2024-06-24 05:03:57.393629
descriptionHelpers to use `async` code as symmetric coroutines in stable Rust.
homepage
repositoryhttps://github.com/DerickEddington/async_as_symmetric_coroutines
max_upload_size
id1281865
size26,930
Derick Eddington (DerickEddington)

documentation

README

async_as_symmetric_coroutines

Helpers to use async code as symmetric coroutines in stable Rust. Just an experiment of mine that became a bit more developed. The general design of this might have potential when coroutines need to move between threads (e.g. for work stealing), since that seems possible when these are Send, as opposed to other coroutine designs that have a call-stack for each which can't be Send.

Example

use async_as_symmetric_coroutines::{Coroutine, Suspender};

type Input1 = Option<Coroutine<Input2>>;
type Input2 = i32;

async fn coro1(_myself: Coroutine<Input1>, suspend: Suspender<Input1>, input: Input1) {
    let coro2 = input.unwrap();
    let input = suspend.yield_to(&coro2, 123).await;
    assert!(input.is_none());
}

let (coro1, coro1_fut) = Coroutine::with_input(coro1);

let (_coro2, coro2_fut) = Coroutine::new(|myself, suspend| async move {
    let input = suspend.yield_to(&coro1, Some(myself.clone())).await;
    coro1.resume(None).await;
    input.to_string()
});

let t1 = std::thread::spawn(|| pollster::block_on(coro1_fut));
let t2 = std::thread::spawn(|| pollster::block_on(coro2_fut));
let r1 = t1.join().unwrap();
assert_eq!(r1, Ok(()));
let r2 = t2.join().unwrap();
assert_eq!(r2, "123");

See the examples/ and tests/ for more examples.

Commit count: 1

cargo fmt