Colorless 🎨🚫
Colorless is a Rust toolkit for writing one body of control-flow that can run in multiple execution contexts, including direct synchronous code and unboxed asynchronous state machines. Its public facade is the colorless crate; colorless-core contains the runtime traits and built-in colors.
The Dilemma: Function Coloring in Rust
In Rust, the ecosystem is fundamentally bifurcated into two worlds: synchronous (std::io, blocking loops, iterators) and asynchronous (async/await, tokio, futures).
If you are writing protocol parsers, serializers, streaming codecs, or core business logic, you are typically forced into a frustrating compromise:
- Duplicate your codebase: Maintain and test nearly identical implementations (
parse_syncvsparse_async). - Accept runtime penalties: Force users into
Box<dyn Future>, dynamic trait objects, or runtime block-on wrappers that murder throughput and memory locality.
Colorless provides a third way.
By combining zero-cost algebraic runtime traits (colorless-core) with an ANF-based procedural lowering compiler (colorless-macros), Colorless allows you to write control-flow logic once. At compile time, the compiler monomorphizes your code directly into the target execution context:
- Direct Synchronous Execution (
Identity): Straight-line, native machine code with direct function calls and zero runtime abstraction penalty. - Unboxed Asynchronous Futures (
Async): Tight, stack-allocated, unboxed asynchronous state machines without heap allocations. - Pluggable Colors: Cleanly extensible to other execution semantics (fallibility, lazy evaluation, generators).
Quick Tour
1. Basic Sequential Pipeline
use ;
// 1. Run synchronously — direct execution, zero overhead:
let sync_result = calculate.sync_;
assert_eq!;
// 2. Run asynchronously — unboxed future, zero heap allocations:
let async_future = calculate.async_;
// await in any async runtime (Tokio, async-std, smol, etc.)
2. Conditional Branching Across Suspension Points
use ;
// Works identically in both worlds:
let sync_data = fetch_payload.sync_;
let async_data = fetch_payload.async_.await;
3. Streaming Parsers & Callbacks (True Source of Color)
use ;
use ;
/// A streaming parser that works with sync or async sources and sinks.
// 1. Drive with synchronous closures pulling from in-memory iterators:
let sync_count = parse_records.sync_?;
// 2. Drive with asynchronous closures pulling from sockets or streams:
let async_count = parse_records.async_.await?;
Key Highlights
- ⚡ Zero-Cost & Unboxed: No
Box<dyn Future>, no dynamic dispatch, no hidden allocations. Generates monomorphized machine code or tightly-packed unboxed state machines. - 📦
no_stdFirst-Class: Core abstractions require no heap allocator—designed from day one for embedded systems, kernels, and memory-constrained environments. - 🔄 Procedural ANF Lowering: The procedural macro transforms arbitrary high-level Rust syntax (branches, loops, early returns, match expressions) into an explicit, verifiable control-flow graph.
- 🛡️ Inert Verification: Emits parallel unreachable inert branches for the standard Rust borrow checker to validate lifetimes, moves, and borrow correctness before emitting runtime carriers.
Status & Safety Model (Alpha)
This repository is preparing for its initial alpha release. The core runtime abstractions are stable and tested, but the procedural compiler is in active development and is not yet a transparent drop-in replacement for arbitrary Rust code.
⚠️ Relaxed Implicit Destruction Model
Macro-transformed code currently operates under a relaxed implicit destruction model:
- A local binding that crosses a
.resolvepoint may be dropped earlier or later than the lexical end of its enclosing scope. - Sibling locals may be dropped in an order different from standard Rust reverse-lexical drop order.
[!IMPORTANT] Do not use the macro on code whose correctness relies on precise RAII destruction timing (e.g. mutex locks, database transaction guards, linear resources, or cleanup-sensitive subscriptions).
For cleanup-sensitive paths, use ordinary Rust or establish an explicit, separately-audited ownership boundary. This is a deliberate semantic trade-off of the current compiler model, not merely a temporary optimization quirk.
Feature Maturity Matrix
| Feature | Status | Notes |
|---|---|---|
Direct calls & .resolve |
✅ Ready | Full support for chained, sequential, and nested resolve calls |
| Conditionals & Branching | ✅ Ready | if/else and match expressions crossing resolve points |
| Loops | 🔄 Guided | Supported via explicit #[state(...)] annotations |
| Local Pinning & Borrowing | 🧪 Experimental | Handled via audited colorless-rt carriers; active research area |
| Strict Lexical RAII | 📋 Roadmap | Planned for the post-alpha production release |
Examples
Runnable examples are located in crates/facade/examples/:
# 1. Streaming parser with sync & async callbacks:
# 2. Sequential computation pipeline:
# 3. Conditional branching with fallback:
See the Examples Guide for more details.
Repository Layout
crates/facade(colorless): The user-facing crate re-exporting core traits, runners (sync_(),async_()), and macros (#[colorless]).crates/library/core:Color,Colored, built-in execution contexts (Identity,Async), and zero-cost capability traits.crates/library/rt: Runtime support machinery, including audited local borrow carriers and safety guards.crates/compiler/{lower,codegen,macros}: The ANF lowering, carrier synthesis, and macro generation pipeline.
Development & Verification
Verification and release gates are documented in RELEASE.md. To run the full verification suite:
- For the unsafe local-pinning contract and its test matrix, see SAFETY.md.
- For dependency release order and crates.io publishing, see PUBLISHING.md.
- Please read CONTRIBUTING.md before modifying lowering rules or runtime state machines.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.