-
Notifications
You must be signed in to change notification settings - Fork 1
Add overload proc macro for #[splat] function overloading
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
teor2345
merged 8 commits into
rustfoundation:main
from
Ajay-singh1:overloading-macro-for-splat
Jul 16, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c14e490
Add proof of concept for overload! macro
Ajay-singh1 b9f2089
Fix Cargo.toml dependencies for splat-overload
Ajay-singh1 904c1c4
Add support for handling multiple function arguments
Ajay-singh1 abdc608
Fix formatting and add test files
Ajay-singh1 37e3407
Ignore clippy approximate constants check
teor2345 7fa68ad
Fix CI errors
Ajay-singh1 a097ad7
Add 3-part dependency versions
teor2345 a0d90ce
Replace accidentally deleted rust-version
teor2345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [toolchain] | ||
|
Ajay-singh1 marked this conversation as resolved.
|
||
| channel = "nightly" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [package] | ||
| name = "splat-overload-test" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| splat-overload = { path = "../splat-overload" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [toolchain] | ||
| channel = "nightly" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #![feature(splat)] | ||
| #![feature(tuple_trait)] | ||
| #![allow(incomplete_features, clippy::approx_constant)] | ||
|
|
||
| use splat_overload::overload; | ||
|
Ajay-singh1 marked this conversation as resolved.
|
||
|
|
||
| overload! { | ||
| fn foo(x: i32, y: f64) { | ||
| println!("i32: {}, f64: {}", x, y); | ||
| } | ||
| fn foo(x: bool, y: i32, z: f64) { | ||
| println!("bool: {}, i32: {}, f64: {}", x, y, z); | ||
| } | ||
| fn foo(a: i32, b: f64, c: bool, d: u8) { | ||
| println!("i32: {}, f64: {}, bool: {}, u8: {}", a, b, c, d); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| foo(42, 3.14); | ||
| foo(true, 42, 3.14); | ||
| foo(42, 3.14, true, 255); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #![feature(splat)] | ||
| #![feature(tuple_trait)] | ||
| #![allow(incomplete_features)] | ||
|
|
||
| use splat_overload::overload; | ||
|
|
||
| overload! { | ||
| fn calculate(a: i32, b: i32) { | ||
| println!("sum: {}", a + b); | ||
| } | ||
| fn calculate(a: f64, b: f64, c: f64) { | ||
| println!("average: {}", (a + b + c) / 3.0); | ||
| } | ||
| fn calculate(x: i32, y: i32, z: i32, w: i32) { | ||
| println!("product: {}", x * y * z * w); | ||
| } | ||
| fn calculate(a: f64, b: f64, c: f64, d: f64, e: f64) { | ||
| println!("max would need std: {} {} {} {} {}", a, b, c, d, e); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| calculate(10, 20); | ||
| calculate(1.0, 2.0, 3.0); | ||
| calculate(2, 3, 4, 5); | ||
| calculate(1.0, 2.0, 3.0, 4.0, 5.0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,89 @@ | ||
| //! Delete this comment and add the actual code here | ||
| use proc_macro::TokenStream; | ||
| use quote::quote; | ||
| use syn::{ | ||
| FnArg, ItemFn, Pat, Result, | ||
| parse::{Parse, ParseStream}, | ||
| parse_macro_input, | ||
| }; | ||
|
|
||
| struct OverloadInput { | ||
| functions: Vec<ItemFn>, | ||
| } | ||
|
|
||
| impl Parse for OverloadInput { | ||
| fn parse(input: ParseStream) -> Result<Self> { | ||
| let mut functions = Vec::new(); | ||
| while !input.is_empty() { | ||
| functions.push(input.parse::<ItemFn>()?); | ||
| } | ||
| Ok(OverloadInput { functions }) | ||
| } | ||
| } | ||
|
|
||
| #[proc_macro] | ||
| pub fn overload(input: TokenStream) -> TokenStream { | ||
| let OverloadInput { functions } = parse_macro_input!(input as OverloadInput); | ||
|
|
||
| let fn_name = &functions[0].sig.ident; | ||
|
|
||
| let trait_name = quote::format_ident!( | ||
| "{}Args", | ||
| fn_name | ||
| .to_string() | ||
| .chars() | ||
| .enumerate() | ||
| .map(|(i, c)| if i == 0 { | ||
| c.to_uppercase().next().unwrap() | ||
| } else { | ||
| c | ||
| }) | ||
| .collect::<String>() | ||
| ); | ||
|
|
||
| let mut impls = Vec::new(); | ||
| for func in &functions { | ||
| // Collect All arguments and names | ||
| let mut arg_types = Vec::new(); | ||
| let mut arg_names = Vec::new(); | ||
| let mut arg_indices = Vec::new(); | ||
| let block = &func.block; | ||
| for (i, arg) in func.sig.inputs.iter().enumerate() { | ||
| if let FnArg::Typed(pat_type) = arg { | ||
| let ty = &pat_type.ty; | ||
| arg_types.push(quote! { #ty }); | ||
|
|
||
| let arg_name = if let Pat::Ident(pat_ident) = &*pat_type.pat { | ||
| let ident = &pat_ident.ident; | ||
| quote! { #ident } | ||
| } else { | ||
| quote! { _arg } | ||
| }; | ||
| arg_names.push(arg_name); | ||
|
|
||
| let index = syn::Index::from(i); | ||
| arg_indices.push(quote! { self.#index }); | ||
| } | ||
| } | ||
| impls.push(quote! { | ||
| impl #trait_name for (#(#arg_types),*,) { | ||
| fn call(self) { | ||
| #(let #arg_names = #arg_indices;)* | ||
| #block | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| let generated = quote! { | ||
| trait #trait_name: std::marker::Tuple { | ||
| fn call(self); | ||
| } | ||
|
|
||
| #(#impls)* | ||
|
|
||
| fn #fn_name<T: #trait_name>(#[splat] args: T) { | ||
| args.call() | ||
| } | ||
| }; | ||
|
|
||
| generated.into() | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.