4 releases

0.0.3 Jan 29, 2024
0.0.2 May 21, 2023
0.0.1 Apr 24, 2023
0.0.0 Jan 16, 2022

#256 in Rust patterns

Download history 12/week @ 2023-11-03 13/week @ 2023-11-10 11/week @ 2023-11-17 17/week @ 2023-11-24 9/week @ 2023-12-01 13/week @ 2023-12-08 8/week @ 2023-12-15 18/week @ 2023-12-22 5/week @ 2023-12-29 8/week @ 2024-01-05 19/week @ 2024-01-12 32/week @ 2024-01-19 194/week @ 2024-01-26 292/week @ 2024-02-02 89/week @ 2024-02-09 307/week @ 2024-02-16

891 downloads per month
Used in 4 crates (2 directly)

Apache-2.0

13KB
201 lines

Variadics

Variadic generics in stable Rust

Variadic Generics?

Variadic generics are one of the most discussed potential Rust features. They would enable traits, functions, and data structures to be generic over variable length tuples of arbitrary types.

Currently you can only implement generic code for tuples of a specific length. If you want to handle tuples of varying lengths you must write a separate implementation for each length. This leads to the notorious limitation that traits in Rust generally only apply for tuples up to length 12.

Variadic generics allow generic code to handle tuples of any length.

Tuple lists

Although variadic generics fundamentally require changing the Rust compiler, we can emulate pretty well with tuple lists.

Any tuple (A, B, C, D) can be mapped to (and from) a recursive tuple (A, (B, (C, (D, ())))).

Each element consists of a nested pair (Item, Rest), where Item is tuple element and Rest is the rest of the list. For last element Rest is a unit tuple (). Unlike regular flat tuples, these recursive tuples can be effectively reasoned about in stable Rust.

You may recognize this fundamental structure from cons lists in Lisp as well as HLists in Haskell.

This crate calls these lists "variadics" and provides traits and macros to allow simple, ergonimc use of them.

Usage

var_expr! creates variadic values/expressions, var_type! creates variadic types, and var_args! creates variadic patterns (used in unpacking arguments, on the left side of let declarations, etc.).

These macros support the "spread" syntax ..., also known as "splat". For example, var_expr!(a, ...var_b, ...var_c, d) will concatenate a, the items of var_b, the items of var_c and d together into a single variadic list.

Acknowledgements

This crate is based on tuple_list by VFLashM, which is MIT licensed:

MIT license
Copyright (c) 2020 Valerii Lashmanov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

Dependencies

~0.4–0.8MB
~19K SLoC