Crates.io | async_as_symmetric_coroutines |
lib.rs | async_as_symmetric_coroutines |
version | 0.0.1 |
source | src |
created_at | 2024-06-24 05:03:57.393629 |
updated_at | 2024-06-24 05:03:57.393629 |
description | Helpers to use `async` code as symmetric coroutines in stable Rust. |
homepage | |
repository | https://github.com/DerickEddington/async_as_symmetric_coroutines |
max_upload_size | |
id | 1281865 |
size | 26,930 |
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
.
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.