23 releases (15 stable)
1.4.0 | May 27, 2024 |
---|---|
1.3.2 | May 24, 2024 |
0.3.0 | May 7, 2024 |
0.2.0 | May 3, 2024 |
0.1.4 | Apr 29, 2024 |
#572 in Rust patterns
49 downloads per month
25KB
80 lines
variadiz
Variadic function support for Rust.
Install
cargo add variadiz
Example
use variadiz::*;
#[variadic]
fn print<T, U>(mut counter: usize, non_variadic: T, others: Option<U>)
where
T: std::fmt::Display,
U: std::fmt::Debug,
{
#[va_expand_ref(mut counter: usize)]
{
println!("{counter}: {:?}", others);
*counter += 1;
}
#[va_expand_mut]
{
others.take();
}
#[va_expand(mut counter: usize, non_variadic: T)]
{
println!("[{non_variadic}] {counter}: {:?}", others);
*counter += 1;
}
}
print(
0,
20240429,
va_args!(Some("hello"), Some(vec![1, 2, 3]), Some('e')),
);
Outputs:
0: Some("hello")
1: Some([1, 2, 3])
2: Some('e')
[20240429] 3: None
[20240429] 4: None
[20240429] 5: None
As methods:
use std::fmt::Debug;
use variadiz::*;
struct Person<'a, T>
where
T: Debug,
{
name: &'a str,
age: u32,
tags: Vec<T>,
}
#[variadic_impl]
impl<'a, T> Person<'a, T>
where
T: Debug,
{
// Non-variadic method
fn hello(&self) -> &'static str {
"hello"
}
#[variadic]
fn grow_up<U>(&mut self, others: U)
where
U: std::fmt::Debug,
{
#[va_expand(hello: &str, who: &str, mut age: u32, tags: Vec<T>)]
#[va_bind(hello = self.hello())]
#[va_bind(who = self.name)]
#[va_bind(age = self.age)]
#[va_bind(tags = self.tags)]
{
println!("{hello}, {who} is {age} years old,");
println!("\tthey are {tags:?},");
println!("\tand do not forget {others:?}");
*self.age += 1;
}
}
}
let mut person = Person {
name: "John",
age: 16,
tags: vec!["smart", "awesome"],
};
person.grow_up(va_args!("hell", Some(62), 0.96));
Outputs:
hello, John is 16 years old,
they are ["smart", "awesome"],
and do not forget "hell"
hello, John is 17 years old,
they are ["smart", "awesome"],
and do not forget Some(62)
hello, John is 18 years old,
they are ["smart", "awesome"],
and do not forget 0.96
Check the documentation page for details.
Dependencies
~0.8–1.3MB
~24K SLoC