CH: Self-Contained Async Bi-Directional Channel
A minimal, single-threaded Rust library for bi-directional communication using async handlers, with zero external dependencies. It is purposely not possible to share channels across threads.
Features
- Sync API, Async Logic: Completely hides
async/awaitimplementation details from the caller. - Stateful Handlers: Supports
FnMutclosures, allowing handlers to maintain and mutate their own state across requests. - Zero Dependencies: Built entirely on the Rust standard library (
std). - Self-Sufficient: Each channel instance is independent and contains its own internal executor logic—no global state or background threads required.
- Bi-Directional: Send a request and block until a response is received from an asynchronous handler.
- Lightweight: Ideal for embedded systems or local task coordination where a full-blown async runtime is overkill.
Usage
Basic Example
use Channel;
Stateful Handlers (FnMut)
You can capture and mutate state directly within the handler closure.
use Channel;
let mut count = 0;
let channel = new;
assert_eq!;
assert_eq!;
Nested Communication
Channels can be cloned and shared, even across other channel handlers.
use Channel;
let doubler = new;
let adder = new;
assert_eq!;
How it Works
ch implements a minimal "micro-executor" inside each send call. When you call send(), the library:
- Invokes your async handler to get a
Future. - Creates a local
Wakertied to the current stack frame. - Polls the future to completion in a loop, ensuring the library remains single-threaded and avoids global task queues.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Running Tests
License
This project is licensed under the Creative Commons Attribution 4.0 International License. See the LICENSE file for details.