Giter VIP home page Giter VIP logo

variadiz's Introduction

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.

variadiz's People

Contributors

nichtshsu avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.