Crates.io | named_func_args |
lib.rs | named_func_args |
version | 1.0.0-beta.1 |
source | src |
created_at | 2024-11-30 01:46:36.060445 |
updated_at | 2024-11-30 01:46:36.060445 |
description | Proc macro to create boilerplate to simulate function with named arguments |
homepage | |
repository | https://github.com/DoumanAsh/named_func_args |
max_upload_size | |
id | 1466217 |
size | 39,522 |
Proc macro to create boilerplate to simulate function with named arguments
Since we cannot have proper functional calls, we might as well use macro to implement it
'implicit_lifetime
?Sized
as struct's generic parameters always Sized
Just because language lacks basic UX feature, doesn't mean we should have it.
Even if you have tool X to substitute lack of this feature, it doesn't mean language has to suck.
Named arguments are important to avoid writing boilerplate code by hand
use named_func_args::named_args;
#[named_args]
fn my_func<'a, T, T2: Copy + 'a, T3>(arg: T, arg2: T2, arg3: T3, text: &'a str,) -> &'a str where T3: 'a + Copy {
text
}
let result = my_func {
arg: true,
arg2: false,
arg3: 10u8,
text: "my_func_call"
}.call();
assert_eq!(result, "my_func_call");
Just because why not
use named_func_args::named_args;
#[named_args]
fn my_func() {
}
my_func.call();
use named_func_args::named_args;
#[named_args]
fn my_func<'a>(arg1: &'a str, arg2: &str) -> String {
format!("{arg1}+{arg2}")
}
let result = my_func { arg1: "1", arg2: "2" }.call();
assert_eq!(result, "1+2");
use named_func_args::named_args;
use core::fmt;
#[named_args]
fn my_func<'a, T: ?Sized + fmt::Display>(arg1: &'a T, arg2: &T) -> String {
format!("{arg1}+{arg2}")
}
let result = my_func { arg1: "1", arg2: "2" }.call();
assert_eq!(result, "1+2");
use named_func_args::named_args;
use core::fmt;
#[named_args]
fn my_func<'a, T: ?Sized + fmt::Display + fmt::Debug, const N: usize>(arg1: &'a T, arg2: &[&'a T; N]) -> String {
format!("{arg1}+{:?}", arg2)
}
let result = my_func { arg1: "1", arg2: &["2"] }.call();
assert_eq!(result, "1+[\"2\"]");