autoargs
A Rust procedural macro for generating argument structs with default values, allowing for named arguments and partial argument specification. Works with both standalone functions and struct methods.
Overview
When you annotate a function or method with #[autoargs], the macro:
- Generates a struct to hold the function's arguments
- Implements
Defaultfor that struct, using specified default expressions - Creates a macro that lets you call the function with named arguments
Examples
Functions
use ;
;
;
;
// Call with named arguments (unspecified arguments use defaults)
let result = draw!;
Methods
For methods, you need to use both autoargs on the methods and impl_autoargs on the impl block:
use ;
let canvas = new;
// All defaults
let result1 = draw_rectangle!;
// Some custom values
let result2 = draw_rectangle!;
// Create and pass an args struct
let args = DrawRectangleArgs ;
let result3 = draw_rectangle!;
Generated Code
For functions, the macro generates:
For methods, the autoargs attribute generates the args struct and modifies the method itself, while the impl_autoargs attribute creates the macro outside the impl block (since Rust doesn't allow macro definitions inside impl blocks):
// Generated by autoargs
// Method implementation is modified by autoargs
// Generated by impl_autoargs
Features
- Named arguments with Rust-like syntax
- Default values for arguments via attributes
- Skip any argument to use its default value
- Works with functions and all method types (
&self,&mut self,self) - Generates proper structs and macros with correct visibility
- Proper CamelCase naming convention for generated structs
Advanced Usage
Creating Custom Arg Structs
You can create a custom args struct and pass it directly:
let custom_args = DrawArgs ;
// Pass the args struct directly
let result = draw!;
Using Default Trait
If a parameter doesn't have a #[default = "..."] attribute, it will use the type's Default implementation:
Best Practices
- Always use specific types for your parameters that implement the required traits
- Provide meaningful default values for each parameter
- Break complex functions into smaller functions with clear argument sets
- Use descriptive parameter names
Implementation Notes
Due to Rust's limitations that prevent macro definitions within impl blocks, a two-part approach is used for methods:
#[autoargs]attribute on the method: Generates the args struct and rewrites the method to take the args struct#[impl_autoargs]attribute on the impl block: Identifies all#[autoargs]methods and generates macros for them outside the impl block#[default = "..."]attribute uses thedefaultattribute from this crate
This approach allows for a clean, ergonomic usage pattern while respecting Rust's constraints.
Important: When using the library, make sure to import all the necessary components:
// For functions
use ;
// For methods in impl blocks
use ;
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
License
MIT
Disclaimer
This crate was 100% vibe coded after my friend went on a 15-minute rant about how Rust has no default args and how all the reasons are bikeshedding or ridiculous complaints about how default args are "code smell".