#coroutine #executor #abstraction #dispatcher #cancellation #async-task #coroutine-scope #dispatchers

rs_coroutine_core

Composable coroutine utilities and flow abstractions built on top of Rust's async ecosystem

2 releases

0.1.1 Dec 6, 2025
0.1.0 Dec 6, 2025

#1262 in Rust patterns


Used in coroflow

MIT/Apache

13KB
271 lines

rs_coroutine_core

Core primitives for building coroutine-style asynchronous workflows in Rust with structured concurrency and dispatcher-aware execution.

Features

  • CoroutineScope for lifecycle-managed coroutines and hierarchical cancellation.
  • Dispatcher abstraction to run work on Tokio executors (Main, IO, Default) or custom executors.
  • async_task and Deferred<T> for parallel work with join-style awaiting.
  • Task-local scope access via CURRENT_SCOPE.
  • suspend_block! macro for reusable suspending computations.

Installation

Add the crate to your project:

[dependencies]
rs_coroutine_core = "0.1.1"
tokio = { version = "1.35", features = ["full"] }

Example

Launch a coroutine and switch dispatchers for blocking work:

use rs_coroutine_core::{CoroutineScope, Dispatchers};
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let scope = Arc::new(CoroutineScope::new(Dispatchers::main()));

    let job = scope.launch(async move {
        let data = scope.with_dispatcher(Dispatchers::io(), async {
            // Background work
            "data".to_string()
        }).await;

        println!("Received: {}", data);
    });

    job.join().await;
}

For more examples, run cargo run --example basic_usage from the workspace root.

License

MIT OR Apache-2.0

Dependencies

~2.8–4.5MB
~71K SLoC