An open API service indexing awesome lists of open source software.

https://github.com/maikklein/variadic

Variadics in stable Rust
https://github.com/maikklein/variadic

Last synced: 6 months ago
JSON representation

Variadics in stable Rust

Awesome Lists containing this project

README

          

# Not yet functional

## Overview

This is just a little fun project and should probably not be used in production. Currently variadics are only implemented up to a dimension of `4`. `Variadic`'s are only pssible because of default types and I consider this implementaion a `hack`.

`Variadic` is the trait that makes variadic arguments possible in stable Rust. `VarArgs1`, `VarArgs2` etc implement `Variadic` which allows the user the call `pop()`.

`pop` will return the first argument inside an `Option` and another `VarArgs(n-1)`. For example

~~~
let (Some(value), rest: VarArgs2) = VarArgs3(1, 2, 3).pop();
~~~

## Examples:

A simple sum example implemented with recursion.
~~~
fn sum>(args: Args) -> i32 {
if let (Some(front), rest) = args.pop() {
front + sum(rest)
} else {
0
}
}
println!("sum: {}", sum(VarArgs4(1, 2, 3, 4)));
~~~

Here we call `pop` on `VarArgsN` and it will return `(Option, VarArgs(N-1))`. The recursion stops at `VarArgs0` which returns a `(Option, VarArgs0)` where `Option` will always be `None`.

~~~
fn fact>(args: Args) -> i32 {
if let (Some(front), rest) = args.pop() {
front * fact(rest)
} else {
1
}
}
println!("fact: {}", fact(VarArgs4(1, 2, 3, 4)));
~~~

It is also possible to use traits with `Variadic`. Here we constrain `T` with `std::fmt::Debug`, then we print out every value that we `pop` off until we reach `VarArgs0`.

~~~
fn debug_print>(args: Args)
where T: std::fmt::Debug
{
if let (Some(front), rest) = args.pop() {
println!("{:?}", front);
debug_print(rest);
}
}

debug_print(VarArgs3(1, 2, 3));
~~~