auto-tuple
Element-wise tuple-ization for Rust traits: an #[auto_tuple] attribute macro
that generates, for each requested arity, a helper trait whose items forward
element-wise to every element of an N-tuple. Package auto-tuple, crate
auto_tuple.
use auto_tuple;
;
;
// Generated for arity 2 (among others):
//
// trait _TrTuple2<__T0, __T1> where __T0: Tr, __T1: Tr { ... }
// impl<__T0: Tr, __T1: Tr> _TrTuple2<__T0, __T1> for (__T0, __T1) { ... }
The helper traits are generated in the same module as the original trait and
share its visibility. Within that module the tuple-ized methods are callable
directly; cross-module calls need the helper trait imported, exactly like
any other trait's methods (use std::io::Read to call .read()).
Core semantics
Every selected trait item is tuple-ized element-wise:
.foo ==
MAX ==
Output ==
There is no other semantics: no arithmetic merging, no short-circuiting.
Usage
// default arities 2..=12, all methods
// explicit arity range
// arities 0 and 1
// only the `foo` method
// only assoc const `MAX` and assoc type `Output`
- Ranges follow Rust range semantics (
2..12excludes 12,2..=12includes it). - Compile cost: each arity generates one helper trait + blanket impl, so
the default
2..=12emits 11 pairs per annotated trait. For heavy trait graphs prefer a tighter range (#[auto_tuple(2..=4)]) to keep build times down. - Without an explicit item list all methods are processed; associated consts and types are processed only when explicitly named.
- A visibility override (
pub,pub(crate), ...) applies to the generated helper traits; by default they inherit the original trait's visibility. - The helper traits are generated in the same module as the original trait and
share its visibility; bring them into scope with
useto call the tuple-ized methods.
Two shapes: shared and All
Whether a trait lands in the shared or the All shape is decided by whether any selected item's signature references an original trait generic parameter:
| Shared | All | |
|---|---|---|
| Trigger | a selected item's signature references an original generic param | no selected item references the original generic params (any shape: multiple type params, lifetimes, const params) |
| Helper trait | _TrTuple2<__T0, __T1, T> where __T0: Tr<T>, __T1: Tr<T> |
_TrTuple2All<__T0, __T1, <per-element param groups>> where __T0: Tr<...>, __T1: Tr<...> |
| Element params | shared original params | one independent param group per element (A: Tr<i32, S>, B: Tr<String, U> allowed) |
| Bounds | original params lifted as-is | original type param bounds carry to the element group (Tr<T: Clone> → per-element Clone) |
| Arity 0/1 names | _TrTuple0 / _TrTuple1 |
_TrTuple0All / _TrTuple1All |
Traits without generic parameters always use the plain name and an
unparameterized element bound (__T0: Tr).
Supported / unsupported
Supported:
&self/&mut self/selfreceivers and associated functions- generic methods,
whereclauses, lifetimes (trait-level'ais lifted into the helper trait; elided-lifetime returns such asfn name(&self) -> &strwork) async fn(sequential await) andunsafe fn/unsafe traitimpl Trait(RPIT) returns: each element keeps its own opaque type (fn f() -> impl Iterator<Item = Self>becomes a tuple of per-element opaques)Selfanywhere in return types, recursively (&Self,Box<Self>,Vec<Self>,Self::Output, ...)Self,&Self,&mut Self,Self::Associn parameters (unpacked per element)- associated consts and types (incl. bounds such as
type Output: Clone;)
Unsupported (clear compile_error!):
Selfnested inside a generic container in parameters (Box<Self>params,Vec<Self>params)- by-value
impl Traitparameters (a single value cannot be forwarded to multiple elements; use a reference or aCopygeneric) impl Traitparameters mentioningSelf(a single value cannot satisfy the per-elementItemconstraints)- custom receivers (
self: Box<Self>etc.) + use<..>precise capturing in returns (the trait/impl capture lists of the per-element opaques are not yet handled)- generic associated types (GATs)
Selfinside where-clause bounds in non-subject position- auto traits
- multiple arity ranges in one attribute
Left to the compiler (no pre-emption):
- by-value non-
Copyparameters of arity >= 2 ((A::g(x), B::g(x))movesxtwice -> E0382, as with any two calls) - generic
async fnif the toolchain rejects them
Design
See docs/design.md for the full design: semantics,
two-track decision procedure, type rewriting rules, error handling and the
edge-case matrix.
Contributing
See CONTRIBUTING.md for the commit convention and the
quality gate.
License
MIT — see LICENSE.