named_func_args 1.0.0-beta.1

Proc macro to create boilerplate to simulate function with named arguments
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 2 items with examples
  • Size
  • Source code size: 39.18 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 339.22 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • DoumanAsh/named_func_args
    0 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • DoumanAsh

named_func_args

Rust Crates.io Documentation

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

Limitations

  • References without lifetime parameter will have explicit lifetime with name 'implicit_lifetime
  • Generics may require explicit ?Sized as struct's generic parameters always Sized
  • Const generics are attached to actual function struct, even when not used by arguments

Why?

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

Usage

 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");

Examples

Plain function

Just because why not

 use named_func_args::named_args;

 #[named_args]
 fn my_func() {
 }

 my_func.call();

Multiple arguments of the same type

 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");

Multiple generics of the same type

 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");

Const generics

 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\"]");