4 releases (2 stable)

2.0.0 Mar 15, 2024
1.0.0 Sep 24, 2022
0.1.0 Mar 20, 2022
0.0.1 Mar 18, 2022

#240 in Procedural macros

Download history 28/week @ 2024-02-19 36/week @ 2024-02-26 119/week @ 2024-03-11 21/week @ 2024-03-18

177 downloads per month

Apache-2.0

26KB
399 lines

opt_args: Optional arguments for functions and structs in Rust

Crates.io Crates.io docs.rs

This crate allows you to auto-generate macros to call functions and instantiate structs with default named arguments

Import the macro and use it on a function or struct like this

use opt_args::opt_args;

opt_args! {
    fn function(a: i32, b: &str = "default", c: (u8,)?) -> (i32, &str, (u8,)) {
        (a, b, c)
    }
}

opt_args! {
    #[derive(Debug, PartialEq, Eq)]
    struct Struct {
        x: i32,
        y: i32 = 1,
        z: i32?,
        other: Option<Box<Self>>?,
    }
}

To auto-generate macros that can be used like this

fn main() {
    assert_eq!(
        function!(1, b = "not the default"),
        (1, "not the default", (0,))
    );
    assert_eq!(
        Struct!(4, z = 5),
        Struct {
            x: 4,
            y: 1,
            z: 5,
            other: None
        }
    );
}

Full documentation here

Dependencies

~3.5MB
~74K SLoC