hetero-cartesian
hetero-cartesian is a Rust procedural macro designed to elegantly flatten nested, callback-based heterogeneous control flows (like deeply nested visitors or inverted iterations) into a clean, unified cartesian product block with absolute zero overhead.
Why this crate?
Iterators are great, but they require all yielded elements to be of the exact same type. When dealing with control flows that yield highly heterogeneous data (different types, varying const bounds) via callback traits, you can't just collect them into a Vec or use itertools.
Imagine two disjoint systems. One yields arrays of varying lengths (const generic N), and another yields tuples of completely different types (type generics T, U):
use Display;
If you want to iterate over the cartesian product of these two systems, standard for loops or iterators completely fail here. Cleanly connecting these requires you to write complex, deeply nested overlapping struct implementations manually.
cartesian_fn! elegantly flattens this with absolute zero overhead:
- 0 Dynamic Dispatch: It generates strictly typed, nested generic struct implementations on the fly. No
Box<dyn Trait>or runtime type checks. - 0 Memory Allocations: Your local environment is captured and passed seamlessly down the call stack. No heap allocation is required.
- 100% Static Context: Bounds are propagated precisely. The control flow is flattened into a unified block where the innermost execution has full access to exact compile-time types, trait bounds, and
constgenerics.
Usage
Define your environments, map your driver iterations to their struct boundaries, and let the macro do the heavy lifting:
use cartesian_fn;
// The attribute macro expands this into a plain fn that takes all args.
// No env-capture boilerplate, no type duplication.