Crates.io | duang |
lib.rs | duang |
version | 0.1.2 |
source | src |
created_at | 2019-06-15 13:38:58.694193 |
updated_at | 2019-06-15 13:51:45.000208 |
description | Use macro to provide default parameter and named parameter in Rust |
homepage | |
repository | https://github.com/xiaoniu-578fa6bff964d005/duang |
max_upload_size | |
id | 141334 |
size | 45,662 |
Rust doesn't support default function arguments and named function arguments.
This crate generate an macro interface for a given function that can be invoked with named arguments and fill the default argument, meanwhile keep the old function intact.
In order to generate macro for a function, we just need to wrap the definition with duang!{...}
.
use duang::duang;
duang!(
pub fn foo<T>(a: T,b: f64 = 13.0, c: T = a*a) -> (T,f64,T)
where
T: std::ops::Mul<T, Output = T>,
T: std::fmt::Display,
T: Copy,
{
(a,b,c)
}
);
use demo_duang::foo;
// pass
assert_eq!(foo!(1, c = 30, b = -2.0), (1, -2.0, 30));
// pass
assert_eq!(foo!(a = 10), (10, 13.0, 100));
// fail
// foo!(1,c=30,c=2);
In order to use the generated macro in other crate, users should add $crate
and path of the variable used.
Also, the variable should be visible(pub
) for the scope where the macro is invoked.
mod bar {
use duang::duang;
pub static NUM: i32 = 42;
duang!(
pub fn foo(a: i32 = $crate::bar::NUM) -> i32 { a }
);
}
fn main() {
use bar::foo;
assert_eq!(foo!(), 42);
}
fn foo((a,_): (i32, i32))
is illegal.License: MIT