Vararg

vararg tests status

This crate provides vararg proc-macro, that can be applied to any function which last arg is Vec, array or reference to slice, turning it into variadic macro, similar to println! or format!

Examples

```rust use vararg::vararg;

[vararg]

fn vararg_func(a: &str, arr: [&str; L]) -> String { format!("\nfirst: {} \nlast: {} \n", a, arr.join(",")) }

fn main() { let s1 = "\n\ first: first \n\ last: \n\ "; let s2 = "\n\ first: first \n\ last: last \n\ "; let s3 = "\n\ first: first \n\ last: last1,last2 \n\ "; asserteq!(s1, varargfunc!("first")); asserteq!(s2, varargfunc!("first", "last")); asserteq!(s3, varargfunc!("first", "last1", "last2")); } ```

You can create vararg function without first required argument (unlike vararg functions in C or nightly Rust). For example, let's create vararg function to join String slices without separator:

```rust use vararg::vararg;

[vararg]

fn join_strs(arr: [&str; L]) -> String { arr.join("") }

fn main() { let s1 = ""; let s2 = "ha"; let s3 = "hahaha"; asserteq!(s1, joinstrs!()); asserteq!(s2, joinstrs!("ha")); asserteq!(s3, joinstrs!("ha", "ha", "ha")); } ```

You also can change name of generated macro or type of last arg

```rust use vararg::vararg; use std::process::Command;

[vararg(name = exec, type = slice)] // or type = array (default), or type = vec

fn execprocess(basename: &str, args: &[&str]) -> String { String::fromutf8( Command::new(basename) .args(args) .output() .expect("failed to execute echo") .stdout, ) .expect("failed to parse hello") }

fn main() { let (name, c) = if cfg!(targetos = "windows") { ("cmd", "/C") } else { ("sh", "-c") }; asserteq!("hello\n", exec!(name, c, "echo hello")); } ```

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.